Index: controlies/trunk/applications/controlies/controllers/gestion.py
===================================================================
--- controlies/trunk/applications/controlies/controllers/gestion.py	(revision 377)
+++ controlies/trunk/applications/controlies/controllers/gestion.py	(revision 378)
@@ -3,12 +3,13 @@
 from applications.controlies.modules.Rayuela2Ldap import Rayuela  
 import applications.controlies.modules.Utils.LdapUtils as LdapUtils
 from applications.controlies.modules.SQLiteConnection import SQLiteConnection
+from applications.controlies.modules.Laptops import Laptops
+from applications.controlies.modules.LaptopsHistory import LaptopsHistory
 
 @service.json   
 @auth.requires_login()
 def base_datos():
     import StringIO
-    import pdb
     import os
 
     right_version=right_firefox_version(request.env.http_user_agent)
@@ -15,71 +16,53 @@
 
     if not "archivos" in session.keys():
         session.archivos=[]    
+   
     form=SQLFORM.factory(submit_button='Enviar')
     if form.accepts(request.vars, session):
-        import pdb
         response.flash = 'Procesando datos, espere'
         #borrando = form.vars.principiocurso
         if len(session.archivos)>0:
             import pdb
             lista_completa=[]                           
-            SQLite=SQLiteConnection()
-            SQLite.define_tables()
+            #SQLite=SQLiteConnection()
+            #SQLite.define_tables()
             
-            dbcontrolies = SQLite.getDB()
+            #dbcontrolies = SQLite.getDB()
             #result = db.executesql(sql)
             for archivo in session.archivos:
                 f = open(archivo)
                 linea = f.readline()
+                last_id = ""
                 while linea:
                     if linea!= '\n' and linea != '':
                         linea = linea.split(",")
                         # Datos del portatil
                   
-                        serial_number = linea[1]
+                        serial_number = linea[1].replace("'","")
                         id_trademark = linea[2]
-                        
-                        # comprobar que no existe el portatil con el nº de serie
-                        row = dbcontrolies(dbcontrolies.laptops.serial_number == serial_number).select(dbcontrolies.laptops.id_laptop)
-                        if not row:
-                            attributes = dict ()
-                            attributes["serial_number"] = serial_number
-                            attributes["id_trademark"] = id_trademark
-                            #attributes="[serial_number="+serial_number+", id_trademark="+id_trademark + "]"
-                            dbcontrolies.laptops.insert (**attributes)
-                            # Obtener id del registro insertado
-                            for row in dbcontrolies(dbcontrolies.laptops.serial_number == serial_number).select(dbcontrolies.laptops.id_laptop):
-                                last_id = row.id_laptop
-
-                            if last_id:
-                                # Datos del registro historico
-                                attributes = dict ()
-                                attributes ["id_laptop"] = last_id
-                                attributes ["datetime"] = linea [4]
-                                attributes ["username"] = linea [9]
-                                attributes ["name"] = linea [10]
-                                attributes ["id_user_type"] = linea [6]
-                                attributes ["nif"] = linea [5]
-                                attributes ["comment"] = ""
-                                attributes ["id_state"] = linea [8]
+                        laptop = Laptops (cdb,"",serial_number, id_trademark)
+                                                
+                        if not laptop.existsSerialNumber (laptop.serial_number):
+                            laptop.add ()
+                            last_id = laptop.getIdbySerialNumber (laptop.serial_number)
                                 
-                                dbcontrolies.laptops_historical.insert (**attributes)
+                        if last_id:
+                            # Datos del registro historico
+                            id_laptop = last_id
+                            username = linea [9].replace("'","")
+                            name = linea [10].replace("'","")
+                            id_user_type = linea [6]
+                            nif = linea [5].replace("'","")
+                            id_state = linea [8]
+                                
+                            laptopshistory = LaptopsHistory (cdb, "", id_laptop, id_state, id_user_type, nif, username, name, "")
+                            laptopshistory.add ()
+                            #dbcontrolies.laptops_historical.insert (**attributes)
+                                
                     linea = f.readline()
 
             response.flash = T('Ficheros importados correctamente')        
             session.archivos=[]
-            #LdapUtils.sanea_grupos(l)                               
-            #l.close()
-            
-            #generamos el archivo de salida 
-            #s=StringIO.StringIO()
-
-                          
-            #return s.getvalue()
-
-
-        #session.archivos=[]
-  
         
     return dict(form=form,right_version=right_version)
     
Index: controlies/trunk/applications/controlies/modules/LaptopsHistory.py
===================================================================
--- controlies/trunk/applications/controlies/modules/LaptopsHistory.py	(revision 377)
+++ controlies/trunk/applications/controlies/modules/LaptopsHistory.py	(revision 378)
@@ -30,8 +30,8 @@
     def __init__(self):
         pass
     
-    def __init__(self,SQLite,id_historical,id_laptop,id_state,id_user_type,nif,username,name,comment):
-        self.SQLite = SQLite
+    def __init__(self,DB,id_historical,id_laptop,id_state,id_user_type,nif,username,name,comment):
+        self.DB = DB
         self.id_historical = str(id_historical)        
         self.id_laptop = str(id_laptop)
         self.id_state = str(id_state)
