flaschengeist/geruecht/controller/databaseController/__init__.py

71 lines
2.8 KiB
Python

from ..mainController import Singleton
from geruecht import db
from ..databaseController import dbUserController, dbCreditListController, dbJobKindController, dbPricelistController, dbWorkerController, dbWorkgroupController, dbJobInviteController, dbJobRequesController, dbAccessTokenController
from geruecht.exceptions import DatabaseExecption
import traceback
from MySQLdb._exceptions import IntegrityError
class DatabaseController(dbUserController.Base,
dbCreditListController.Base,
dbWorkerController.Base,
dbWorkgroupController.Base,
dbPricelistController.Base,
dbJobKindController.Base,
dbJobInviteController.Base,
dbJobRequesController.Base,
dbAccessTokenController.Base,
metaclass=Singleton):
'''
DatabaesController
Connect to the Database and execute sql-executions
'''
def __init__(self):
self.db = db
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 DatabaseExecption("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__':
db = DatabaseController()
user = db.getUser('jhille')
db.getCreditListFromUser(user, year=2018)