Index: controlies/trunk/debian/changelog
===================================================================
--- controlies/trunk/debian/changelog	(revision 388)
+++ controlies/trunk/debian/changelog	(revision 389)
@@ -1,3 +1,9 @@
+controlies (0.6.4-1) unstable; urgency=low
+
+  * Correcci�n de errores y activaci�n de la base de datos
+
+ -- Manuel Mora Gordillo <manuito@gmail.com>  Wed, 26 Sep 2012 10:23:00 +0200
+
 controlies (0.6.3-5) unstable; urgency=low
 
   * Cambio almacenamiento nombre y apellidos
Index: controlies/trunk/applications/controlies/views/laptops/index.html
===================================================================
--- controlies/trunk/applications/controlies/views/laptops/index.html	(revision 388)
+++ controlies/trunk/applications/controlies/views/laptops/index.html	(revision 389)
@@ -48,11 +48,12 @@
                     }
                 },
                 
-                colNames:['Marca','Modelo','Número Serie','Login','Estado'],
+                colNames:['Marca','Modelo','Número Serie','Tipo','Login','Estado'],
                 colModel:[ 
                     {name:'trademark',index:'trademark', width:60, align:"center"},
                     {name:'model',index:'model', width:115, align:"center"},
                     {name:'serial_number',index:'serial_number', width:115, align:"center"},
+                    {name:'user_type',index:'user_type', width:60, align:"center",  stype:'select', editoptions:{value:":Todos;Profesor:Profesores;Alumno:Alumnos;Ninguno:Ninguno"}},
                     {name:'username',index:'username', width:115, align:"center"},
                     {name:'state',index:'state', width:115, align:"center"}
                 ], 
Index: controlies/trunk/applications/controlies/controllers/laptops_groups.py
===================================================================
--- controlies/trunk/applications/controlies/controllers/laptops_groups.py	(revision 388)
+++ controlies/trunk/applications/controlies/controllers/laptops_groups.py	(revision 389)
@@ -112,7 +112,7 @@
         userData = getUserData(request.vars["username"])
 
         max = l.getMaxId()
-        lh = LaptopsHistory(cdb,"",max,"2","2",userData["nif"],request.vars["username"],userData["name"],"Reasignado")
+        lh = LaptopsHistory(cdb,"",max,"2","2",userData["nif"],request.vars["username"],userData["name"],"")
         
         id_laptop = lh.userAssignment()
         if id_laptop:
Index: controlies/trunk/applications/controlies/controllers/laptops.py
===================================================================
--- controlies/trunk/applications/controlies/controllers/laptops.py	(revision 388)
+++ controlies/trunk/applications/controlies/controllers/laptops.py	(revision 389)
@@ -35,6 +35,7 @@
 
     l = Laptops(cdb,request.vars["id_laptop"],serial,request.vars["id_trademark"])
     response = l.process(request.vars["action"])
+        
     return dict(response = response)            
 
 @service.json
Index: controlies/trunk/applications/controlies/modules/Laptops.py
===================================================================
--- controlies/trunk/applications/controlies/modules/Laptops.py	(revision 388)
+++ controlies/trunk/applications/controlies/modules/Laptops.py	(revision 389)
@@ -23,6 +23,7 @@
 ##############################################################################
 
 from math import floor
+from applications.controlies.modules.LaptopsHistory import LaptopsHistory
 
 class Laptops(object):
 
@@ -76,13 +77,16 @@
             
     def list(self,args):
 
-        sql = "SELECT l.id_laptop, lt.trademark, lt.model, l.serial_number, lh.username, s.state"
-        sql = sql+" FROM laptops l"
-        sql = sql+" LEFT JOIN laptops_trademarks lt ON l.id_trademark=lt.id_trademark "
-        sql = sql+" LEFT JOIN laptops_historical lh ON l.id_laptop=lh.id_laptop "        
-        sql = sql+" LEFT JOIN states s ON lh.id_state=s.id_state "        
+        sql = "SELECT l.id_laptop, lt.trademark, lt.model, l.serial_number, ut.user_type, lh.username, s.state"
+        sql = sql+" FROM laptops l, laptops_historical lh"
+        sql = sql+" LEFT JOIN laptops_trademarks lt ON l.id_trademark=lt.id_trademark "        
+        #sql = sql+" INNER JOIN laptops_historical lh ON l.id_laptop=lh.id_laptop"
+        sql = sql+" LEFT JOIN states s ON lh.id_state=s.id_state "
+        sql = sql+" LEFT JOIN users_types ut ON ut.id_user_type=lh.id_user_type "     
         sql = sql+" WHERE 1=1 "
