| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  | import pymysql | 
					
						
							| 
									
										
										
										
											2019-12-28 20:52:49 +00:00
										 |  |  | from . import Singleton | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  | from geruecht import db | 
					
						
							| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  | from geruecht.model.user import User | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  | from geruecht.model.creditList import CreditList | 
					
						
							| 
									
										
										
										
											2020-01-18 22:31:49 +00:00
										 |  |  | from datetime import datetime, timedelta | 
					
						
							| 
									
										
										
										
											2020-01-23 22:27:26 +00:00
										 |  |  | import traceback | 
					
						
							| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DatabaseController(metaclass=Singleton): | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  |     DatabaesController | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Connect to the Database and execute sql-executions | 
					
						
							|  |  |  |     '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.db = db | 
					
						
							| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def getAllUser(self): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  |         cursor.execute("select * from user") | 
					
						
							|  |  |  |         data = cursor.fetchall() | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if data: | 
					
						
							| 
									
										
										
										
											2019-12-28 20:52:49 +00:00
										 |  |  |             retVal = [] | 
					
						
							|  |  |  |             for value in data: | 
					
						
							|  |  |  |                 user = User(value) | 
					
						
							|  |  |  |                 creditLists = self.getCreditListFromUser(user) | 
					
						
							|  |  |  |                 user.initGeruechte(creditLists) | 
					
						
							|  |  |  |                 retVal.append(user) | 
					
						
							|  |  |  |             return retVal | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  |     def getUser(self, username): | 
					
						
							|  |  |  |         retVal = None | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  |         cursor.execute("select * from user where uid='{}'".format(username)) | 
					
						
							|  |  |  |         data = cursor.fetchone() | 
					
						
							| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  |         if data: | 
					
						
							|  |  |  |             retVal = User(data) | 
					
						
							| 
									
										
										
										
											2019-12-28 20:52:49 +00:00
										 |  |  |             creditLists = self.getCreditListFromUser(retVal) | 
					
						
							|  |  |  |             retVal.initGeruechte(creditLists) | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  |         return retVal | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 22:31:49 +00:00
										 |  |  |     def getUserById(self, id): | 
					
						
							|  |  |  |         retVal = None | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  |         cursor.execute("select * from user where id={}".format(id)) | 
					
						
							|  |  |  |         data = cursor.fetchone() | 
					
						
							| 
									
										
										
										
											2020-01-18 22:31:49 +00:00
										 |  |  |         if data: | 
					
						
							|  |  |  |             retVal = User(data) | 
					
						
							|  |  |  |             creditLists = self.getCreditListFromUser(retVal) | 
					
						
							|  |  |  |             retVal.initGeruechte(creditLists) | 
					
						
							|  |  |  |         return retVal | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-22 21:27:39 +00:00
										 |  |  |     def _convertGroupToString(self, groups): | 
					
						
							|  |  |  |         retVal = '' | 
					
						
							|  |  |  |         for group in groups: | 
					
						
							|  |  |  |             if len(retVal) != 0: | 
					
						
							|  |  |  |                 retVal += ',' | 
					
						
							|  |  |  |             retVal += group | 
					
						
							|  |  |  |         return retVal | 
					
						
							| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 22:34:09 +00:00
										 |  |  |     def insertUser(self, user): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							| 
									
										
										
										
											2019-12-28 22:34:09 +00:00
										 |  |  |         groups = self._convertGroupToString(user.group) | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         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() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-28 22:34:09 +00:00
										 |  |  |     def updateUser(self, user): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							| 
									
										
										
										
											2019-12-28 22:34:09 +00:00
										 |  |  |         groups = self._convertGroupToString(user.group) | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         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() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  |     def getCreditListFromUser(self, user, **kwargs): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  |         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() | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  |         if len(data) == 1: | 
					
						
							| 
									
										
										
										
											2019-12-26 09:28:30 +00:00
										 |  |  |             return [CreditList(data[0])] | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             return [CreditList(value) for value in data] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  |     def createCreditList(self, user_id, year=datetime.now().year): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							|  |  |  |         cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id)) | 
					
						
							|  |  |  |         self.db.connection.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  |     def updateCreditList(self, creditlist): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         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() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 22:31:49 +00:00
										 |  |  |     def getWorker(self, user, date): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  |         cursor.execute("select * from bardienste where user_id={} and startdatetime='{}'".format(user.id, date)) | 
					
						
							|  |  |  |         data = cursor.fetchone() | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         return {"user": user.toJSON(), "startdatetime": data['startdatetime'], "enddatetime": data['enddatetime']} if data else None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 22:31:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def getWorkers(self, date): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         cursor = self.db.connection.cursor() | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  |         cursor.execute("select * from bardienste where startdatetime='{}'".format(date)) | 
					
						
							|  |  |  |         data = cursor.fetchall() | 
					
						
							| 
									
										
										
										
											2020-01-18 22:31:49 +00:00
										 |  |  |         return [{"user": self.getUserById(work['user_id']).toJSON(), "startdatetime": work['startdatetime'], "enddatetime": work['enddatetime']} for work in data] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 22:31:49 +00:00
										 |  |  |     def setWorker(self, user, date): | 
					
						
							| 
									
										
										
										
											2020-01-19 20:32:58 +00:00
										 |  |  |         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() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-18 22:31:49 +00:00
										 |  |  |     def deleteWorker(self, user, date): | 
					
						
							| 
									
										
										
										
											2020-01-23 22:27:26 +00:00
										 |  |  |         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() | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 07:12:29 +00:00
										 |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2019-12-19 17:26:41 +00:00
										 |  |  |     db = DatabaseController() | 
					
						
							|  |  |  |     user = db.getUser('jhille') | 
					
						
							|  |  |  |     db.getCreditListFromUser(user, year=2018) |