Manage DB disconnection
This commit is contained in:
parent
d61c64112a
commit
0bdae5c0f4
1 changed files with 82 additions and 65 deletions
|
@ -25,84 +25,101 @@ class DB(object):
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
log.fatal('Error connecting to database : %s' % e)
|
log.fatal('Error connecting to database : %s' % e)
|
||||||
return
|
return
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
self.con.ping(True)
|
||||||
|
except Exception, e:
|
||||||
|
self.con = 0
|
||||||
|
return self.connect()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def do_sql(self,sql):
|
def do_sql(self,sql):
|
||||||
try:
|
if self.connect():
|
||||||
c=self.con.cursor()
|
try:
|
||||||
c.execute(sql)
|
c=self.con.cursor()
|
||||||
self.con.commit()
|
c.execute(sql)
|
||||||
return c
|
self.con.commit()
|
||||||
except Exception,e:
|
return c
|
||||||
log.error('Error executing request %s : %s' % (sql,e))
|
except Exception,e:
|
||||||
return False
|
log.error('Error executing request %s : %s' % (sql,e))
|
||||||
|
return False
|
||||||
|
|
||||||
def select(self,sql):
|
def select(self,sql):
|
||||||
ret=self.do_sql(sql)
|
if self.connect():
|
||||||
if ret!=False:
|
ret=self.do_sql(sql)
|
||||||
return ret.fetchall()
|
if ret!=False:
|
||||||
return ret
|
return ret.fetchall()
|
||||||
|
return ret
|
||||||
|
|
||||||
def login(self,email,password):
|
def login(self,email,password):
|
||||||
ret=self.select("SELECT email,name,password FROM users WHERE email='%s' AND password='%s'" % (email,password))
|
if self.connect():
|
||||||
log.debug(ret)
|
ret=self.select("SELECT email,name,password FROM users WHERE email='%s' AND password='%s'" % (email,password))
|
||||||
if ret:
|
log.debug(ret)
|
||||||
if len(ret)==1:
|
if ret:
|
||||||
return {
|
if len(ret)==1:
|
||||||
'email': ret[0][0],
|
return {
|
||||||
'name': ret[0][1]
|
'email': ret[0][0],
|
||||||
}
|
'name': ret[0][1]
|
||||||
elif len(ret)>=1:
|
}
|
||||||
log.warning('Duplicate user %s in database' % email)
|
elif len(ret)>=1:
|
||||||
elif ret==():
|
log.warning('Duplicate user %s in database' % email)
|
||||||
return { 'loginerror': 'Utilisateur inconnu' }
|
elif ret==():
|
||||||
return { 'loginerror': 'Erreur inconnu' }
|
return { 'loginerror': 'Utilisateur inconnu' }
|
||||||
|
return { 'loginerror': 'Erreur inconnu' }
|
||||||
|
|
||||||
def subscribe(self,email,name,password):
|
def subscribe(self,email,name,password):
|
||||||
ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email))
|
if self.connect():
|
||||||
log.debug(ret)
|
ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email))
|
||||||
if ret[0][0]!=0:
|
log.debug(ret)
|
||||||
return {'subscribeerror': u'Cette adresse mail est déjà associés a un compte !'}
|
if ret[0][0]!=0:
|
||||||
else:
|
return {'subscribeerror': u'Cette adresse mail est déjà associés a un compte !'}
|
||||||
if self.do_sql("INSERT INTO users (email,name,password) VALUES ('%s','%s','%s')" % (email,name,password)):
|
else:
|
||||||
return {
|
if self.do_sql("INSERT INTO users (email,name,password) VALUES ('%s','%s','%s')" % (email,name,password)):
|
||||||
'email': email,
|
return {
|
||||||
'name': name,
|
'email': email,
|
||||||
'password': password
|
'name': name,
|
||||||
}
|
'password': password
|
||||||
return {'subscribeerror': u'Une erreur est survenue durant votre inscription :('}
|
}
|
||||||
|
return {'subscribeerror': u'Une erreur est survenue durant votre inscription :('}
|
||||||
|
|
||||||
def sync_group(self,email,groups):
|
def sync_group(self,email,groups):
|
||||||
db_groups=self.get_group(email)
|
if self.connect():
|
||||||
if db_groups!=False:
|
db_groups=self.get_group(email)
|
||||||
db_grouplist=group.GroupList()
|
if db_groups!=False:
|
||||||
db_grouplist.load(db_groups)
|
db_grouplist=group.GroupList()
|
||||||
grouplist=group.GroupList()
|
db_grouplist.load(db_groups)
|
||||||
grouplist.load(groups)
|
grouplist=group.GroupList()
|
||||||
synced_grouplist=db_grouplist.sync(grouplist)
|
grouplist.load(groups)
|
||||||
if self.set_group(email,synced_grouplist.export()):
|
synced_grouplist=db_grouplist.sync(grouplist)
|
||||||
log.debug('Groups successfuly synchronized, return result')
|
if self.set_group(email,synced_grouplist.export()):
|
||||||
return {'groups': synced_grouplist.export()}
|
log.debug('Groups successfuly synchronized, return result')
|
||||||
else:
|
return {'groups': synced_grouplist.export()}
|
||||||
return {'syncerror': 'Erreur en modifiant les informations de la base de donnees'}
|
else:
|
||||||
return {'syncerror': 'Erreur inconnu'}
|
return {'syncerror': 'Erreur en modifiant les informations de la base de donnees'}
|
||||||
|
return {'syncerror': 'Erreur inconnu'}
|
||||||
|
else:
|
||||||
|
return {'syncerror': u"Erreur de connexion à la base de données"}
|
||||||
|
|
||||||
def get_group(self,email):
|
def get_group(self,email):
|
||||||
ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
|
if self.connect():
|
||||||
if ret!=False:
|
ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
|
||||||
if len(ret)==1:
|
if ret!=False:
|
||||||
return json.loads(ret[0][0])
|
if len(ret)==1:
|
||||||
|
return json.loads(ret[0][0])
|
||||||
|
else:
|
||||||
|
return {'groups': {}}
|
||||||
else:
|
else:
|
||||||
return {'groups': {}}
|
return False
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def set_group(self,email,groups):
|
def set_group(self,email,groups):
|
||||||
ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
|
if self.connect():
|
||||||
if ret!=False:
|
ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
|
||||||
json_groups=json.dumps(groups)
|
if ret!=False:
|
||||||
if len(ret)==1:
|
json_groups=json.dumps(groups)
|
||||||
return self.do_sql("UPDATE groups SET groups='%s' WHERE email='%s'" % (json_groups,email))
|
if len(ret)==1:
|
||||||
|
return self.do_sql("UPDATE groups SET groups='%s' WHERE email='%s'" % (json_groups,email))
|
||||||
|
else:
|
||||||
|
return self.do_sql("INSERT INTO groups (email,groups) VALUES ('%s','%s')" % (email,json_groups))
|
||||||
else:
|
else:
|
||||||
return self.do_sql("INSERT INTO groups (email,groups) VALUES ('%s','%s')" % (email,json_groups))
|
return False
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
Loading…
Reference in a new issue