diff --git a/geruecht/controller/userController.py b/geruecht/controller/userController.py index 8171995..cabf70b 100644 --- a/geruecht/controller/userController.py +++ b/geruecht/controller/userController.py @@ -6,7 +6,7 @@ import calendar from geruecht.model.user import User from geruecht.exceptions import PermissionDenied from datetime import datetime, timedelta -from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExecption, LDAPExcetpion +from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExecption, LDAPExcetpion, DayLocked db = dc.DatabaseController() ldap = lc.LDAPController(ldapConfig['dn']) @@ -31,7 +31,12 @@ class UserController(metaclass=Singleton): return [db.getWorker(user, date)] return db.getWorkers(date) - def addWorker(self, username, date): + def addWorker(self, username, date, userExc=False): + if (userExc): + lockedDay = self.getLockedDay(date) + if lockedDay: + if lockedDay['locked']: + raise DayLocked("Day is locked. You can't get the Job") user = self.getUser(username) if (not db.getWorker(user, date)): db.setWorker(user, date) diff --git a/geruecht/exceptions/__init__.py b/geruecht/exceptions/__init__.py index 6aeaf8e..4ef2dbf 100644 --- a/geruecht/exceptions/__init__.py +++ b/geruecht/exceptions/__init__.py @@ -7,4 +7,6 @@ class UsernameExistLDAP(Exception): class DatabaseExecption(Exception): pass class LDAPExcetpion(Exception): + pass +class DayLocked(Exception): pass \ No newline at end of file diff --git a/geruecht/user/routes.py b/geruecht/user/routes.py index 469a66e..e98be40 100644 --- a/geruecht/user/routes.py +++ b/geruecht/user/routes.py @@ -3,8 +3,7 @@ from geruecht.decorator import login_required import geruecht.controller.userController as uc from geruecht.model import USER from datetime import datetime -import time -import traceback +from geruecht.exceptions import DayLocked user = Blueprint("user", __name__) @@ -59,9 +58,48 @@ def _getUser(**kwargs): month = data['month'] year = data['year'] date = datetime(year, month, day, 12) + lockedDay = userController.getLockedDay(date) + if not lockedDay: + lockedDay = { + 'date': { + 'year': year, + 'month': month, + 'day': day + }, + 'locked': False + } + else: + lockedDay = { + 'date': { + 'year': year, + 'month': month, + 'day': day + }, + 'locked': lockedDay['locked'] + } retVal = { 'worker': userController.getWorker(date), - 'day': userController.getLockedDay(date) + 'day': lockedDay } print(retVal) - return jsonify(retVal) \ No newline at end of file + return jsonify(retVal) + +@user.route("/user/addJob", methods=['POST']) +@login_required(groups=[USER]) +def _addUser(**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) + retVal = userController.addWorker(user.uid, date, userExc=True) + print(retVal) + return jsonify(retVal) + except DayLocked as err: + return jsonify({'error': err}), 403 + except Exception as err: + return jsonify({'error': err}), 409 \ No newline at end of file diff --git a/geruecht/vorstand/routes.py b/geruecht/vorstand/routes.py index 57680ac..40c6234 100644 --- a/geruecht/vorstand/routes.py +++ b/geruecht/vorstand/routes.py @@ -34,9 +34,28 @@ def _getUser(**kwargs): month = data['month'] year = data['year'] date = datetime(year, month, day, 12) + lockedDay = userController.getLockedDay(date) + if not lockedDay: + lockedDay = { + 'date': { + 'year': year, + 'month': month, + 'day': day + }, + 'locked': False + } + else: + lockedDay = { + 'date': { + 'year': year, + 'month': month, + 'day': day + }, + 'locked': lockedDay['locked'] + } retVal = { 'worker': userController.getWorker(date), - 'day': userController.getLockedDay(date) + 'day': lockedDay } print(retVal) return jsonify(retVal)