flaschengeist/geruecht/vorstand/routes.py

169 lines
5.1 KiB
Python

from flask import Blueprint, request, jsonify
from datetime import datetime
import geruecht.controller.userController as uc
import geruecht.controller.ldapController as lc
from geruecht.decorator import login_required
from geruecht.model import MONEY, GASTRO, VORSTAND
import time
vorstand = Blueprint("vorstand", __name__)
userController = uc.UserController()
ldap= lc.LDAPController()
@vorstand.route('/um/setStatus', methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _setStatus(**kwargs):
try:
data = request.get_json()
name = data['name']
retVal = userController.setStatus(name)
return jsonify(retVal)
except Exception as err:
return jsonify({"error": str(err)}), 500
@vorstand.route('/um/updateStatus', methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _updateStatus(**kwargs):
try:
data = request.get_json()
retVal = userController.updateStatus(data)
return jsonify(retVal)
except Exception as err:
return jsonify({"error": str(err)}), 500
@vorstand.route('/um/deleteStatus', methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _deleteStatus(**kwargs):
try:
data = request.get_json()
userController.deleteStatus(data)
return jsonify({"ok": "ok"})
except Exception as err:
return jsonify({"error": str(err)}), 409
@vorstand.route('/um/updateStatusUser', methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _updateStatusUser(**kwargs):
try:
data = request.get_json()
username = data['username']
status = data['status']
retVal = userController.updateStatusOfUser(username, status).toJSON()
return jsonify(retVal)
except Exception as err:
return jsonify({"error": str(err)}), 500
@vorstand.route('/um/updateVoting', methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _updateVoting(**kwargs):
try:
data = request.get_json()
username = data['username']
voting = data['voting']
retVal = userController.updateVotingOfUser(username, voting).toJSON()
return jsonify(retVal)
except Exception as err:
return jsonify({"error": str(err)}), 500
@vorstand.route("/sm/addUser", methods=['POST', 'GET'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _addUser(**kwargs):
if request.method == 'GET':
return "<h1>HEllo World</h1>"
data = request.get_json()
user = data['user']
day = data['day']
month = data['month']
year = data['year']
date = datetime(year,month,day,12)
retVal = userController.addWorker(user['username'], date)
print(retVal)
return jsonify(retVal)
@vorstand.route("/sm/getUser", methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _getUser(**kwargs):
data = request.get_json()
day = data['day']
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': lockedDay
}
print(retVal)
return jsonify(retVal)
@vorstand.route("/sm/deleteUser", methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _deletUser(**kwargs):
data = request.get_json()
user = data['user']
day = data['day']
month = data['month']
year = data['year']
date = datetime(year, month, day, 12)
userController.deleteWorker(user['username'], date)
return jsonify({"ok": "ok"})
@vorstand.route("/sm/lockDay", methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _lockDay(**kwargs):
try:
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
locked = data['locked']
date = datetime(year, month, day, 12)
lockedDay = userController.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']
}
print(retVal)
return jsonify(retVal)
except Exception as err:
return jsonify({'error': err}), 409
@vorstand.route("/sm/searchWithExtern", methods=['GET'])
@login_required(groups=[VORSTAND])
def _search(**kwargs):
retVal = ldap.getAllUser()
return jsonify(retVal)