Manage DB disconnection

This commit is contained in:
Benjamin Renard 2014-08-17 19:44:49 +02:00 committed by root
parent d61c64112a
commit 0bdae5c0f4

View file

@ -25,8 +25,17 @@ 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):
if self.connect():
try: try:
c=self.con.cursor() c=self.con.cursor()
c.execute(sql) c.execute(sql)
@ -37,12 +46,14 @@ class DB(object):
return False return False
def select(self,sql): def select(self,sql):
if self.connect():
ret=self.do_sql(sql) ret=self.do_sql(sql)
if ret!=False: if ret!=False:
return ret.fetchall() return ret.fetchall()
return ret return ret
def login(self,email,password): def login(self,email,password):
if self.connect():
ret=self.select("SELECT email,name,password FROM users WHERE email='%s' AND password='%s'" % (email,password)) ret=self.select("SELECT email,name,password FROM users WHERE email='%s' AND password='%s'" % (email,password))
log.debug(ret) log.debug(ret)
if ret: if ret:
@ -58,6 +69,7 @@ class DB(object):
return { 'loginerror': 'Erreur inconnu' } return { 'loginerror': 'Erreur inconnu' }
def subscribe(self,email,name,password): def subscribe(self,email,name,password):
if self.connect():
ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email)) ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email))
log.debug(ret) log.debug(ret)
if ret[0][0]!=0: if ret[0][0]!=0:
@ -72,6 +84,7 @@ class DB(object):
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):
if self.connect():
db_groups=self.get_group(email) db_groups=self.get_group(email)
if db_groups!=False: if db_groups!=False:
db_grouplist=group.GroupList() db_grouplist=group.GroupList()
@ -85,8 +98,11 @@ class DB(object):
else: else:
return {'syncerror': 'Erreur en modifiant les informations de la base de donnees'} return {'syncerror': 'Erreur en modifiant les informations de la base de donnees'}
return {'syncerror': 'Erreur inconnu'} 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):
if self.connect():
ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email) ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
if ret!=False: if ret!=False:
if len(ret)==1: if len(ret)==1:
@ -97,6 +113,7 @@ class DB(object):
return False return False
def set_group(self,email,groups): def set_group(self,email,groups):
if self.connect():
ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email) ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email)
if ret!=False: if ret!=False:
json_groups=json.dumps(groups) json_groups=json.dumps(groups)