import pymysql from . import Singleton from geruecht import db from geruecht.model.user import User from geruecht.model.creditList import CreditList from datetime import datetime, timedelta import traceback class DatabaseController(metaclass=Singleton): ''' DatabaesController Connect to the Database and execute sql-executions ''' def __init__(self): self.db = db def getAllUser(self): cursor = self.db.connection.cursor() cursor.execute("select * from user") data = cursor.fetchall() if data: retVal = [] for value in data: user = User(value) creditLists = self.getCreditListFromUser(user) user.initGeruechte(creditLists) retVal.append(user) return retVal def getUser(self, username): retVal = None cursor = self.db.connection.cursor() cursor.execute("select * from user where uid='{}'".format(username)) data = cursor.fetchone() if data: retVal = User(data) creditLists = self.getCreditListFromUser(retVal) retVal.initGeruechte(creditLists) return retVal def getUserById(self, id): retVal = None cursor = self.db.connection.cursor() cursor.execute("select * from user where id={}".format(id)) data = cursor.fetchone() if data: retVal = User(data) creditLists = self.getCreditListFromUser(retVal) retVal.initGeruechte(creditLists) return retVal def _convertGroupToString(self, groups): retVal = '' for group in groups: if len(retVal) != 0: retVal += ',' retVal += group return retVal def insertUser(self, user): cursor = self.db.connection.cursor() groups = self._convertGroupToString(user.group) cursor.execute("insert into user (uid, dn, firstname, lastname, gruppe, lockLimit, locked, autoLock, mail) VALUES ('{}','{}','{}','{}','{}',{},{},{},'{}')".format( user.uid, user.dn, user.firstname, user.lastname, groups, user.limit, user.locked, user.autoLock, user.mail)) self.db.connection.commit() def updateUser(self, user): cursor = self.db.connection.cursor() groups = self._convertGroupToString(user.group) sql = "update user set dn='{}', firstname='{}', lastname='{}', gruppe='{}', lockLimit={}, locked={}, autoLock={}, mail='{}' where uid='{}'".format( user.dn, user.firstname, user.lastname, groups, user.limit, user.locked, user.autoLock, user.mail, user.uid) print(sql) cursor.execute(sql) self.db.connection.commit() def getCreditListFromUser(self, user, **kwargs): cursor = self.db.connection.cursor() if 'year' in kwargs: sql = "select * from creditList where user_id={} and year_date={}".format(user.id, kwargs['year']) else: sql = "select * from creditList where user_id={}".format(user.id) cursor.execute(sql) data = cursor.fetchall() if len(data) == 1: return [CreditList(data[0])] else: return [CreditList(value) for value in data] def createCreditList(self, user_id, year=datetime.now().year): cursor = self.db.connection.cursor() cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id)) self.db.connection.commit() def updateCreditList(self, creditlist): cursor = self.db.connection.cursor() cursor.execute("select * from creditList where user_id={} and year_date={}".format(creditlist.user_id, creditlist.year)) data = cursor.fetchall() if len(data) == 0: self.createCreditList(creditlist.user_id, creditlist.year) sql = "update creditList set jan_guthaben={}, jan_schulden={},feb_guthaben={}, feb_schulden={}, maer_guthaben={}, maer_schulden={}, apr_guthaben={}, apr_schulden={}, mai_guthaben={}, mai_schulden={}, jun_guthaben={}, jun_schulden={}, jul_guthaben={}, jul_schulden={}, aug_guthaben={}, aug_schulden={},sep_guthaben={}, sep_schulden={},okt_guthaben={}, okt_schulden={}, nov_guthaben={}, nov_schulden={}, dez_guthaben={}, dez_schulden={}, last_schulden={} where year_date={} and user_id={}".format(creditlist.jan_guthaben, creditlist.jan_schulden, creditlist.feb_guthaben, creditlist.feb_schulden, creditlist.maer_guthaben, creditlist.maer_schulden, creditlist.apr_guthaben, creditlist.apr_schulden, creditlist.mai_guthaben, creditlist.mai_schulden, creditlist.jun_guthaben, creditlist.jun_schulden, creditlist.jul_guthaben, creditlist.jul_schulden, creditlist.aug_guthaben, creditlist.aug_schulden, creditlist.sep_guthaben, creditlist.sep_schulden, creditlist.okt_guthaben, creditlist.okt_schulden, creditlist.nov_guthaben, creditlist.nov_schulden, creditlist.dez_guthaben, creditlist.dez_schulden, creditlist.last_schulden, creditlist.year, creditlist.user_id) print(sql) cursor = self.db.connection.cursor() cursor.execute(sql) self.db.connection.commit() def getWorker(self, user, date): cursor = self.db.connection.cursor() cursor.execute("select * from bardienste where user_id={} and startdatetime='{}'".format(user.id, date)) data = cursor.fetchone() return {"user": user.toJSON(), "startdatetime": data['startdatetime'], "enddatetime": data['enddatetime']} if data else None def getWorkers(self, date): cursor = self.db.connection.cursor() cursor.execute("select * from bardienste where startdatetime='{}'".format(date)) data = cursor.fetchall() return [{"user": self.getUserById(work['user_id']).toJSON(), "startdatetime": work['startdatetime'], "enddatetime": work['enddatetime']} for work in data] def setWorker(self, user, date): cursor = self.db.connection.cursor() cursor.execute("insert into bardienste (user_id, startdatetime, enddatetime) values ({},'{}','{}')".format(user.id, date, date + timedelta(days=1))) self.db.connection.commit() def deleteWorker(self, user, date): try: cursor = self.db.connection.cursor() cursor.execute("delete from bardienste where user_id={} and startdatetime='{}'".format(user.id, date)) self.db.connection.commit() except Exception as err: traceback.print_exc() if __name__ == '__main__': db = DatabaseController() user = db.getUser('jhille') db.getCreditListFromUser(user, year=2018)