add route for user for ##222

This commit is contained in:
Tim Gröger 2020-03-13 19:06:24 +02:00
parent b4d648530e
commit 2f756983c2
1 changed files with 84 additions and 20 deletions

View File

@ -2,7 +2,7 @@ from flask import Blueprint, request, jsonify
from geruecht.decorator import login_required from geruecht.decorator import login_required
import geruecht.controller.userController as uc import geruecht.controller.userController as uc
from geruecht.model import USER from geruecht.model import USER
from datetime import datetime from datetime import datetime, time
from geruecht.exceptions import DayLocked from geruecht.exceptions import DayLocked
from geruecht.logger import getDebugLogger, getCreditLogger, getJobsLogger from geruecht.logger import getDebugLogger, getCreditLogger, getJobsLogger
@ -14,6 +14,7 @@ debug = getDebugLogger()
creditL = getCreditLogger() creditL = getCreditLogger()
jobL = getJobsLogger() jobL = getJobsLogger()
@user.route("/user/main") @user.route("/user/main")
@login_required(groups=[USER]) @login_required(groups=[USER])
def _main(**kwargs): def _main(**kwargs):
@ -23,13 +24,15 @@ def _main(**kwargs):
accToken = kwargs['accToken'] accToken = kwargs['accToken']
accToken.user = userController.getUser(accToken.user.uid) accToken.user = userController.getUser(accToken.user.uid)
retVal = accToken.user.toJSON() retVal = accToken.user.toJSON()
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte} retVal['creditList'] = {credit.year: credit.toJSON()
for credit in accToken.user.geruechte}
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal) return jsonify(retVal)
except Exception: except Exception:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify("error", "something went wrong"), 500 return jsonify("error", "something went wrong"), 500
@user.route("/user/addAmount", methods=['POST']) @user.route("/user/addAmount", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _addAmount(**kwargs): def _addAmount(**kwargs):
@ -40,17 +43,21 @@ def _addAmount(**kwargs):
data = request.get_json() data = request.get_json()
amount = int(data['amount']) amount = int(data['amount'])
date = datetime.now() date = datetime.now()
userController.addAmount(accToken.user.uid, amount, year=date.year, month=date.month) userController.addAmount(
accToken.user.uid, amount, year=date.year, month=date.month)
accToken.user = userController.getUser(accToken.user.uid) accToken.user = userController.getUser(accToken.user.uid)
retVal = accToken.user.toJSON() retVal = accToken.user.toJSON()
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte} retVal['creditList'] = {credit.year: credit.toJSON()
for credit in accToken.user.geruechte}
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
creditL.info("{} {} {} fügt sich selbst {} € Schulden hinzu".format(date, accToken.user.firstname, accToken.user.lastname, amount/100)) creditL.info("{} {} {} fügt sich selbst {} € Schulden hinzu".format(
date, accToken.user.firstname, accToken.user.lastname, amount/100))
return jsonify(retVal) return jsonify(retVal)
except Exception: except Exception:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": "something went wrong"}), 500 return jsonify({"error": "something went wrong"}), 500
@user.route("/user/saveConfig", methods=['POST']) @user.route("/user/saveConfig", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _saveConfig(**kwargs): def _saveConfig(**kwargs):
@ -59,15 +66,53 @@ def _saveConfig(**kwargs):
if 'accToken' in kwargs: if 'accToken' in kwargs:
accToken = kwargs['accToken'] accToken = kwargs['accToken']
data = request.get_json() data = request.get_json()
accToken.user = userController.modifyUser(accToken.user, accToken.ldap_conn, data) accToken.user = userController.modifyUser(
accToken.user, accToken.ldap_conn, data)
retVal = accToken.user.toJSON() retVal = accToken.user.toJSON()
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte} retVal['creditList'] = {credit.year: credit.toJSON()
for credit in accToken.user.geruechte}
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal) return jsonify(retVal)
except Exception as err: except Exception as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409 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 = userController.getLockedDays(from_date, to_date)
retVal = []
for lockedDay in lockedDays:
day = datetime.combine(lockedDay['daydate'], time(12))
retDay = {
"worker": userController.getWorker(day),
"day": {
"date": {
"year": day.year,
"month": day.month,
"day": day.day
},
"locked": lockedDay['locked']
}
}
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']) @user.route("/user/job", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _getUser(**kwargs): def _getUser(**kwargs):
@ -107,6 +152,7 @@ def _getUser(**kwargs):
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500 return jsonify({"error": str(err)}), 500
@user.route("/user/addJob", methods=['POST']) @user.route("/user/addJob", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _addUser(**kwargs): def _addUser(**kwargs):
@ -119,10 +165,11 @@ def _addUser(**kwargs):
day = data['day'] day = data['day']
month = data['month'] month = data['month']
year = data['year'] year = data['year']
date = datetime(year,month,day,12) date = datetime(year, month, day, 12)
retVal = userController.addWorker(user.uid, date, userExc=True) retVal = userController.addWorker(user.uid, date, userExc=True)
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
jobL.info("Mitglied {} {} schreib sich am {} zum Dienst ein.".format(user.firstname, user.lastname, date.date())) jobL.info("Mitglied {} {} schreib sich am {} zum Dienst ein.".format(
user.firstname, user.lastname, date.date()))
return jsonify(retVal) return jsonify(retVal)
except DayLocked as err: except DayLocked as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
@ -131,6 +178,7 @@ def _addUser(**kwargs):
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({'error': str(err)}), 409 return jsonify({'error': str(err)}), 409
@user.route("/user/deleteJob", methods=['POST']) @user.route("/user/deleteJob", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _deletJob(**kwargs): def _deletJob(**kwargs):
@ -143,10 +191,11 @@ def _deletJob(**kwargs):
day = data['day'] day = data['day']
month = data['month'] month = data['month']
year = data['year'] year = data['year']
date = datetime(year,month,day,12) date = datetime(year, month, day, 12)
userController.deleteWorker(user.uid, date, True) userController.deleteWorker(user.uid, date, True)
debug.debug("return ok") debug.debug("return ok")
jobL.info("Mitglied {} {} entfernt sich am {} aus dem Dienst".format(user.firstname, user.lastname, date.date())) jobL.info("Mitglied {} {} entfernt sich am {} aus dem Dienst".format(
user.firstname, user.lastname, date.date()))
return jsonify({"ok": "ok"}) return jsonify({"ok": "ok"})
except DayLocked as err: except DayLocked as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
@ -155,6 +204,7 @@ def _deletJob(**kwargs):
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409 return jsonify({"error": str(err)}), 409
@user.route("/user/transactJob", methods=['POST']) @user.route("/user/transactJob", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _transactJob(**kwargs): def _transactJob(**kwargs):
@ -177,12 +227,14 @@ def _transactJob(**kwargs):
retVal['to_user'] = retVal['to_user'].toJSON() retVal['to_user'] = retVal['to_user'].toJSON()
retVal['date'] = {'year': year, 'month': month, 'day': day} retVal['date'] = {'year': year, 'month': month, 'day': day}
debug.debug("return {{ {} }}".format(retVal)) 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())) 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) return jsonify(retVal)
except Exception as err: except Exception as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409 return jsonify({"error": str(err)}), 409
@user.route("/user/answerTransactJob", methods=['POST']) @user.route("/user/answerTransactJob", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _answer(**kwargs): def _answer(**kwargs):
@ -199,19 +251,22 @@ def _answer(**kwargs):
username = data['username'] username = data['username']
date = datetime(year, month, day, 12) date = datetime(year, month, day, 12)
from_user = userController.getUser(username) from_user = userController.getUser(username)
retVal = userController.answerdTransactJob(from_user, user, date, answer) retVal = userController.answerdTransactJob(
from_user, user, date, answer)
from_userl = retVal['from_user'] from_userl = retVal['from_user']
to_userl = retVal['to_user'] to_userl = retVal['to_user']
retVal['from_user'] = retVal['from_user'].toJSON() retVal['from_user'] = retVal['from_user'].toJSON()
retVal['to_user'] = retVal['to_user'].toJSON() retVal['to_user'] = retVal['to_user'].toJSON()
retVal['date'] = {'year': year, 'month': month, 'day': day} retVal['date'] = {'year': year, 'month': month, 'day': day}
debug.debug("return {{ {} }}".format(retVal)) 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')) 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) return jsonify(retVal)
except Exception as err: except Exception as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409 return jsonify({"error": str(err)}), 409
@user.route("/user/jobRequests", methods=['POST']) @user.route("/user/jobRequests", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _requests(**kwargs): def _requests(**kwargs):
@ -230,13 +285,15 @@ def _requests(**kwargs):
data['from_user'] = data['from_user'].toJSON() data['from_user'] = data['from_user'].toJSON()
data['to_user'] = data['to_user'].toJSON() data['to_user'] = data['to_user'].toJSON()
data_date = data['date'] data_date = data['date']
data['date'] = {'year': data_date.year, 'month': data_date.month, 'day': data_date.day} data['date'] = {'year': data_date.year,
'month': data_date.month, 'day': data_date.day}
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal) return jsonify(retVal)
except Exception as err: except Exception as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409 return jsonify({"error": str(err)}), 409
@user.route("/user/getTransactJobs", methods=['POST']) @user.route("/user/getTransactJobs", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _getTransactJobs(**kwargs): def _getTransactJobs(**kwargs):
@ -255,13 +312,15 @@ def _getTransactJobs(**kwargs):
data['from_user'] = data['from_user'].toJSON() data['from_user'] = data['from_user'].toJSON()
data['to_user'] = data['to_user'].toJSON() data['to_user'] = data['to_user'].toJSON()
data_date = data['date'] data_date = data['date']
data['date'] = {'year': data_date.year, 'month': data_date.month, 'day': data_date.day} data['date'] = {'year': data_date.year,
'month': data_date.month, 'day': data_date.day}
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal) return jsonify(retVal)
except Exception as err: except Exception as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409 return jsonify({"error": str(err)}), 409
@user.route("/user/deleteTransactJob", methods=['POST']) @user.route("/user/deleteTransactJob", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _deleteTransactJob(**kwargs): def _deleteTransactJob(**kwargs):
@ -279,12 +338,14 @@ def _deleteTransactJob(**kwargs):
to_user = userController.getUser(username) to_user = userController.getUser(username)
userController.deleteTransactJob(from_user, to_user, date) userController.deleteTransactJob(from_user, to_user, date)
debug.debug("return ok") 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())) 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"}) return jsonify({"ok": "ok"})
except Exception as err: except Exception as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 409 return jsonify({"error": str(err)}), 409
@user.route("/user/storno", methods=['POST']) @user.route("/user/storno", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
def _storno(**kwargs): def _storno(**kwargs):
@ -306,12 +367,15 @@ def _storno(**kwargs):
amount = int(data['amount']) amount = int(data['amount'])
date = datetime.now() date = datetime.now()
userController.addCredit(user.uid, amount, year=date.year, month=date.month) userController.addCredit(
user.uid, amount, year=date.year, month=date.month)
accToken.user = userController.getUser(accToken.user.uid) accToken.user = userController.getUser(accToken.user.uid)
retVal = accToken.user.toJSON() retVal = accToken.user.toJSON()
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte} retVal['creditList'] = {credit.year: credit.toJSON()
for credit in accToken.user.geruechte}
debug.debug("return {{ {} }}".format(retVal)) debug.debug("return {{ {} }}".format(retVal))
creditL.info("{} {} {} storniert {}".format(date, user.firstname, user.lastname, amount/100)) creditL.info("{} {} {} storniert {}".format(
date, user.firstname, user.lastname, amount/100))
return jsonify(retVal) return jsonify(retVal)
except Exception as err: except Exception as err:
debug.debug("exception", exc_info=True) debug.debug("exception", exc_info=True)