@@ -75,13 +75,12 @@
                 return response
         
     def list(self,args):
-        db = self.SQLite.getDB()
 
         sql = "SELECT id_historical, datetime, state, username, name, comment FROM laptops_historical lh INNER JOIN states s ON lh.id_state=s.id_state"
         sql = sql + " WHERE lh.id_laptop='"+str(args["id_laptop"])+"'"
         sql = sql + " ORDER BY "+args['sidx']+" "+args['sord']
-        result = db.executesql(sql)
-
+        result = self.DB.executesql(sql)
+        
         rows = []
         for reg in result:
             d = datetime.datetime.strptime(str(reg[1]), '%Y-%m-%d %H:%M:%S')
@@ -123,34 +122,26 @@
     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) "
-        sql = sql + "VALUES(null,'"+self.id_laptop+"','"+now.strftime('%Y-%m-%d %H:%M:%S')+"','"+self.username+"','"+self.name+"','"+self.id_user_type+"','"+self.nif+"','"+self.comment+"','"+self.id_state+"')"
-        db = self.SQLite.getDB()
-        result = db.executesql(sql)
+        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)
         
         return "OK"
             
             
     def modify(self):
-        sql = "UPDATE laptops_historical SET username='"+self.username+"', name='"+self.name+"', id_user_type='"+self.id_user_type+"', nif='"+self.nif+"', comment='"+self.comment+"', id_state='"+self.id_state+"' "
-        sql = sql + "WHERE id_historical='"+str(self.id_historical)+"'"
-        db = self.SQLite.getDB()
-        result = db.executesql(sql)
+        now = datetime.datetime.now()
+        self.DB(self.DB.laptops_historical.id_historical==self.id_historical).update(username=self.username, name=self.name, id_user_type=self.id_user_type, nif=self.nif, comment = self.comment, id_state = self.id_state, datetime=now.strftime('%Y-%m-%d %H:%M:%S'))        
                     
         return "OK"
 
 
     def delete(self):
-        sql = "DELETE FROM laptops_historical WHERE id_historical='"+str(self.id_historical)+"'"
-
-        db = self.SQLite.getDB()
-        result = db.executesql(sql)
-            
+        self.DB(self.DB.laptops_history.id_historical==self.id_historical).delete()
+                    
         return "OK"
 
     def getAllStates(self):
         sql="SELECT * FROM states ORDER BY state"
-        db = self.SQLite.getDB()
-        result = db.executesql(sql)
+        result = self.DB.executesql(sql)
 
         data=[]
         for r in result:
@@ -163,8 +154,7 @@
 
     def getAllUserTypes(self):
         sql="SELECT * FROM users_types ORDER BY user_type"
-        db = self.SQLite.getDB()
-        result = db.executesql(sql)
+        result = self.DB.executesql(sql)
 
         data=[]
         for r in result:
@@ -177,8 +167,7 @@
 
     def getDataHistory(self):
         sql="SELECT * FROM laptops_historical WHERE id_historical='"+str(self.id_historical)+"'"
-        db = self.SQLite.getDB()
-        result = db.executesql(sql)
+        result = self.DB.executesql(sql)
 
         data = {"id_historical":"","id_laptop":"","username":"","name":"","id_user_type":"","nif":"","comment":"","id_state":""}
         if len(result)>0:
@@ -197,8 +186,7 @@
 
     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"
-        db = self.SQLite.getDB()
-        result = db.executesql(sql)
+        result = self.DB.executesql(sql)
 
         data={"username":"","id_state":""}
         if len(result)>0:
@@ -213,8 +201,7 @@
         sql="SELECT lh.id_laptop, lh.username FROM laptops l, laptops_historical lh"
         sql=sql+" WHERE l.id_laptop=lh.id_laptop"
         sql=sql+" GROUP BY l.id_laptop ORDER BY lh.datetime desc"
-        db = self.SQLite.getDB()
-        result = db.executesql(sql)
+        result = self.DB.executesql(sql)
 
         for r in result:
             if str(r[1])==self.username:
Index: controlies/trunk/applications/controlies/modules/Laptops.py
===================================================================
--- controlies/trunk/applications/controlies/modules/Laptops.py	(revision 377)
+++ controlies/trunk/applications/controlies/modules/Laptops.py	(revision 378)
@@ -155,7 +155,6 @@
 
     def add(self):
         self.DB.laptops.insert(serial_number=self.serial_number,id_trademark=self.id_trademark)
-        
         return "OK"
             
             
@@ -212,4 +211,11 @@
         sql="SELECT max(id_laptop) AS max FROM laptops"
         result = self.DB.executesql(sql)
 
-        return str(result[0][0])
\ No newline at end of file
+        return str(result[0][0])
+    
+    def getIdbySerialNumber (self, serial_number):
+        sql="SELECT id_laptop FROM laptops WHERE serial_number = '" + serial_number + "'"
+        result = self.DB.executesql(sql)
+        
+        return str(result[0][0])
+        
\ No newline at end of file
