flaschengeist/geruecht/routes.py

200 lines
7.7 KiB
Python
Raw Normal View History

2020-03-09 18:54:51 +00:00
from geruecht import app
from geruecht.logger import getDebugLogger
2020-03-03 21:33:47 +00:00
from geruecht.decorator import login_required
from geruecht.exceptions import PermissionDenied
import geruecht.controller.accesTokenController as ac
import geruecht.controller.mainController as mc
2020-03-04 20:11:41 +00:00
from geruecht.model import MONEY, BAR, USER, GASTRO, VORSTAND, EXTERN
2019-04-11 21:56:55 +00:00
from flask import request, jsonify
accesTokenController = ac.AccesTokenController()
mainController = mc.MainController()
debug = getDebugLogger()
2020-03-17 19:37:01 +00:00
@app.route("/valid", methods=['POST'])
@login_required(bar=True)
def _valid(**kwargs):
debug.info('/valid')
try:
accToken = kwargs['accToken']
data = request.get_json()
mainController.validateUser(accToken.user.uid, data['password'])
2020-03-17 19:37:01 +00:00
debug.debug('return {{ "ok": "ok" }}')
return jsonify({"ok": "ok"})
except Exception as err:
debug.warning("exception in valide.", exc_info=True)
return jsonify({"error": str(err)}), 500
2020-02-27 20:55:00 +00:00
@app.route("/pricelist", methods=['GET'])
def _getPricelist():
try:
2020-03-09 18:54:51 +00:00
debug.info("get pricelist")
retVal = mainController.getPricelist()
debug.info("return pricelist {{ {} }}".format(retVal))
2020-02-27 20:55:00 +00:00
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get pricelist.", exc_info=True)
return jsonify({"error": str(err)}), 500
2020-02-27 20:55:00 +00:00
@app.route('/drinkTypes', methods=['GET'])
def getTypes():
try:
2020-03-09 18:54:51 +00:00
debug.info("get drinktypes")
retVal = mainController.getAllDrinkTypes()
debug.info("return drinktypes {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get drinktypes.", exc_info=True)
return jsonify({"error": str(err)}), 500
2020-03-03 21:33:47 +00:00
@app.route('/getAllStatus', methods=['GET'])
2020-03-17 19:37:01 +00:00
@login_required(groups=[USER, MONEY, GASTRO, BAR, VORSTAND], bar=True)
2020-03-03 21:33:47 +00:00
def _getAllStatus(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("get all status for users")
retVal = mainController.getAllStatus()
debug.info("return all status for users {{ {} }}".format(retVal))
2020-03-03 21:33:47 +00:00
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get all status for users.", exc_info=True)
2020-03-03 21:33:47 +00:00
return jsonify({"error": str(err)}), 500
2020-02-27 20:55:00 +00:00
2020-03-03 21:33:47 +00:00
@app.route('/getStatus', methods=['POST'])
2020-03-17 19:37:01 +00:00
@login_required(groups=[USER, MONEY, GASTRO, BAR, VORSTAND], bar=True)
2020-03-03 21:33:47 +00:00
def _getStatus(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("get status from user")
2020-03-03 21:33:47 +00:00
data = request.get_json()
name = data['name']
debug.info("get status from user {{ {} }}".format(name))
retVal = mainController.getStatus(name)
debug.info(
"return status from user {{ {} }} : {{ {} }}".format(name, retVal))
2020-03-03 21:33:47 +00:00
return jsonify(retVal)
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in get status from user.", exc_info=True)
2020-03-03 21:33:47 +00:00
return jsonify({"error": str(err)}), 500
2020-03-03 21:33:47 +00:00
@app.route('/getUsers', methods=['GET'])
@login_required(groups=[USER], bar=True)
2020-03-03 21:33:47 +00:00
def _getUsers(**kwargs):
try:
extern = True
if 'extern' in request.args:
extern = not bool(int(request.args['extern']))
2020-03-09 18:54:51 +00:00
debug.info("get all users from database")
users = mainController.getAllUsersfromDB(extern=extern)
debug.debug("users are {{ {} }}".format(users))
2020-03-03 21:33:47 +00:00
retVal = [user.toJSON() for user in users]
debug.info("return all users from database {{ {} }}".format(retVal))
2020-03-03 21:33:47 +00:00
return jsonify(retVal)
except Exception as err:
debug.warning(
"exception in get all users from database.", exc_info=True)
2020-03-03 21:33:47 +00:00
return jsonify({"error": str(err)}), 500
2020-01-17 00:05:58 +00:00
2020-03-07 13:56:44 +00:00
@app.route("/getLifeTime", methods=['GET'])
2020-03-17 19:37:01 +00:00
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER], bar=True)
2020-03-07 13:56:44 +00:00
def _getLifeTime(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("get lifetime of accesstoken")
2020-03-07 13:56:44 +00:00
if 'accToken' in kwargs:
accToken = kwargs['accToken']
debug.debug("accessToken is {{ {} }}".format(accToken))
retVal = {"value": accToken.lifetime,
"group": accToken.user.toJSON()['group']}
debug.info(
"return get lifetime from accesstoken {{ {} }}".format(retVal))
2020-03-09 18:54:51 +00:00
return jsonify(retVal)
2020-03-07 13:56:44 +00:00
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.info("exception in get lifetime of accesstoken.", exc_info=True)
2020-03-07 13:56:44 +00:00
return jsonify({"error": str(err)}), 500
2020-03-07 13:56:44 +00:00
@app.route("/saveLifeTime", methods=['POST'])
2020-03-17 19:37:01 +00:00
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER], bar=True)
2020-03-07 13:56:44 +00:00
def _saveLifeTime(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("save lifetime for accessToken")
2020-03-07 13:56:44 +00:00
if 'accToken' in kwargs:
accToken = kwargs['accToken']
debug.debug("accessToken is {{ {} }}".format(accToken))
2020-03-07 13:56:44 +00:00
data = request.get_json()
lifetime = data['value']
debug.debug("lifetime is {{ {} }}".format(lifetime))
debug.info("set lifetime {{ {} }} to accesstoken {{ {} }}".format(
lifetime, accToken))
2020-03-07 13:56:44 +00:00
accToken.lifetime = lifetime
2020-03-09 18:54:51 +00:00
debug.info("update accesstoken timestamp")
accToken = accesTokenController.updateAccessToken(accToken)
accToken = accesTokenController.validateAccessToken(accToken.token, [USER, EXTERN])
retVal = {"value": accToken.lifetime,
"group": accToken.user.toJSON()['group']}
debug.info(
"return save lifetime for accessToken {{ {} }}".format(retVal))
2020-03-09 18:54:51 +00:00
return jsonify(retVal)
2020-03-07 13:56:44 +00:00
except Exception as err:
debug.warning(
"exception in save lifetime for accesstoken.", exc_info=True)
2020-03-07 13:56:44 +00:00
return jsonify({"error": str(err)}), 500
@app.route("/logout", methods=['GET'])
2020-03-17 19:37:01 +00:00
@login_required(groups=[MONEY, GASTRO, VORSTAND, EXTERN, USER], bar=True)
2020-03-07 13:56:44 +00:00
def _logout(**kwargs):
try:
2020-03-09 18:54:51 +00:00
debug.info("logout user")
2020-03-07 13:56:44 +00:00
if 'accToken' in kwargs:
accToken = kwargs['accToken']
debug.debug("accesstoken is {{ {} }}".format(accToken))
2020-03-09 18:54:51 +00:00
debug.info("delete accesstoken")
2020-03-07 13:56:44 +00:00
accesTokenController.deleteAccessToken(accToken)
2020-03-09 18:54:51 +00:00
debug.info("return ok logout user")
2020-03-07 13:56:44 +00:00
return jsonify({"ok": "ok"})
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in logout user.", exc_info=True)
2020-03-07 13:56:44 +00:00
return jsonify({"error": str(err)}), 500
2019-04-11 21:56:55 +00:00
@app.route("/login", methods=['POST'])
def _login():
2019-04-17 12:46:46 +00:00
""" Login User
2019-04-17 12:46:46 +00:00
Nothing to say.
Login in User and create an AccessToken for the User.
Returns:
A JSON-File with createt Token or Errors
"""
2020-03-09 18:54:51 +00:00
debug.info("Start log in.")
2019-04-11 21:56:55 +00:00
data = request.get_json()
username = data['username']
password = data['password']
debug.debug("username is {{ {} }}".format(username))
try:
user_agent = request.user_agent
debug.info("search {{ {} }} in database".format(username))
user = mainController.loginUser(username, password)
debug.debug("user is {{ {} }}".format(user))
token = accesTokenController.createAccesToken(user, user_agent=user_agent)
debug.debug("accesstoken is {{ {} }}".format(token))
2020-03-09 18:54:51 +00:00
debug.info("validate accesstoken")
dic = accesTokenController.validateAccessToken(
token, [USER, EXTERN]).user.toJSON()
dic["token"] = token
dic["accessToken"] = token
debug.info("User {{ {} }} success login.".format(username))
debug.info("return login {{ {} }}".format(dic))
return jsonify(dic)
except PermissionDenied as err:
2020-03-09 18:54:51 +00:00
debug.warning("permission denied exception in logout", exc_info=True)
return jsonify({"error": str(err)}), 401
except Exception as err:
2020-03-09 18:54:51 +00:00
debug.warning("exception in logout.", exc_info=True)
return jsonify({"error": "permission denied"}), 401