From b0f09969a5322381b3743965f1761ef348f866a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Tue, 2 Jun 2020 23:24:17 +0200 Subject: [PATCH] =?UTF-8?q?user=20kann=20jobrequest=20updaten,=20erstellen?= =?UTF-8?q?=20oder=20l=C3=B6schen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/databaseController/__init__.py | 3 +- .../dbJobRequesController.py | 97 +++++++++++++++++++ .../databaseController/dbWorkerController.py | 10 ++ .../controller/mainController/__init__.py | 3 +- .../mainJobRequestController.py | 41 ++++++++ .../mainController/mainWorkerController.py | 4 + geruecht/user/routes.py | 57 +++++++++++ 7 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 geruecht/controller/databaseController/dbJobRequesController.py create mode 100644 geruecht/controller/mainController/mainJobRequestController.py diff --git a/geruecht/controller/databaseController/__init__.py b/geruecht/controller/databaseController/__init__.py index 76a47c6..9ab55bb 100644 --- a/geruecht/controller/databaseController/__init__.py +++ b/geruecht/controller/databaseController/__init__.py @@ -1,6 +1,6 @@ from ..mainController import Singleton from geruecht import db -from ..databaseController import dbUserController, dbCreditListController, dbJobKindController, dbPricelistController, dbWorkerController, dbWorkgroupController, dbJobInviteController +from ..databaseController import dbUserController, dbCreditListController, dbJobKindController, dbPricelistController, dbWorkerController, dbWorkgroupController, dbJobInviteController, dbJobRequesController from geruecht.exceptions import DatabaseExecption import traceback from MySQLdb._exceptions import IntegrityError @@ -12,6 +12,7 @@ class DatabaseController(dbUserController.Base, dbPricelistController.Base, dbJobKindController.Base, dbJobInviteController.Base, + dbJobRequesController.Base, metaclass=Singleton): ''' DatabaesController diff --git a/geruecht/controller/databaseController/dbJobRequesController.py b/geruecht/controller/databaseController/dbJobRequesController.py new file mode 100644 index 0000000..b65e323 --- /dev/null +++ b/geruecht/controller/databaseController/dbJobRequesController.py @@ -0,0 +1,97 @@ +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={} where id={}".format(jobrequest['watched'], jobrequest['answered'], 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={}, accepted={} where from_user={} and on_date='{}'".format(jobrequest['answered'], jobrequest['accepted'], 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)) \ No newline at end of file diff --git a/geruecht/controller/databaseController/dbWorkerController.py b/geruecht/controller/databaseController/dbWorkerController.py index 134625d..7a2e917 100644 --- a/geruecht/controller/databaseController/dbWorkerController.py +++ b/geruecht/controller/databaseController/dbWorkerController.py @@ -46,6 +46,16 @@ class Base: self.db.connection.rollback() raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) + def changeWorker(self, from_user, to_user, date): + try: + cursor = self.db.connection.cursor() + cursor.execute("update bardienste set user_id={} where user_id={} and startdatetime='{}'".format(to_user['id'], from_user['id'], 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 deleteAllWorkerWithJobKind(self, date, job_kind): try: cursor = self.db.connection.cursor() diff --git a/geruecht/controller/mainController/__init__.py b/geruecht/controller/mainController/__init__.py index 3d4c969..41f2a7f 100644 --- a/geruecht/controller/mainController/__init__.py +++ b/geruecht/controller/mainController/__init__.py @@ -5,7 +5,7 @@ import geruecht.controller.emailController as ec from geruecht.model.user import User from datetime import datetime, timedelta from geruecht.logger import getDebugLogger -from ..mainController import mainJobKindController, mainCreditListController, mainPricelistController, mainUserController, mainWorkerController, mainWorkgroupController, mainJobInviteController +from ..mainController import mainJobKindController, mainCreditListController, mainPricelistController, mainUserController, mainWorkerController, mainWorkgroupController, mainJobInviteController, mainJobRequestController db = dc.DatabaseController() ldap = lc.LDAPController() @@ -21,6 +21,7 @@ class MainController(mainJobKindController.Base, mainWorkerController.Base, mainWorkgroupController.Base, mainJobInviteController.Base, + mainJobRequestController.Base, metaclass=Singleton): def __init__(self): diff --git a/geruecht/controller/mainController/mainJobRequestController.py b/geruecht/controller/mainController/mainJobRequestController.py new file mode 100644 index 0000000..8c7bc1f --- /dev/null +++ b/geruecht/controller/mainController/mainJobRequestController.py @@ -0,0 +1,41 @@ +from datetime import date, time, datetime + +import geruecht.controller.databaseController as dc +from geruecht import getDebugLogger + +db = dc.DatabaseController() +debug = getDebugLogger() + +class Base: + def getJobRequests(self, from_user, to_user, date): + debug.info("get JobRequests from_user {{ {} }} to_user {{ {} }} on date {{ {} }}".format(from_user, to_user, date)) + if from_user is None: + retVal = db.getJobRequestsToUser(to_user, date) + elif to_user is None: + retVal = db.getJobRequestsFromUser(from_user, date) + else: + raise Exception("from_user {{ {} }} and to_user {{ {} }} are None".format(from_user, to_user)) + return retVal + + def setJobRequests(self, data): + debug.info("set new JobRequests data {{ {} }}".format(data)) + retVal = [] + for jobRequest in data: + from_user = jobRequest['from_user'] + to_user = jobRequest['to_user'] + on_date = date(jobRequest['date']['year'], jobRequest['date']['month'], jobRequest['date']['day']) + debug.info("set new JobRequest from_user {{ {} }}, to_user {{ {} }}, on_date {{ {} }}") + retVal.append(db.setJobRequest(from_user, to_user, on_date)) + debug.debug("seted JobRequests are {{ {} }}".format(retVal)) + return retVal + + def updateJobRequests(self, data): + debug.info("update JobRequest data {{ {} }}".format(data)) + if data['accepted']: + self.changeWorker(data['from_user'], data['to_user'], datetime.combine(data['on_date'], time(12))) + db.updateAllJobRequest(data) + return db.updateJobRequest(data) + + def deleteJobRequest(self, jobRequest): + debug.info("delete JobRequest {{ {} }}".format(jobRequest)) + db.deleteJobRequest(jobRequest) \ No newline at end of file diff --git a/geruecht/controller/mainController/mainWorkerController.py b/geruecht/controller/mainController/mainWorkerController.py index 95bd2a0..4ae0d16 100644 --- a/geruecht/controller/mainController/mainWorkerController.py +++ b/geruecht/controller/mainController/mainWorkerController.py @@ -39,6 +39,10 @@ class Base: debug.debug("worker on date is {{ {} }}".format(retVal)) return retVal + def changeWorker(self, from_user, to_user, date): + debug.info("change worker from {{ {} }} to {{ {} }} on {{ {} }}".format(from_user, to_user, date)) + db.changeWorker(from_user, to_user, date) + def deleteWorker(self, username, date, userExc=False): debug.info( "delete worker {{ {} }} on date {{ {} }}".format(username, date)) diff --git a/geruecht/user/routes.py b/geruecht/user/routes.py index e9b49b0..b48e5b3 100644 --- a/geruecht/user/routes.py +++ b/geruecht/user/routes.py @@ -326,6 +326,63 @@ def _deleteJobInvite(**kwargs): retVal = mainController.getJobInvites(data['from_user'], None, datetime.now().date()) debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) + except Exception as err: + debug.debug("exception", exc_info=True) + return jsonify({"error": str(err)}), 500 + + +@user.route("/user/getJobRequests", methods=['POST']) +@login_required(groups=[USER]) +def _getJobRequests(**kwargs): + try: + debug.info("/user/getJobRequests") + from_user = None + to_user = None + on_date = None + + data = request.get_json() + + if 'from_user' in data: + from_user = data['from_user'] + if 'to_user' in data: + to_user = data['to_user'] + on_date = date(data['date']['year'], data['date']['month'], data['date']['day']) + retVal = mainController.getJobRequests(from_user, to_user, on_date) + debug.debug("return {{ {} }}".format(retVal)) + return jsonify(retVal) + except Exception as err: + debug.debug("exception", exc_info=True) + return jsonify({"error": str(err)}), 500 + +@user.route("/user/JobRequests", methods=['PUT', 'POST']) +@login_required(groups=[USER]) +def _JobRequests(**kwargs): + try: + debug.info("/user/JobRequests") + data = request.get_json() + data['on_date'] = date(data['on_date']['year'], data['on_date']['month'], data['on_date']['day']) + if request.method == 'PUT': + mainController.setJobRequests(data) + retVal = mainController.getJobRequests(kwargs['accToken'].user.toJSON(), None, datetime.now().date()) + debug.debug("return {{ {} }}".format(retVal)) + if request.method == 'POST': + retVal = mainController.updateJobRequests(data) + + return jsonify(retVal) + except Exception as err: + debug.debug("exception", exc_info=True) + return jsonify({"error": str(err)}), 500 + +@user.route("/user/deleteJobRequest", methods=['POST']) +@login_required(groups=[USER]) +def _deleteJobRequest(**kwargs): + try: + debug.info("/user/deleteJobRequest") + data = request.get_json() + mainController.deleteJobRequest(data) + retVal = mainController.getJobRequests(data['from_user'], None, datetime.now().date()) + debug.debug("return {{ {} }}".format(retVal)) + return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 \ No newline at end of file