from flask import Blueprint, request, jsonify from datetime import datetime, time, date import geruecht.controller.mainController as mc import geruecht.controller.ldapController as lc from geruecht.decorator import login_required from geruecht.model import MONEY, GASTRO, VORSTAND from geruecht.logger import getDebugLogger, getJobsLogger debug = getDebugLogger() jobL = getJobsLogger() vorstand = Blueprint("vorstand", __name__) mainController = mc.MainController() ldap = lc.LDAPController() @vorstand.route('/um/setStatus', methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _setStatus(**kwargs): debug.info("/um/setStatus") try: data = request.get_json() name = data['name'] retVal = mainController.setStatus(name) debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route('/um/updateStatus', methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _updateStatus(**kwargs): debug.info("/um/updateStatus") try: data = request.get_json() retVal = mainController.updateStatus(data) debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route('/um/deleteStatus', methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _deleteStatus(**kwargs): debug.info("/um/deleteStatus") try: data = request.get_json() mainController.deleteStatus(data) debug.debug("return ok") return jsonify({"ok": "ok"}) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 409 @vorstand.route('/um/updateStatusUser', methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _updateStatusUser(**kwargs): debug.info("/um/updateStatusUser") try: data = request.get_json() username = data['username'] status = data['status'] retVal = mainController.updateStatusOfUser(username, status).toJSON() debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route('/um/updateVoting', methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _updateVoting(**kwargs): debug.info("/um/updateVoting") try: data = request.get_json() username = data['username'] voting = data['voting'] retVal = mainController.updateVotingOfUser(username, voting).toJSON() debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route('/um/updateWorkgroups', methods=['POST']) @login_required(groups=[VORSTAND]) def _updateWorkgroups(**kwargs): debug.info("/um/updateWorkgroups") try: data = request.get_json() retVal = mainController.updateWorkgroupsOfUser({"id": data['id']}, data['workgroups']) debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal), 200 except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/addUser", methods=['POST', 'GET']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _addUser(**kwargs): debug.info("/sm/addUser") try: data = request.get_json() user = data['user'] day = data['day'] month = data['month'] year = data['year'] date = datetime(year, month, day, 12) job_kind = None if 'job_kind' in data: job_kind = data['job_kind'] retVal = mainController.addWorker(user['username'], date, job_kind=job_kind) debug.debug("retrun {{ {} }}".format(retVal)) userl = mainController.getUser(user['username']) jobL.info("Vorstand {} {} schreibt Mitglied {} {} am {} zum Dienst ein".format( kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, userl.firstname, userl.lastname, date.date())) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/getUser", methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _getUser(**kwargs): debug.info("/sm/getUser") try: data = request.get_json() day = data['day'] month = data['month'] year = data['year'] date = datetime(year, month, day, 12) lockedDay = mainController.getLockedDay(date) lockedDay = { 'date': { 'year': year, 'month': month, 'day': day }, 'locked': lockedDay['locked'] } retVal = { 'worker': mainController.getWorker(date), 'day': lockedDay } debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/deleteUser", methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _deletUser(**kwargs): debug.info("/sm/deletUser") try: data = request.get_json() user = data['user'] day = data['day'] month = data['month'] year = data['year'] date = datetime(year, month, day, 12) mainController.deleteWorker(user['username'], date) debug.debug("return ok") user = mainController.getUser(user['username']) jobL.info("Vorstand {} {} entfernt Mitglied {} {} am {} vom Dienst".format( kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, user.firstname, user.lastname, date.date())) return jsonify({"ok": "ok"}) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/wgm/getAllWorkgroups", methods=['GET']) @login_required(bar=True) def _getAllWorkgroups(**kwargs): try: debug.info("get all workgroups") retVal = mainController.getAllWorkgroups() debug.info("return all workgroups {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.warning("exception in get all workgroups.", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/wgm/getWorkgroup", methods=['POST']) @login_required(bar=True) def _getWorkgroup(**kwargs): try: debug.info("get workgroup") data = request.get_json() name = data['name'] debug.info("get workgroup {{ {} }}".format(name)) retVal = mainController.getWorkgroups(name) debug.info( "return workgroup {{ {} }} : {{ {} }}".format(name, retVal)) return jsonify(retVal) except Exception as err: debug.warning("exception in get workgroup.", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/wgm/workgroup", methods=['POST', 'PUT', 'DELETE']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _workgroup(**kwargs): debug.info("/wgm/workgroup") try: data = request.get_json() if request.method == 'PUT': name = data['name'] boss = None if 'boss' in data: boss = data['boss'] retVal = mainController.setWorkgroup(name, boss) debug.debug("return {{ {} }}".format(retVal)) if request.method == 'POST': retVal = mainController.updateWorkgroup(data) debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/wgm/deleteWorkgroup", methods=['POST']) @login_required(groups=[VORSTAND]) def _deleteWorkgroup(**kwargs): try: data = request.get_json() debug.info("/wgm/deleteWorkgroup") mainController.deleteWorkgroup(data) retVal = {"ok": "ok"} debug.debug("return ok") return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/getAllJobKinds", methods=['GET']) @login_required(bar=True) def _getAllJobKinds(**kwargs): try: debug.info("get all jobkinds") retVal = mainController.getAllJobKinds() debug.info("return all jobkinds {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.warning("exception in get all workgroups.", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/getJobKind", methods=['POST']) @login_required(bar=True) def _getJobKinds(**kwargs): try: debug.info("get jobkind") data = request.get_json() name = data['name'] debug.info("get jobkind {{ {} }}".format(name)) retVal = mainController.getJobKind(name) debug.info( "return workgroup {{ {} }} : {{ {} }}".format(name, retVal)) return jsonify(retVal) except Exception as err: debug.warning("exception in get workgroup.", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/JobKind", methods=['POST', 'PUT', 'DELETE']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _JobKinds(**kwargs): debug.info("/sm/JobKind") try: data = request.get_json() if request.method == 'PUT': name = data['name'] workgroup = None if 'workgroup' in data: workgroup = data['workgroup'] retVal = mainController.setJobKind(name, workgroup) debug.debug("return {{ {} }}".format(retVal)) if request.method == 'POST': retVal = mainController.updateJobKind(data) debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/deleteJobKind", methods=['POST']) @login_required(groups=[VORSTAND]) def _deleteJobKind(**kwargs): try: data = request.get_json() debug.info("/sm/deleteJobKind") mainController.deleteJobKind(data) retVal = {"ok": "ok"} debug.debug("return ok") return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/jk/getJobKindDates", methods=['POST']) @login_required() def _getJobKindDates(**kwargs): try: debug.info("/jk/getJobKindDates") data = request.get_json() datum = date(data['year'], data['month'], data['day']) retVal = mainController.getJobKindDates(datum) debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/jk/JobKindDate", methods=['POST']) @login_required(groups=[VORSTAND]) def _jobKindDates(**kwargs): try: debug.info("/jk/JobKindDate") data = request.get_json() retVal = mainController.controllJobKindDates(data) debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500 @vorstand.route("/sm/lockDay", methods=['POST']) @login_required(groups=[MONEY, GASTRO, VORSTAND]) def _lockDay(**kwargs): debug.info("/sm/lockDay") try: data = request.get_json() year = data['year'] month = data['month'] day = data['day'] locked = data['locked'] date = datetime(year, month, day, 12) lockedDay = mainController.setLockedDay(date, locked, True) if not lockedDay: retVal = { 'date': { 'year': year, 'month': month, 'day': day }, 'locked': False } else: retVal = { 'date': { 'year': year, 'month': month, 'day': day }, 'locked': lockedDay['locked'] } debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({'error': err}), 409 @vorstand.route("/sm/searchWithExtern", methods=['GET']) @login_required(groups=[VORSTAND]) def _search(**kwargs): debug.info("/sm/searchWithExtern") try: retVal = ldap.getAllUser() debug.debug("return {{ {} }}".format(retVal)) return jsonify(retVal) except Exception as err: debug.debug("exception", exc_info=True) return jsonify({"error": str(err)}), 500