From 65d09225b1b8112bf24a2e0adaca616e5973b0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Sun, 23 Feb 2020 21:27:03 +0100 Subject: [PATCH] finished ##189 it test if the date is lower or equal then date now if true the month will be locked in soft mode if one day exists in database the status of locked will not change --- geruecht/controller/databaseController.py | 281 +++++++++++++++------- geruecht/controller/userController.py | 9 + geruecht/user/routes.py | 5 +- geruecht/vorstand/routes.py | 5 +- 4 files changed, 205 insertions(+), 95 deletions(-) diff --git a/geruecht/controller/databaseController.py b/geruecht/controller/databaseController.py index fe84080..c88c977 100644 --- a/geruecht/controller/databaseController.py +++ b/geruecht/controller/databaseController.py @@ -6,6 +6,7 @@ from geruecht.model.creditList import CreditList from datetime import datetime, timedelta from geruecht.exceptions import UsernameExistDB, DatabaseExecption import traceback +from MySQLdb._exceptions import IntegrityError class DatabaseController(metaclass=Singleton): ''' @@ -18,41 +19,55 @@ class DatabaseController(metaclass=Singleton): self.db = db def getAllUser(self): - cursor = self.db.connection.cursor() - cursor.execute("select * from user") - data = cursor.fetchall() + try: + 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 + if data: + retVal = [] + for value in data: + user = User(value) + creditLists = self.getCreditListFromUser(user) + user.initGeruechte(creditLists) + retVal.append(user) + return retVal + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) 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 + try: + 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 + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) 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 + try: + 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 + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) def _convertGroupToString(self, groups): retVal = '' @@ -66,86 +81,124 @@ class DatabaseController(metaclass=Singleton): 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() + try: + 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() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) def updateUser(self, user): - cursor = self.db.connection.cursor() - print('uid: {}; group: {}'.format(user.uid, user.group)) - 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() + try: + cursor = self.db.connection.cursor() + print('uid: {}; group: {}'.format(user.uid, user.group)) + 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() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) 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] + try: + 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] + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) 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() + try: + cursor = self.db.connection.cursor() + cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id)) + self.db.connection.commit() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) 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() + try: + 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() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) 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'], "start": { "year": data['startdatetime'].year, "month": data['startdatetime'].month, "day": data['startdatetime'].day}} if data else None - + try: + 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'], "start": { "year": data['startdatetime'].year, "month": data['startdatetime'].month, "day": data['startdatetime'].day}} if data else None + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) 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'], "start": { "year": work['startdatetime'].year, "month": work['startdatetime'].month, "day": work['startdatetime'].day}} for work in data] - + try: + 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'], "start": { "year": work['startdatetime'].year, "month": work['startdatetime'].month, "day": work['startdatetime'].day}} for work in data] + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) 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() + try: + 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() + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) def deleteWorker(self, user, date): @@ -155,6 +208,8 @@ class DatabaseController(metaclass=Singleton): self.db.connection.commit() except Exception as err: traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) def changeUsername(self, user, newUsername): try: @@ -165,9 +220,49 @@ class DatabaseController(metaclass=Singleton): raise UsernameExistDB("Username already exists") else: cursor.execute("update user set uid='{}' where id={}".format(newUsername, user.id)) - self.db.connection() + self.db.connection.commit() except Exception as err: traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) + + def getLockedDay(self, date): + try: + cursor = self.db.connection.cursor() + cursor.execute("select * from locked_days where daydate='{}'".format(date)) + data = cursor.fetchone() + return data + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) + + def setLockedDay(self, date, locked, hard=False): + try: + cursor = self.db.connection.cursor() + sql = "insert into locked_days (daydate, locked) VALUES ('{}', {})".format(date, locked) + cursor.execute(sql) + self.db.connection.commit() + return self.getLockedDay(date) + except IntegrityError as err: + self.db.connection.rollback() + try: + exists = self.getLockedDay(date) + if hard: + sql = "update locked_days set locked={} where id={}".format(locked, exists['id']) + else: + sql = False + if sql: + cursor.execute(sql) + self.db.connection.commit() + return self.getLockedDay(date) + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() + raise DatabaseController("Something went wrong with Database: {}".format(err)) + except Exception as err: + traceback.print_exc() + self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) if __name__ == '__main__': diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index 23ca96d..8171995 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -2,6 +2,7 @@ from . import LOGGER, Singleton, ldapConfig, dbConfig, mailConfig import geruecht.controller.databaseController as dc import geruecht.controller.ldapController as lc import geruecht.controller.emailController as ec +import calendar from geruecht.model.user import User from geruecht.exceptions import PermissionDenied from datetime import datetime, timedelta @@ -16,6 +17,14 @@ class UserController(metaclass=Singleton): def __init__(self): pass + def getLockedDay(self, date): + now = datetime.now() + daysInMonth = calendar.monthrange(date.year, date.month)[1] + if date.year <= now.year and date.month <= now.month: + for i in range(1, daysInMonth + 1): + db.setLockedDay(datetime(date.year, date.month, i).date(), True) + return db.getLockedDay(date.date()) + def getWorker(self, date, username=None): if (username): user = self.getUser(username) diff --git a/geruecht/user/routes.py b/geruecht/user/routes.py index b1af021..469a66e 100644 --- a/geruecht/user/routes.py +++ b/geruecht/user/routes.py @@ -59,6 +59,9 @@ def _getUser(**kwargs): month = data['month'] year = data['year'] date = datetime(year, month, day, 12) - retVal = userController.getWorker(date) + retVal = { + 'worker': userController.getWorker(date), + 'day': userController.getLockedDay(date) + } print(retVal) return jsonify(retVal) \ No newline at end of file diff --git a/geruecht/vorstand/routes.py b/geruecht/vorstand/routes.py index a420330..57680ac 100644 --- a/geruecht/vorstand/routes.py +++ b/geruecht/vorstand/routes.py @@ -34,7 +34,10 @@ def _getUser(**kwargs): month = data['month'] year = data['year'] date = datetime(year, month, day, 12) - retVal = userController.getWorker(date) + retVal = { + 'worker': userController.getWorker(date), + 'day': userController.getLockedDay(date) + } print(retVal) return jsonify(retVal)