flaschengeist/geruecht/controller/databaseController/dbJobTransactController.py

110 lines
5.5 KiB
Python

import traceback
from MySQLdb._exceptions import IntegrityError
from geruecht.exceptions import DatabaseExecption
class Base:
def getTransactJob(self, from_user, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where from_user_id={} and to_user_id={} and jobdate='{}'".format(from_user.id, to_user.id, date))
data = cursor.fetchone()
if data:
return {"from_user": from_user, "to_user": to_user, "date": data['jobdate'], "answerd": data['answerd'], "accepted": data['accepted']}
return None
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Database: {}".format(err))
def getAllTransactJobFromUser(self, from_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where from_user_id={}".format(from_user.id))
data = cursor.fetchall()
retVal = []
for transact in data:
if date <= transact['jobdate']:
retVal.append({"from_user": from_user, "to_user": self.getUserById(transact['to_user_id']), "date": transact['jobdate'], "accepted": transact['accepted'], "answerd": transact['answerd']})
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def getAllTransactJobToUser(self, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where to_user_id={}".format(to_user.id))
data = cursor.fetchall()
retVal = []
for transact in data:
if date <= transact['jobdate']:
retVal.append({"to_user": to_user, "from_user": self.getUserById(transact['from_user_id']), "date": transact['jobdate'], "accepted": transact['accepted'], "answerd": transact['answerd']})
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def getTransactJobToUser(self, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where to_user_id={} and jobdate='{}'".format(to_user.id, date))
data = cursor.fetchone()
if data:
return {"from_user": self.getUserById(data['from_user_id']), "to_user": to_user, "date": data['jobdate'], "accepted": data['accepted'], "answerd": data['answerd']}
else:
return None
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def updateTransactJob(self, from_user, to_user, date, accepted):
try:
cursor = self.db.connection.cursor()
cursor.execute("update job_transact set accepted={}, answerd=true where to_user_id={} and jobdate='{}'".format(accepted, to_user.id, date))
self.db.connection.commit()
return self.getTransactJob(from_user, to_user, date)
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def getTransactJobFromUser(self, user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where from_user_id={} and jobdate='{}'".format(user.id, date))
data = cursor.fetchall()
return [{"from_user": user, "to_user": self.getUserById(transact['to_user_id']), "date": transact['jobdate'], "accepted": transact['accepted'], "answerd": transact['answerd']} for transact in data]
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def deleteTransactJob(self, from_user, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("delete from job_transact where from_user_id={} and to_user_id={} and jobdate='{}'".format(from_user.id, to_user.id, date))
self.db.connection.commit()
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went wrong with Database: {}".format(err))
def setTransactJob(self, from_user, to_user, date):
try:
exists = self.getTransactJob(from_user, to_user, date)
if exists:
raise IntegrityError("job_transact already exists")
cursor = self.db.connection.cursor()
cursor.execute("insert into job_transact (jobdate, from_user_id, to_user_id) VALUES ('{}', {}, {})".format(date, from_user.id, to_user.id))
self.db.connection.commit()
return self.getTransactJob(from_user, to_user, date)
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))