flaschengeist/flaschengeist/modules/schedule/__init__.py

382 lines
14 KiB
Python

from flask import Blueprint, request, jsonify
from flaschengeist.system.decorator import login_required
from flaschengeist import logger
from datetime import datetime
import .roles
schedule = Blueprint("schedule", __name__)
@schedule.route("/schedule/jobs", methods=['POST'])
@login_required(roles=['schedule_read'])
def _getUsers(**kwrags):
logger.debug("/schedule/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)
logger.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
logger.warn("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@schedule.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
@schedule.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
@schedule.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)
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)
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
@schedule.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)
retVal = mainController.getWorker(date)
debug.debug("return ok")
jobL.info("Mitglied {} {} entfernt sich am {} aus dem Dienst".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
@schedule.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
@schedule.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
@schedule.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)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@schedule.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
@schedule.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':
data['on_date'] = date(data['on_date']['year'], data['on_date']['month'], data['on_date']['day'])
retVal = mainController.updateJobRequests(data)
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@schedule.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)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
# CREDIT == Gerücht? CreditList?
#creditL = getCreditLogger()
#@schedule.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
#@schedule.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
#@schedule.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()
# password = data['acceptedPassword']
# data.pop('acceptedPassword')
# accToken.user = mainController.modifyUser(
# accToken.user, data, password)
# 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
#
#@schedule.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)}), 500