flaschengeist/geruecht/controller/databaseController/dbJobRequesController.py

97 lines
4.9 KiB
Python
Raw Permalink Normal View History

import traceback
from geruecht.exceptions import DatabaseExecption
class Base:
def getJobRequest(self, from_user, to_user, date, id=None):
try:
cursor = self.db.connection.cursor()
if id:
cursor.execute("select * from job_request where id={}".format(id))
else:
cursor.execute("select * from job_request where from_user={} and to_user={} and on_date='{}'".format(from_user['id'], to_user['id'], date))
retVal = cursor.fetchone()
retVal['to_user'] = self.getUserById(retVal['to_user']).toJSON()
retVal['from_user'] = self.getUserById(retVal['from_user']).toJSON()
retVal['on_date'] = {'year': retVal['on_date'].year, 'month': retVal['on_date'].month, 'day': retVal['on_date'].day}
retVal['job_kind'] = self.getJobKind(retVal['job_kind'])
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
def getJobRequestsFromUser(self, from_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_request where from_user={} and on_date>='{}'".format(from_user['id'], date))
retVal = cursor.fetchall()
for item in retVal:
item['from_user'] = from_user
item['to_user'] = self.getUserById(item['to_user']).toJSON()
item['on_date'] = {'year': item['on_date'].year, 'month': item['on_date'].month, 'day': item['on_date'].day}
item['job_kind'] = self.getJobKind(item['job_kind'])
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
def getJobRequestsToUser(self, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_request where to_user={} and on_date>='{}'".format(to_user['id'], date))
retVal = cursor.fetchall()
for item in retVal:
item['from_user'] = self.getUserById(item['from_user']).toJSON()
item['to_user'] = to_user
item['on_date'] = {'year': item['on_date'].year, 'month': item['on_date'].month, 'day': item['on_date'].day}
item['job_kind'] = self.getJobKind(item['job_kind'])
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
def setJobRequest(self, from_user, to_user, date, job_kind):
try:
cursor = self.db.connection.cursor()
cursor.execute("insert into job_request (from_user, to_user, on_date, job_kind) values ({}, {}, '{}', {})".format(from_user['id'], to_user['id'], date, job_kind['id']))
self.db.connection.commit()
return self.getJobRequest(from_user, to_user, date)
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
def updateJobRequest(self, jobrequest):
try:
cursor = self.db.connection.cursor()
cursor.execute("update job_request set watched={}, answered={}, accepted={} where id={}".format(jobrequest['watched'], jobrequest['answered'], jobrequest['accepted'], jobrequest['id']))
self.db.connection.commit()
return self.getJobRequest(None, None, None, jobrequest['id'])
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
def updateAllJobRequest(self, jobrequest):
try:
cursor = self.db.connection.cursor()
cursor.execute("update job_request set answered={} where from_user={} and on_date='{}'".format(jobrequest['answered'], jobrequest['from_user']['id'], jobrequest['on_date']))
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 deleteJobRequest(self, jobrequest):
try:
cursor = self.db.connection.cursor()
cursor.execute("delete from job_request where id={}".format(jobrequest['id']))
self.db.connection.commit()
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))