#!/usr/bin/python # -*- coding: utf-8 -*- import json import logging log = logging.getLogger(__name__) import MySQLdb from mycoserver import group class DB(object): def __init__(self,host,user,pwd,db): self.host = host self.user = user self.pwd = pwd self.db = db self.con = 0 def connect(self): if self.con == 0: try: con = MySQLdb.connect(self.host,self.user,self.pwd,self.db) self.con = con return True except Exception, e: log.fatal('Error connecting to database : %s' % e) return else: try: self.con.ping(True) except Exception, e: self.con = 0 return self.connect() return True def do_sql(self,sql): if self.connect(): try: c=self.con.cursor() c.execute(sql) self.con.commit() return c except Exception,e: log.error('Error executing request %s' % sql) log.error('Error executing request : %s' % e) return False def select(self,sql): if self.connect(): ret=self.do_sql(sql) if ret!=False: return ret.fetchall() return ret 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)) log.debug(ret) if ret: if len(ret)==1: return { 'email': ret[0][0], 'name': ret[0][1] } elif len(ret)>=1: log.warning('Duplicate user %s in database' % email) elif ret==(): return { 'loginerror': 'Utilisateur inconnu' } return { 'loginerror': 'Erreur inconnu' } def subscribe(self,email,name,password): if self.connect(): ret=self.select("SELECT count(*) as count FROM users WHERE email='%s'" % (email)) log.debug(ret) if ret[0][0]!=0: return {'subscribeerror': u'Cette adresse mail est déjà associés a un compte !'} else: if self.do_sql("INSERT INTO users (email,name,password) VALUES ('%s','%s','%s')" % (email,name,password)): return { 'email': email, 'name': name, 'password': password } return {'subscribeerror': u'Une erreur est survenue durant votre inscription :('} def sync_group(self,email,groups): if self.connect(): db_groups=self.get_group(email) if db_groups!=False: db_grouplist=group.GroupList() db_grouplist.load(db_groups) grouplist=group.GroupList() grouplist.load(groups) synced_grouplist=db_grouplist.sync(grouplist) if self.set_group(email,synced_grouplist.export()): log.debug('Groups successfuly synchronized, return result') return {'groups': synced_grouplist.export()} else: 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): if self.connect(): ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email) if ret!=False: if len(ret)==1: return json.loads(ret[0][0]) else: return {'groups': {}} else: return False def set_group(self,email,groups): if self.connect(): ret=self.select("SELECT groups FROM groups WHERE email='%s'" % email) if ret!=False: json_groups=json.dumps(groups) 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: return False