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-01-18 23:37:40 +00:00
|
|
|
import geruecht.controller.userController as uc
|
2020-01-17 00:05:58 +00:00
|
|
|
from geruecht.model import USER
|
|
|
|
from datetime import datetime
|
2020-02-23 21:31:22 +00:00
|
|
|
from geruecht.exceptions import DayLocked
|
2020-01-17 00:05:58 +00:00
|
|
|
|
|
|
|
user = Blueprint("user", __name__)
|
|
|
|
|
2020-01-18 23:37:40 +00:00
|
|
|
userController = uc.UserController()
|
|
|
|
|
2020-01-17 00:05:58 +00:00
|
|
|
|
2020-01-19 08:07:45 +00:00
|
|
|
@user.route("/user/main")
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _main(**kwargs):
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
2020-01-17 00:05:58 +00:00
|
|
|
accToken.user = userController.getUser(accToken.user.uid)
|
|
|
|
retVal = accToken.user.toJSON()
|
|
|
|
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte}
|
|
|
|
return jsonify(retVal)
|
2020-01-19 08:07:45 +00:00
|
|
|
return jsonify("error", "something went wrong"), 500
|
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):
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
2020-01-17 00:05:58 +00:00
|
|
|
data = request.get_json()
|
|
|
|
amount = int(data['amount'])
|
|
|
|
date = datetime.now()
|
|
|
|
userController.addAmount(accToken.user.uid, amount, year=date.year, month=date.month)
|
|
|
|
accToken.user = userController.getUser(accToken.user.uid)
|
|
|
|
retVal = accToken.user.toJSON()
|
|
|
|
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte}
|
|
|
|
return jsonify(retVal)
|
2020-01-26 22:31:22 +00:00
|
|
|
return jsonify({"error": "something went wrong"}), 500
|
|
|
|
|
|
|
|
@user.route("/user/saveConfig", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _saveConfig(**kwargs):
|
|
|
|
try:
|
|
|
|
if 'accToken' in kwargs:
|
|
|
|
accToken = kwargs['accToken']
|
|
|
|
data = request.get_json()
|
|
|
|
accToken.user = userController.modifyUser(accToken.user, accToken.ldap_conn, data)
|
2020-01-26 22:54:18 +00:00
|
|
|
retVal = accToken.user.toJSON()
|
|
|
|
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte}
|
|
|
|
return jsonify(retVal)
|
2020-01-26 22:31:22 +00:00
|
|
|
except Exception as err:
|
2020-02-25 21:50:32 +00:00
|
|
|
return jsonify({"error": str(err)}), 409
|
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):
|
|
|
|
data = request.get_json()
|
2020-02-23 10:18:54 +00:00
|
|
|
day = data['day']
|
|
|
|
month = data['month']
|
|
|
|
year = data['year']
|
|
|
|
date = datetime(year, month, day, 12)
|
2020-02-23 21:31:22 +00:00
|
|
|
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']
|
|
|
|
}
|
2020-02-23 20:27:03 +00:00
|
|
|
retVal = {
|
|
|
|
'worker': userController.getWorker(date),
|
2020-02-23 21:31:22 +00:00
|
|
|
'day': lockedDay
|
2020-02-23 20:27:03 +00:00
|
|
|
}
|
2020-02-22 10:15:20 +00:00
|
|
|
print(retVal)
|
2020-02-23 21:31:22 +00:00
|
|
|
return jsonify(retVal)
|
|
|
|
|
|
|
|
@user.route("/user/addJob", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _addUser(**kwargs):
|
|
|
|
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 = userController.addWorker(user.uid, date, userExc=True)
|
|
|
|
print(retVal)
|
|
|
|
return jsonify(retVal)
|
|
|
|
except DayLocked as err:
|
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-02-25 21:50:32 +00:00
|
|
|
return jsonify({'error': str(err)}), 409
|
2020-02-24 17:55:57 +00:00
|
|
|
|
|
|
|
@user.route("/user/deleteJob", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _deletJob(**kwargs):
|
|
|
|
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)
|
|
|
|
userController.deleteWorker(user.uid, date, True)
|
|
|
|
return jsonify({"ok": "ok"})
|
|
|
|
except DayLocked as err:
|
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-02-25 21:50:32 +00:00
|
|
|
return jsonify({"error": str(err)}), 409
|
|
|
|
|
|
|
|
@user.route("/user/transactJob", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _transactJob(**kwargs):
|
|
|
|
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 = userController.getUser(username)
|
|
|
|
retVal = userController.setTransactJob(user, to_user, date)
|
|
|
|
retVal['from_user'] = retVal['from_user'].toJSON()
|
|
|
|
retVal['to_user'] = retVal['to_user'].toJSON()
|
|
|
|
retVal['date'] = {'year': year, 'month': month, 'day': day}
|
|
|
|
print(retVal)
|
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
return jsonify({"error": str(err)}), 409
|
|
|
|
|
|
|
|
@user.route("/user/answerTransactJob", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _answer(**kwargs):
|
|
|
|
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 = userController.getUser(username)
|
|
|
|
retVal = userController.answerdTransactJob(from_user, user, date, answer)
|
|
|
|
retVal['from_user'] = retVal['from_user'].toJSON()
|
|
|
|
retVal['to_user'] = retVal['to_user'].toJSON()
|
|
|
|
retVal['date'] = {'year': year, 'month': month, 'day': day}
|
|
|
|
print(retVal)
|
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
return jsonify({"error": str(err)}), 409
|
|
|
|
|
|
|
|
@user.route("/user/jobRequests", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _requests(**kwargs):
|
|
|
|
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 = userController.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}
|
|
|
|
print(retVal)
|
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
return jsonify({"error": str(err)}), 409
|
|
|
|
|
|
|
|
@user.route("/user/getTransactJobs", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _getTransactJobs(**kwargs):
|
|
|
|
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 = userController.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}
|
|
|
|
print(retVal)
|
|
|
|
return jsonify(retVal)
|
|
|
|
except Exception as err:
|
|
|
|
return jsonify({"error": str(err)}), 409
|
|
|
|
|
|
|
|
@user.route("/user/deleteTransactJob", methods=['POST'])
|
|
|
|
@login_required(groups=[USER])
|
|
|
|
def _deleteTransactJob(**kwargs):
|
|
|
|
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 = userController.getUser(username)
|
|
|
|
userController.deleteTransactJob(from_user, to_user, date)
|
|
|
|
return jsonify({"ok": "ok"})
|
|
|
|
except Exception as err:
|
|
|
|
return jsonify({"error": str(err)}), 409
|