flaschengeist/geruecht/user/routes.py

384 lines
14 KiB
Python

from flask import Blueprint, request, jsonify
from geruecht.decorator import login_required
import geruecht.controller.mainController as mc
from geruecht.model import USER
from datetime import datetime, time
from geruecht.exceptions import DayLocked
from geruecht.logger import getDebugLogger, getCreditLogger, getJobsLogger
user = Blueprint("user", __name__)
mainController = mc.MainController()
debug = getDebugLogger()
creditL = getCreditLogger()
jobL = getJobsLogger()
@user.route("/user/main")
@login_required(groups=[USER])
def _main(**kwargs):
debug.info("/user/main")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
accToken.user = mainController.getUser(accToken.user.uid)
retVal = accToken.user.toJSON()
retVal['creditList'] = {credit.year: credit.toJSON()
for credit in accToken.user.geruechte}
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception:
debug.debug("exception", exc_info=True)
return jsonify("error", "something went wrong"), 500
@user.route("/user/addAmount", methods=['POST'])
@login_required(groups=[USER])
def _addAmount(**kwargs):
debug.info("/user/addAmount")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
data = request.get_json()
amount = int(data['amount'])
date = datetime.now()
mainController.addAmount(
accToken.user.uid, amount, year=date.year, month=date.month)
accToken.user = mainController.getUser(accToken.user.uid)
retVal = accToken.user.toJSON()
retVal['creditList'] = {credit.year: credit.toJSON()
for credit in accToken.user.geruechte}
debug.debug("return {{ {} }}".format(retVal))
creditL.info("{} {} {} fügt sich selbst {} € Schulden hinzu".format(
date, accToken.user.firstname, accToken.user.lastname, amount/100))
return jsonify(retVal)
except Exception:
debug.debug("exception", exc_info=True)
return jsonify({"error": "something went wrong"}), 500
@user.route("/user/saveConfig", methods=['POST'])
@login_required(groups=[USER])
def _saveConfig(**kwargs):
debug.info("/user/saveConfig")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
data = request.get_json()
accToken.user = mainController.modifyUser(
accToken.user, accToken.ldap_conn, data)
retVal = accToken.user.toJSON()
retVal['creditList'] = {credit.year: credit.toJSON()
for credit in accToken.user.geruechte}
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409
@user.route("/user/jobs", methods=['POST'])
@login_required(groups=[USER])
def _getUsers(**kwrags):
debug.info("/user/jobs")
try:
data = request.get_json()
from_date = data['from_date']
to_date = data['to_date']
from_date = datetime(
from_date['year'], from_date['month'], from_date['day'])
to_date = datetime(to_date['year'], to_date['month'], to_date['day'])
lockedDays = mainController.getLockedDays(from_date, to_date)
retVal = []
for lockedDay in lockedDays:
day = datetime.combine(lockedDay['daydate'], time(12))
retDay = {
"worker": mainController.getWorker(day),
"day": {
"date": {
"year": day.year,
"month": day.month,
"day": day.day
},
"locked": lockedDay['locked']
},
"jobkinddate": mainController.getJobKindDates(day.date())
}
retVal.append(retDay)
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/job", methods=['POST'])
@login_required(groups=[USER])
def _getUser(**kwargs):
debug.info("/user/job")
try:
data = request.get_json()
day = data['day']
month = data['month']
year = data['year']
date = datetime(year, month, day, 12)
lockedDay = mainController.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': mainController.getWorker(date),
'day': lockedDay
}
debug.debug("retrun {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@user.route("/user/addJob", methods=['POST'])
@login_required(groups=[USER])
def _addUser(**kwargs):
debug.info("/user/addJob")
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 = mainController.addWorker(user.uid, date, userExc=True)
debug.debug("return {{ {} }}".format(retVal))
jobL.info("Mitglied {} {} schreib sich am {} zum Dienst ein.".format(
user.firstname, user.lastname, date.date()))
return jsonify(retVal)
except DayLocked as err:
debug.debug("exception", exc_info=True)
return jsonify({'error': str(err)}), 403
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({'error': str(err)}), 409
@user.route("/user/deleteJob", methods=['POST'])
@login_required(groups=[USER])
def _deletJob(**kwargs):
debug.info("/user/deleteJob")
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)
mainController.deleteWorker(user.uid, date, True)
debug.debug("return ok")
jobL.info("Mitglied {} {} entfernt sich am {} aus dem Dienst".format(
user.firstname, user.lastname, date.date()))
return jsonify({"ok": "ok"})
except DayLocked as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 403
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409
@user.route("/user/transactJob", methods=['POST'])
@login_required(groups=[USER])
def _transactJob(**kwargs):
debug.info("/user/transactJob")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
username = data['user']
date = datetime(year, month, day, 12)
to_user = mainController.getUser(username)
retVal = mainController.setTransactJob(user, to_user, date)
from_userl = retVal['from_user']
to_userl = retVal['to_user']
retVal['from_user'] = retVal['from_user'].toJSON()
retVal['to_user'] = retVal['to_user'].toJSON()
retVal['date'] = {'year': year, 'month': month, 'day': day}
debug.debug("return {{ {} }}".format(retVal))
jobL.info("Mitglied {} {} sendet Dienstanfrage an Mitglied {} {} am {}".format(
from_userl.firstname, from_userl.lastname, to_userl.firstname, to_userl.lastname, date.date()))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409
@user.route("/user/answerTransactJob", methods=['POST'])
@login_required(groups=[USER])
def _answer(**kwargs):
debug.info("/user/answerTransactJob")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
answer = data['answer']
username = data['username']
date = datetime(year, month, day, 12)
from_user = mainController.getUser(username)
retVal = mainController.answerdTransactJob(
from_user, user, date, answer)
from_userl = retVal['from_user']
to_userl = retVal['to_user']
retVal['from_user'] = retVal['from_user'].toJSON()
retVal['to_user'] = retVal['to_user'].toJSON()
retVal['date'] = {'year': year, 'month': month, 'day': day}
debug.debug("return {{ {} }}".format(retVal))
jobL.info("Mitglied {} {} beantwortet Dienstanfrage von {} {} am {} mit {}".format(to_userl.firstname,
to_userl.lastname, from_userl.firstname, from_userl.lastname, date.date(), 'JA' if answer else 'NEIN'))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409
@user.route("/user/jobRequests", methods=['POST'])
@login_required(groups=[USER])
def _requests(**kwargs):
debug.info("/user/jobRequests")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
date = datetime(year, month, day, 12)
retVal = mainController.getAllTransactJobToUser(user, date)
for data in retVal:
data['from_user'] = data['from_user'].toJSON()
data['to_user'] = data['to_user'].toJSON()
data_date = data['date']
data['date'] = {'year': data_date.year,
'month': data_date.month, 'day': data_date.day}
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409
@user.route("/user/getTransactJobs", methods=['POST'])
@login_required(groups=[USER])
def _getTransactJobs(**kwargs):
debug.info("/user/getTransactJobs")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
date = datetime(year, month, day, 12)
retVal = mainController.getAllTransactJobFromUser(user, date)
for data in retVal:
data['from_user'] = data['from_user'].toJSON()
data['to_user'] = data['to_user'].toJSON()
data_date = data['date']
data['date'] = {'year': data_date.year,
'month': data_date.month, 'day': data_date.day}
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409
@user.route("/user/deleteTransactJob", methods=['POST'])
@login_required(groups=[USER])
def _deleteTransactJob(**kwargs):
debug.info("/user/deleteTransactJob")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
from_user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
username = data['username']
date = datetime(year, month, day, 12)
to_user = mainController.getUser(username)
mainController.deleteTransactJob(from_user, to_user, date)
debug.debug("return ok")
jobL.info("Mitglied {} {} entfernt Dienstanfrage an {} {} am {}".format(
from_user.firstname, from_user.lastname, to_user.firstname, to_user.lastname, date.date()))
return jsonify({"ok": "ok"})
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409
@user.route("/user/storno", methods=['POST'])
@login_required(groups=[USER])
def _storno(**kwargs):
""" Function for Baruser to storno amount
This function added to the user with the posted userID the posted amount.
Returns:
JSON-File with userID and the amount
or ERROR 401 Permission Denied
"""
debug.info("/user/storno")
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
amount = int(data['amount'])
date = datetime.now()
mainController.addCredit(
user.uid, amount, year=date.year, month=date.month)
accToken.user = mainController.getUser(accToken.user.uid)
retVal = accToken.user.toJSON()
retVal['creditList'] = {credit.year: credit.toJSON()
for credit in accToken.user.geruechte}
debug.debug("return {{ {} }}".format(retVal))
creditL.info("{} {} {} storniert {}".format(
date, user.firstname, user.lastname, amount/100))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409