From 4e91587731dd660045c3c27c307273eaac4cf978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Mon, 24 Feb 2020 18:55:57 +0100 Subject: [PATCH] finished ##193 route for user to delete job, but only if day not locked fixed a bug, that the month start on the first wednesday in month and ended in first tuesday in nextmonth --- geruecht/controller/userController.py | 14 ++++++++++++-- geruecht/user/routes.py | 21 ++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index 0f7c293..293820e 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -23,8 +23,13 @@ class UserController(metaclass=Singleton): def getLockedDay(self, date): now = datetime.now() daysInMonth = calendar.monthrange(date.year, date.month)[1] + startMonth = 1 + for i in range(1, 8): + if datetime(date.year, date.month, i).weekday() == 2: + startMonth = i + break if date.year <= now.year and date.month <= now.month: - for i in range(1, daysInMonth + 1): + for i in range(startMonth, daysInMonth + 1): self.setLockedDay(datetime(date.year, date.month, i), True) for i in range(1, 8): nextMonth = datetime(date.year, date.month + 1, i) @@ -50,7 +55,12 @@ class UserController(metaclass=Singleton): db.setWorker(user, date) return self.getWorker(date, username=username) - def deleteWorker(self, username, date): + def deleteWorker(self, username, date, userExc=False): + if userExc: + lockedDay = self.getLockedDay(date) + if lockedDay: + if lockedDay['locked']: + raise DayLocked("Day is locked. You can't delete the Job") user = self.getUser(username) db.deleteWorker(user, date) diff --git a/geruecht/user/routes.py b/geruecht/user/routes.py index e98be40..4f4ed33 100644 --- a/geruecht/user/routes.py +++ b/geruecht/user/routes.py @@ -102,4 +102,23 @@ def _addUser(**kwargs): except DayLocked as err: return jsonify({'error': err}), 403 except Exception as err: - return jsonify({'error': err}), 409 \ No newline at end of file + return jsonify({'error': err}), 409 + +@user.route("/user/deleteJob", methods=['POST']) +@login_required(groups=[USER]) +def _deletJob(**kwargs): + try: + if 'accToken' in kwargs: + accToken = kwargs['accToken'] + user = accToken.user + data = request.get_json() + day = data['day'] + month = data['month'] + year = data['year'] + date = datetime(year,month,day,12) + userController.deleteWorker(user.uid, date, True) + return jsonify({"ok": "ok"}) + except DayLocked as err: + return jsonify({"error": err}), 403 + except Exception as err: + return jsonify({"error": err}), 409 \ No newline at end of file