-        
+        sql = sql+" AND l.id_laptop=lh.id_laptop "
+        sql = sql+" AND lh.id_historical IN (SELECT MAX(lh2.id_historical) FROM laptops_historical lh2 WHERE lh2.id_laptop=l.id_laptop) "
+                
         try:
             if str(args['serial_number']) != "None":
                 sql = sql+" AND l.serial_number LIKE '%"+str(args['serial_number'])+"%'"
@@ -102,6 +106,15 @@
             pass
 
         try:
+            if str(args['user_type']) != "None":
+                if str(args['user_type']) == "Ninguno":
+                    sql = sql+" AND (lh.id_user_type='' OR lh.id_user_type='0')"
+                else:
+                    sql = sql+" AND ut.user_type='"+str(args['user_type'])+"'"
+        except LookupError:
+            pass
+
+        try:
             if str(args['username']) != "None":
                 sql = sql+" AND lh.username LIKE '%"+str(args['username'])+"%'"
         except LookupError:
@@ -122,12 +135,13 @@
         for reg in result:
             row = {
 				"id":reg[0],
-				"cell":[reg[1],reg[2],reg[3],reg[4],reg[5]],
+				"cell":[reg[1],reg[2],reg[3],reg[4],reg[5],reg[6]],
 				"trademark":reg[1],
 				"model":reg[2],
 				"serial_number":reg[3],
-                "username":reg[4],
-                "state":reg[5]
+                "user_type":reg[4],
+                "username":reg[5],
+                "state":reg[6],
             }
             rows.append(row)
 
@@ -155,6 +169,11 @@
 
     def add(self):
         self.DB.laptops.insert(serial_number=self.serial_number,id_trademark=self.id_trademark)
+        
+        max = self.getMaxId()
+        lh = LaptopsHistory(self.DB,"",max,"1","0","","","","Nuevo en el sistema")
+        lh.add()
+
         return "OK"
             
             
Index: controlies/trunk/applications/controlies/modules/LaptopsHistory.py
===================================================================
--- controlies/trunk/applications/controlies/modules/LaptopsHistory.py	(revision 388)
+++ controlies/trunk/applications/controlies/modules/LaptopsHistory.py	(revision 389)
@@ -88,7 +88,7 @@
         #result = self.DB.executesql(sql)
 
         id_laptop = str(args["id_laptop"])
-        result=self.DB(self.DB.laptops_historical.id_laptop==id_laptop)(self.DB.laptops_historical.id_state==self.DB.states.id_state).select(self.DB.laptops_historical.ALL, self.DB.states.state, orderby=args['sidx']+" "+args['sord'])
+        result=self.DB(self.DB.laptops_historical.id_laptop==id_laptop)(self.DB.laptops_historical.id_state==self.DB.states.id_state).select(self.DB.laptops_historical.ALL, self.DB.states.state, orderby=args['sidx']+" "+args['sord']+", id_historical desc")
 
         rows = []
         for reg in result:
@@ -128,7 +128,7 @@
         return { "page":page, "total":totalPages, "records":len(rows), "rows":rows[start:finish] }
 
 
-    def add(self):
+    def add(self): 
         now = datetime.datetime.now()
         #sql = "INSERT INTO laptops_historical (id_historical,id_laptop,datetime,username,name,id_user_type,nif,comment,id_state) "
         self.DB.laptops_historical.insert (id_laptop=self.id_laptop, datetime=now.strftime('%Y-%m-%d %H:%M:%S'), username=self.username, name = self.name, id_user_type=self.id_user_type,nif=self.nif,comment=self.comment, id_state=self.id_state)
@@ -194,7 +194,7 @@
         return data
 
     def getLastHistory(self):
-        sql="SELECT username, id_state FROM laptops_historical WHERE id_laptop='"+str(self.id_laptop)+"' ORDER BY datetime desc LIMIT 0,1"
+        sql="SELECT username, id_state FROM laptops_historical WHERE id_laptop='"+str(self.id_laptop)+"' ORDER BY id_historical desc LIMIT 0,1"
         result = self.DB.executesql(sql)
 
         data={"username":"","id_state":""}
