2020-01-17 00:05:58 +00:00
|
|
|
from flask import Blueprint, request, jsonify
|
2020-01-19 08:07:45 +00:00
|
|
|
from geruecht.decorator import login_required
|
2020-05-22 19:55:14 +00:00
|
|
|
import geruecht.controller.mainController as mc
|
2020-01-17 00:05:58 +00:00
|
|
|
from geruecht.model import USER
|
2020-05-27 10:10:51 +00:00
|
|
|
from datetime import datetime, time, date
|
2020-02-23 21:31:22 +00:00
|
|
|
from geruecht.exceptions import DayLocked
|
2020-03-10 08:19:11 +00:00
|
|
|
from geruecht.logger import getDebugLogger, getCreditLogger, getJobsLogger
|
2020-01-17 00:05:58 +00:00
|
|
|
|
|
|
|
user = Blueprint("user", __name__)
|
|
|
|
|
2020-05-22 19:55:14 +00:00
|
|
|
mainController = mc.MainController()
|
2020-01-18 23:37:40 +00:00
|
|
|
|
2020-03-10 08:19:11 +00:00
|
|
|
debug = getDebugLogger()
|
|
|
|
creditL = getCreditLogger()
|
|
|
|
jobL = getJobsLogger()
|
2020-01-17 00:05:58 +00:00
|
|
|
|
2020-03-13 17:06:24 +00:00
|
|
|
|
2020-01-19 08:07:45 +00:00
|
|
|
@user.route("/user/main")
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _main(**kwargs):
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/user/main")
|
|
|
|
try:
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
2020-05-22 19:55:14 +00:00
|
|
|
accToken.user = mainController.getUser(accToken.user.uid)
|
2020-03-10 08:19:11 +00:00
|
|
|
retVal = accToken.user.toJSON()
|
2020-03-13 17:06:24 +00:00
|
|
|
retVal['creditList'] = {credit.year: credit.toJSON()
|
|
|
|
for credit in accToken.user.geruechte}
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(retVal)
|
2020-03-10 10:08:24 +00:00
|
|
|
except Exception:
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify("error", "something went wrong"), 500
|
2020-01-17 00:05:58 +00:00
|
|
|
|
2020-03-13 17:06:24 +00:00
|
|
|
|
2020-01-17 00:05:58 +00:00
|
|
|
@user.route("/user/addAmount", methods=['POST'])
|
2020-01-19 08:07:45 +00:00
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _addAmount(**kwargs):
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/user/addAmount")
|
|
|
|
try:
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
|
|
|
data = request.get_json()
|
|
|
|
amount = int(data['amount'])
|
|
|
|
date = datetime.now()
|
2020-05-22 19:55:14 +00:00
|
|
|
mainController.addAmount(
|
2020-03-13 17:06:24 +00:00
|
|
|
accToken.user.uid, amount, year=date.year, month=date.month)
|
2020-05-22 19:55:14 +00:00
|
|
|
accToken.user = mainController.getUser(accToken.user.uid)
|
2020-03-10 08:19:11 +00:00
|
|
|
retVal = accToken.user.toJSON()
|
2020-03-13 17:06:24 +00:00
|
|
|
retVal['creditList'] = {credit.year: credit.toJSON()
|
|
|
|
for credit in accToken.user.geruechte}
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
2020-03-13 17:06:24 +00:00
|
|
|
creditL.info("{} {} {} fügt sich selbst {} € Schulden hinzu".format(
|
|
|
|
date, accToken.user.firstname, accToken.user.lastname, amount/100))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(retVal)
|
2020-03-10 10:08:24 +00:00
|
|
|
except Exception:
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": "something went wrong"}), 500
|
2020-01-26 22:31:22 +00:00
|
|
|
|
2020-03-13 17:06:24 +00:00
|
|
|
|
2020-01-26 22:31:22 +00:00
|
|
|
@user.route("/user/saveConfig", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _saveConfig(**kwargs):
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/user/saveConfig")
|
2020-01-26 22:31:22 +00:00
|
|
|
try:
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
|
|
|
data = request.get_json()
|
2020-05-22 19:55:14 +00:00
|
|
|
accToken.user = mainController.modifyUser(
|
2020-03-13 17:06:24 +00:00
|
|
|
accToken.user, accToken.ldap_conn, data)
|
2020-01-26 22:54:18 +00:00
|
|
|
retVal = accToken.user.toJSON()
|
2020-03-13 17:06:24 +00:00
|
|
|
retVal['creditList'] = {credit.year: credit.toJSON()
|
|
|
|
for credit in accToken.user.geruechte}
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
2020-01-26 22:54:18 +00:00
|
|
|
return jsonify(retVal)
|
2020-01-26 22:31:22 +00:00
|
|
|
except Exception as err:
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("exception", exc_info=True)
|
2020-02-25 21:50:32 +00:00
|
|
|
return jsonify({"error": str(err)}), 409
|
2020-01-27 19:16:04 +00:00
|
|
|
|
2020-03-13 17:06:24 +00:00
|
|
|
|
|
|
|
@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'])
|
2020-05-22 19:55:14 +00:00
|
|
|
lockedDays = mainController.getLockedDays(from_date, to_date)
|
2020-03-13 17:06:24 +00:00
|
|
|
retVal = []
|
|
|
|
for lockedDay in lockedDays:
|
|
|
|
day = datetime.combine(lockedDay['daydate'], time(12))
|
|
|
|
retDay = {
|
2020-05-22 19:55:14 +00:00
|
|
|
"worker": mainController.getWorker(day),
|
2020-03-13 17:06:24 +00:00
|
|
|
"day": {
|
|
|
|
"date": {
|
|
|
|
"year": day.year,
|
|
|
|
"month": day.month,
|
|
|
|
"day": day.day
|
|
|
|
},
|
|
|
|
"locked": lockedDay['locked']
|
2020-05-22 19:55:14 +00:00
|
|
|
},
|
|
|
|
"jobkinddate": mainController.getJobKindDates(day.date())
|
2020-03-13 17:06:24 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2020-05-27 10:10:51 +00:00
|
|
|
@user.route("/user/jobsOnDates", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _getJobsOnDates(**kwargs):
|
|
|
|
debug.info("/user/jobsOnDates")
|
|
|
|
try:
|
|
|
|
data = request.get_json()
|
|
|
|
lockedDays = mainController.getLockedDaysFromList(data)
|
|
|
|
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
|
2020-03-13 17:06:24 +00:00
|
|
|
|
2020-01-27 19:16:04 +00:00
|
|
|
@user.route("/user/job", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
2020-02-22 10:15:20 +00:00
|
|
|
def _getUser(**kwargs):
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/user/job")
|
|
|
|
try:
|
|
|
|
data = request.get_json()
|
|
|
|
day = data['day']
|
|
|
|
month = data['month']
|
|
|
|
year = data['year']
|
|
|
|
date = datetime(year, month, day, 12)
|
2020-05-22 19:55:14 +00:00
|
|
|
lockedDay = mainController.getLockedDay(date)
|
2020-03-10 08:19:11 +00:00
|
|
|
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 = {
|
2020-05-22 19:55:14 +00:00
|
|
|
'worker': mainController.getWorker(date),
|
2020-03-10 08:19:11 +00:00
|
|
|
'day': lockedDay
|
2020-02-23 21:31:22 +00:00
|
|
|
}
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("retrun {{ {} }}".format(retVal))
|
2020-03-10 08:19:11 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
2020-02-23 21:31:22 +00:00
|
|
|
|
2020-03-13 17:06:24 +00:00
|
|
|
|
2020-02-23 21:31:22 +00:00
|
|
|
@user.route("/user/addJob", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _addUser(**kwargs):
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/user/addJob")
|
2020-02-23 21:31:22 +00:00
|
|
|
try:
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
|
|
|
user = accToken.user
|
|
|
|
data = request.get_json()
|
|
|
|
day = data['day']
|
|
|
|
month = data['month']
|
|
|
|
year = data['year']
|
2020-03-13 17:06:24 +00:00
|
|
|
date = datetime(year, month, day, 12)
|
2020-05-24 20:12:46 +00:00
|
|
|
job_kind = None
|
|
|
|
if 'job_kind' in data:
|
|
|
|
job_kind = data['job_kind']
|
|
|
|
mainController.addWorker(user.uid, date, job_kind=job_kind, userExc=True)
|
|
|
|
retVal = mainController.getWorker(date)
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
2020-03-13 17:06:24 +00:00
|
|
|
jobL.info("Mitglied {} {} schreib sich am {} zum Dienst ein.".format(
|
|
|
|
user.firstname, user.lastname, date.date()))
|
2020-02-23 21:31:22 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
except DayLocked as err:
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("exception", exc_info=True)
|
2020-02-25 21:50:32 +00:00
|
|
|
return jsonify({'error': str(err)}), 403
|
2020-02-23 21:31:22 +00:00
|
|
|
except Exception as err:
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("exception", exc_info=True)
|
2020-02-25 21:50:32 +00:00
|
|
|
return jsonify({'error': str(err)}), 409
|
2020-02-24 17:55:57 +00:00
|
|
|
|
2020-03-13 17:06:24 +00:00
|
|
|
|
2020-02-24 17:55:57 +00:00
|
|
|
@user.route("/user/deleteJob", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _deletJob(**kwargs):
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.info("/user/deleteJob")
|
2020-02-24 17:55:57 +00:00
|
|
|
try:
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
|
|
|
user = accToken.user
|
|
|
|
data = request.get_json()
|
|
|
|
day = data['day']
|
|
|
|
month = data['month']
|
|
|
|
year = data['year']
|
2020-03-13 17:06:24 +00:00
|
|
|
date = datetime(year, month, day, 12)
|
2020-05-22 19:55:14 +00:00
|
|
|
mainController.deleteWorker(user.uid, date, True)
|
2020-05-27 10:10:51 +00:00
|
|
|
retVal = mainController.getWorker(date)
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("return ok")
|
2020-03-13 17:06:24 +00:00
|
|
|
jobL.info("Mitglied {} {} entfernt sich am {} aus dem Dienst".format(
|
|
|
|
user.firstname, user.lastname, date.date()))
|
2020-05-27 10:10:51 +00:00
|
|
|
return jsonify(retVal)
|
2020-02-24 17:55:57 +00:00
|
|
|
except DayLocked as err:
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("exception", exc_info=True)
|
2020-02-25 21:50:32 +00:00
|
|
|
return jsonify({"error": str(err)}), 403
|
2020-02-24 17:55:57 +00:00
|
|
|
except Exception as err:
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("exception", exc_info=True)
|
2020-02-25 21:50:32 +00:00
|
|
|
return jsonify({"error": str(err)}), 409
|
|
|
|
|
2020-02-27 14:53:49 +00:00
|
|
|
@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
|
|
|
|
"""
|
2020-03-10 08:19:11 +00:00
|
|
|
|
|
|
|
debug.info("/user/storno")
|
2020-02-27 14:53:49 +00:00
|
|
|
try:
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
|
|
|
user = accToken.user
|
|
|
|
data = request.get_json()
|
|
|
|
amount = int(data['amount'])
|
|
|
|
|
|
|
|
date = datetime.now()
|
2020-05-22 19:55:14 +00:00
|
|
|
mainController.addCredit(
|
2020-03-13 17:06:24 +00:00
|
|
|
user.uid, amount, year=date.year, month=date.month)
|
2020-05-22 19:55:14 +00:00
|
|
|
accToken.user = mainController.getUser(accToken.user.uid)
|
2020-02-27 14:53:49 +00:00
|
|
|
retVal = accToken.user.toJSON()
|
2020-03-13 17:06:24 +00:00
|
|
|
retVal['creditList'] = {credit.year: credit.toJSON()
|
|
|
|
for credit in accToken.user.geruechte}
|
2020-03-10 10:08:24 +00:00
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
2020-03-13 17:06:24 +00:00
|
|
|
creditL.info("{} {} {} storniert {} €".format(
|
|
|
|
date, user.firstname, user.lastname, amount/100))
|
2020-02-27 14:53:49 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
2020-03-10 08:19:11 +00:00
|
|
|
debug.debug("exception", exc_info=True)
|
2020-05-27 10:10:51 +00:00
|
|
|
return jsonify({"error": str(err)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
@user.route("/user/getJobInvites", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _getJobInvites(**kwargs):
|
|
|
|
try:
|
|
|
|
debug.info("/user/getJobInvites")
|
|
|
|
from_user = None
|
|
|
|
to_user = None
|
|
|
|
on_date = None
|
|
|
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
|
|
if 'from_user' in data:
|
|
|
|
from_user = data['from_user']
|
|
|
|
if 'to_user' in data:
|
|
|
|
to_user = data['to_user']
|
|
|
|
on_date = date(data['date']['year'], data['date']['month'], data['date']['day'])
|
|
|
|
retVal = mainController.getJobInvites(from_user, to_user, on_date)
|
|
|
|
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/JobInvites", methods=['PUT', 'POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _JobInvites(**kwargs):
|
|
|
|
try:
|
|
|
|
debug.info("/user/JobInvites")
|
|
|
|
data = request.get_json()
|
|
|
|
if request.method == 'PUT':
|
|
|
|
mainController.setJobInvites(data)
|
|
|
|
retVal = mainController.getJobInvites(kwargs['accToken'].user.toJSON(), None, datetime.now().date())
|
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
|
|
|
if request.method == 'POST':
|
|
|
|
retVal = mainController.updateJobInvites(data)
|
|
|
|
|
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
|
|
|
|
|
|
|
@user.route("/user/deleteJobInvite", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _deleteJobInvite(**kwargs):
|
|
|
|
try:
|
|
|
|
debug.info("/user/deleteJobInvite")
|
|
|
|
data = request.get_json()
|
|
|
|
mainController.deleteJobInvite(data)
|
|
|
|
retVal = mainController.getJobInvites(data['from_user'], None, datetime.now().date())
|
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
|
|
|
return jsonify(retVal)
|
2020-06-02 21:24:17 +00:00
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
@user.route("/user/getJobRequests", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _getJobRequests(**kwargs):
|
|
|
|
try:
|
|
|
|
debug.info("/user/getJobRequests")
|
|
|
|
from_user = None
|
|
|
|
to_user = None
|
|
|
|
on_date = None
|
|
|
|
|
|
|
|
data = request.get_json()
|
|
|
|
|
|
|
|
if 'from_user' in data:
|
|
|
|
from_user = data['from_user']
|
|
|
|
if 'to_user' in data:
|
|
|
|
to_user = data['to_user']
|
|
|
|
on_date = date(data['date']['year'], data['date']['month'], data['date']['day'])
|
|
|
|
retVal = mainController.getJobRequests(from_user, to_user, on_date)
|
|
|
|
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/JobRequests", methods=['PUT', 'POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _JobRequests(**kwargs):
|
|
|
|
try:
|
|
|
|
debug.info("/user/JobRequests")
|
|
|
|
data = request.get_json()
|
|
|
|
if request.method == 'PUT':
|
|
|
|
mainController.setJobRequests(data)
|
|
|
|
retVal = mainController.getJobRequests(kwargs['accToken'].user.toJSON(), None, datetime.now().date())
|
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
|
|
|
if request.method == 'POST':
|
2020-06-04 11:19:00 +00:00
|
|
|
data['on_date'] = date(data['on_date']['year'], data['on_date']['month'], data['on_date']['day'])
|
2020-06-02 21:24:17 +00:00
|
|
|
retVal = mainController.updateJobRequests(data)
|
|
|
|
|
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|
|
|
|
|
|
|
|
@user.route("/user/deleteJobRequest", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _deleteJobRequest(**kwargs):
|
|
|
|
try:
|
|
|
|
debug.info("/user/deleteJobRequest")
|
|
|
|
data = request.get_json()
|
|
|
|
mainController.deleteJobRequest(data)
|
|
|
|
retVal = mainController.getJobRequests(data['from_user'], None, datetime.now().date())
|
|
|
|
debug.debug("return {{ {} }}".format(retVal))
|
|
|
|
return jsonify(retVal)
|
2020-05-27 10:10:51 +00:00
|
|
|
except Exception as err:
|
|
|
|
debug.debug("exception", exc_info=True)
|
|
|
|
return jsonify({"error": str(err)}), 500
|