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
This commit is contained in:
parent
576002a95c
commit
65d09225b1
|
@ -6,6 +6,7 @@ from geruecht.model.creditList import CreditList
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from geruecht.exceptions import UsernameExistDB, DatabaseExecption
|
from geruecht.exceptions import UsernameExistDB, DatabaseExecption
|
||||||
import traceback
|
import traceback
|
||||||
|
from MySQLdb._exceptions import IntegrityError
|
||||||
|
|
||||||
class DatabaseController(metaclass=Singleton):
|
class DatabaseController(metaclass=Singleton):
|
||||||
'''
|
'''
|
||||||
|
@ -18,6 +19,7 @@ class DatabaseController(metaclass=Singleton):
|
||||||
self.db = db
|
self.db = db
|
||||||
|
|
||||||
def getAllUser(self):
|
def getAllUser(self):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute("select * from user")
|
cursor.execute("select * from user")
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
|
@ -30,8 +32,13 @@ class DatabaseController(metaclass=Singleton):
|
||||||
user.initGeruechte(creditLists)
|
user.initGeruechte(creditLists)
|
||||||
retVal.append(user)
|
retVal.append(user)
|
||||||
return retVal
|
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):
|
def getUser(self, username):
|
||||||
|
try:
|
||||||
retVal = None
|
retVal = None
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute("select * from user where uid='{}'".format(username))
|
cursor.execute("select * from user where uid='{}'".format(username))
|
||||||
|
@ -40,10 +47,14 @@ class DatabaseController(metaclass=Singleton):
|
||||||
retVal = User(data)
|
retVal = User(data)
|
||||||
creditLists = self.getCreditListFromUser(retVal)
|
creditLists = self.getCreditListFromUser(retVal)
|
||||||
retVal.initGeruechte(creditLists)
|
retVal.initGeruechte(creditLists)
|
||||||
|
|
||||||
return retVal
|
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):
|
def getUserById(self, id):
|
||||||
|
try:
|
||||||
retVal = None
|
retVal = None
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute("select * from user where id={}".format(id))
|
cursor.execute("select * from user where id={}".format(id))
|
||||||
|
@ -53,6 +64,10 @@ class DatabaseController(metaclass=Singleton):
|
||||||
creditLists = self.getCreditListFromUser(retVal)
|
creditLists = self.getCreditListFromUser(retVal)
|
||||||
retVal.initGeruechte(creditLists)
|
retVal.initGeruechte(creditLists)
|
||||||
return retVal
|
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):
|
def _convertGroupToString(self, groups):
|
||||||
retVal = ''
|
retVal = ''
|
||||||
|
@ -66,14 +81,20 @@ class DatabaseController(metaclass=Singleton):
|
||||||
|
|
||||||
|
|
||||||
def insertUser(self, user):
|
def insertUser(self, user):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
groups = self._convertGroupToString(user.group)
|
groups = self._convertGroupToString(user.group)
|
||||||
cursor.execute("insert into user (uid, dn, firstname, lastname, gruppe, lockLimit, locked, autoLock, mail) VALUES ('{}','{}','{}','{}','{}',{},{},{},'{}')".format(
|
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))
|
user.uid, user.dn, user.firstname, user.lastname, groups, user.limit, user.locked, user.autoLock, user.mail))
|
||||||
self.db.connection.commit()
|
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):
|
def updateUser(self, user):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
print('uid: {}; group: {}'.format(user.uid, user.group))
|
print('uid: {}; group: {}'.format(user.uid, user.group))
|
||||||
groups = self._convertGroupToString(user.group)
|
groups = self._convertGroupToString(user.group)
|
||||||
|
@ -82,9 +103,14 @@ class DatabaseController(metaclass=Singleton):
|
||||||
print(sql)
|
print(sql)
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
self.db.connection.commit()
|
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):
|
def getCreditListFromUser(self, user, **kwargs):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
if 'year' in kwargs:
|
if 'year' in kwargs:
|
||||||
sql = "select * from creditList where user_id={} and year_date={}".format(user.id, kwargs['year'])
|
sql = "select * from creditList where user_id={} and year_date={}".format(user.id, kwargs['year'])
|
||||||
|
@ -96,15 +122,25 @@ class DatabaseController(metaclass=Singleton):
|
||||||
return [CreditList(data[0])]
|
return [CreditList(data[0])]
|
||||||
else:
|
else:
|
||||||
return [CreditList(value) for value in data]
|
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):
|
def createCreditList(self, user_id, year=datetime.now().year):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id))
|
cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id))
|
||||||
self.db.connection.commit()
|
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):
|
def updateCreditList(self, creditlist):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute("select * from creditList where user_id={} and year_date={}".format(creditlist.user_id, creditlist.year))
|
cursor.execute("select * from creditList where user_id={} and year_date={}".format(creditlist.user_id, creditlist.year))
|
||||||
data = cursor.fetchall()
|
data = cursor.fetchall()
|
||||||
|
@ -127,25 +163,42 @@ class DatabaseController(metaclass=Singleton):
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute(sql)
|
cursor.execute(sql)
|
||||||
self.db.connection.commit()
|
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):
|
def getWorker(self, user, date):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute("select * from bardienste where user_id={} and startdatetime='{}'".format(user.id, date))
|
cursor.execute("select * from bardienste where user_id={} and startdatetime='{}'".format(user.id, date))
|
||||||
data = cursor.fetchone()
|
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
|
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):
|
def getWorkers(self, date):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute("select * from bardienste where startdatetime='{}'".format(date))
|
cursor.execute("select * from bardienste where startdatetime='{}'".format(date))
|
||||||
data = cursor.fetchall()
|
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]
|
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):
|
def setWorker(self, user, date):
|
||||||
|
try:
|
||||||
cursor = self.db.connection.cursor()
|
cursor = self.db.connection.cursor()
|
||||||
cursor.execute("insert into bardienste (user_id, startdatetime, enddatetime) values ({},'{}','{}')".format(user.id, date, date + timedelta(days=1)))
|
cursor.execute("insert into bardienste (user_id, startdatetime, enddatetime) values ({},'{}','{}')".format(user.id, date, date + timedelta(days=1)))
|
||||||
self.db.connection.commit()
|
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):
|
def deleteWorker(self, user, date):
|
||||||
|
@ -155,6 +208,8 @@ class DatabaseController(metaclass=Singleton):
|
||||||
self.db.connection.commit()
|
self.db.connection.commit()
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
self.db.connection.rollback()
|
||||||
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
||||||
|
|
||||||
def changeUsername(self, user, newUsername):
|
def changeUsername(self, user, newUsername):
|
||||||
try:
|
try:
|
||||||
|
@ -165,9 +220,49 @@ class DatabaseController(metaclass=Singleton):
|
||||||
raise UsernameExistDB("Username already exists")
|
raise UsernameExistDB("Username already exists")
|
||||||
else:
|
else:
|
||||||
cursor.execute("update user set uid='{}' where id={}".format(newUsername, user.id))
|
cursor.execute("update user set uid='{}' where id={}".format(newUsername, user.id))
|
||||||
self.db.connection()
|
self.db.connection.commit()
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
traceback.print_exc()
|
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))
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -2,6 +2,7 @@ from . import LOGGER, Singleton, ldapConfig, dbConfig, mailConfig
|
||||||
import geruecht.controller.databaseController as dc
|
import geruecht.controller.databaseController as dc
|
||||||
import geruecht.controller.ldapController as lc
|
import geruecht.controller.ldapController as lc
|
||||||
import geruecht.controller.emailController as ec
|
import geruecht.controller.emailController as ec
|
||||||
|
import calendar
|
||||||
from geruecht.model.user import User
|
from geruecht.model.user import User
|
||||||
from geruecht.exceptions import PermissionDenied
|
from geruecht.exceptions import PermissionDenied
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
@ -16,6 +17,14 @@ class UserController(metaclass=Singleton):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
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):
|
def getWorker(self, date, username=None):
|
||||||
if (username):
|
if (username):
|
||||||
user = self.getUser(username)
|
user = self.getUser(username)
|
||||||
|
|
|
@ -59,6 +59,9 @@ def _getUser(**kwargs):
|
||||||
month = data['month']
|
month = data['month']
|
||||||
year = data['year']
|
year = data['year']
|
||||||
date = datetime(year, month, day, 12)
|
date = datetime(year, month, day, 12)
|
||||||
retVal = userController.getWorker(date)
|
retVal = {
|
||||||
|
'worker': userController.getWorker(date),
|
||||||
|
'day': userController.getLockedDay(date)
|
||||||
|
}
|
||||||
print(retVal)
|
print(retVal)
|
||||||
return jsonify(retVal)
|
return jsonify(retVal)
|
|
@ -34,7 +34,10 @@ def _getUser(**kwargs):
|
||||||
month = data['month']
|
month = data['month']
|
||||||
year = data['year']
|
year = data['year']
|
||||||
date = datetime(year, month, day, 12)
|
date = datetime(year, month, day, 12)
|
||||||
retVal = userController.getWorker(date)
|
retVal = {
|
||||||
|
'worker': userController.getWorker(date),
|
||||||
|
'day': userController.getLockedDay(date)
|
||||||
|
}
|
||||||
print(retVal)
|
print(retVal)
|
||||||
return jsonify(retVal)
|
return jsonify(retVal)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue