2019-12-19 07:12:29 +00:00
|
|
|
import pymysql
|
|
|
|
from geruecht import Singleton
|
|
|
|
from geruecht.model.user import User
|
2019-12-19 17:26:41 +00:00
|
|
|
from geruecht.model.creditList import CreditList
|
|
|
|
from datetime import datetime
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
class DatabaseController(metaclass=Singleton):
|
|
|
|
'''
|
|
|
|
DatabaesController
|
|
|
|
|
|
|
|
Connect to the Database and execute sql-executions
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, url='192.168.5.108', user='wu5', password='E1n$tein', database='geruecht'):
|
|
|
|
self.url = url
|
|
|
|
self.user = user
|
|
|
|
self.password = password
|
|
|
|
self.database = database
|
|
|
|
self.connect()
|
|
|
|
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
try:
|
|
|
|
self.db = pymysql.connect(self.url, self.user, self.password, self.database, cursorclass=pymysql.cursors.DictCursor)
|
|
|
|
except Exception as err:
|
|
|
|
raise err
|
|
|
|
|
|
|
|
def getAllUser(self):
|
2019-12-19 17:26:41 +00:00
|
|
|
self.connect()
|
2019-12-19 07:12:29 +00:00
|
|
|
cursor = self.db.cursor()
|
2019-12-19 17:26:41 +00:00
|
|
|
try:
|
|
|
|
cursor.execute("select * from user")
|
|
|
|
data = cursor.fetchall()
|
|
|
|
self.db.close()
|
|
|
|
except Exception as err:
|
|
|
|
raise err
|
|
|
|
|
|
|
|
if data:
|
|
|
|
return [User(value) for value in data]
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
def getUser(self, username):
|
|
|
|
self.connect()
|
|
|
|
retVal = None
|
|
|
|
cursor = self.db.cursor()
|
2019-12-19 17:26:41 +00:00
|
|
|
try:
|
|
|
|
cursor.execute("select * from user where cn='{}'".format(username))
|
|
|
|
data = cursor.fetchone()
|
|
|
|
self.db.close()
|
|
|
|
except Exception as err:
|
|
|
|
raise err
|
2019-12-19 07:12:29 +00:00
|
|
|
if data:
|
|
|
|
retVal = User(data)
|
2019-12-19 17:26:41 +00:00
|
|
|
|
2019-12-19 07:12:29 +00:00
|
|
|
return retVal
|
|
|
|
|
|
|
|
|
|
|
|
def insertUser(self, data):
|
|
|
|
self.connect()
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
try:
|
2019-12-19 17:26:41 +00:00
|
|
|
cursor.execute("insert into user (cn, dn, firstname, lastname, gruppe) VALUES ('{}','{}','{}','{}','{}')".format(
|
2019-12-19 07:12:29 +00:00
|
|
|
data['cn'], data['dn'], data['givenName'], data['sn'], data['group']))
|
|
|
|
self.db.commit()
|
|
|
|
except Exception as err:
|
|
|
|
self.db.rollback()
|
|
|
|
self.db.close()
|
|
|
|
raise err
|
|
|
|
self.db.close()
|
|
|
|
|
|
|
|
def updateUser(self, data):
|
|
|
|
self.connect()
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
try:
|
2019-12-19 17:26:41 +00:00
|
|
|
cursor.execute("update user set dn='{}', firstname='{}', lastname='{}', gruppe='{}' where cn='{}'".format(
|
2019-12-19 07:12:29 +00:00
|
|
|
data['dn'], data['givenName'], data['sn'], data['group'], data['cn']))
|
|
|
|
self.db.commit()
|
|
|
|
except Exception as err:
|
|
|
|
self.db.rollback()
|
|
|
|
self.db.close()
|
|
|
|
raise err
|
|
|
|
self.db.close()
|
|
|
|
|
2019-12-19 17:26:41 +00:00
|
|
|
def getCreditListFromUser(self, user, **kwargs):
|
|
|
|
self.connect()
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
try:
|
|
|
|
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()
|
|
|
|
self.db.close()
|
|
|
|
except Exception as err:
|
|
|
|
self.db.close()
|
|
|
|
raise err
|
|
|
|
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):
|
|
|
|
self.connect()
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
try:
|
|
|
|
cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id))
|
|
|
|
self.db.close()
|
|
|
|
except Exception as err:
|
|
|
|
self.db.close()
|
|
|
|
raise err
|
|
|
|
|
|
|
|
def updateCreditList(self, creditlist):
|
|
|
|
self.connect()
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
try:
|
|
|
|
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)
|
2019-12-19 18:46:04 +00:00
|
|
|
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,
|
2019-12-19 17:26:41 +00:00
|
|
|
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,
|
2019-12-19 18:46:04 +00:00
|
|
|
creditlist.last_schulden, creditlist.year, creditlist.user_id)
|
|
|
|
print(sql)
|
|
|
|
cursor.execute(sql)
|
2019-12-19 17:26:41 +00:00
|
|
|
self.db.close()
|
|
|
|
except Exception as err:
|
|
|
|
self.db.rollback()
|
|
|
|
self.db.close()
|
|
|
|
raise err
|
|
|
|
|
|
|
|
|
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)
|