222 lines
7.5 KiB
Python
222 lines
7.5 KiB
Python
"""Pricelist plugin"""
|
|
|
|
from flask import Blueprint, jsonify, request, current_app
|
|
from werkzeug.local import LocalProxy
|
|
from werkzeug.exceptions import BadRequest, Forbidden
|
|
|
|
from flaschengeist.plugins import Plugin
|
|
from flaschengeist.utils.decorators import login_required
|
|
from flaschengeist.utils.HTTP import no_content
|
|
from flaschengeist.models.session import Session
|
|
from flaschengeist.controller import userController
|
|
|
|
from . import models
|
|
from . import pricelist_controller, permissions
|
|
|
|
|
|
class PriceListPlugin(Plugin):
|
|
name = "pricelist"
|
|
blueprint = Blueprint(name, __name__, url_prefix="/pricelist")
|
|
plugin = LocalProxy(lambda: current_app.config["FG_PLUGINS"][PriceListPlugin.name])
|
|
models = models
|
|
|
|
def __init__(self, cfg):
|
|
super().__init__(cfg)
|
|
config = {"discount": 0}
|
|
config.update(cfg)
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drink-types", methods=["GET"])
|
|
@PriceListPlugin.blueprint.route("/drink-types/<int:identifier>", methods=["GET"])
|
|
def get_drink_types(identifier=None):
|
|
if identifier is None:
|
|
result = pricelist_controller.get_drink_types()
|
|
else:
|
|
result = pricelist_controller.get_drink_type(identifier)
|
|
return jsonify(result)
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drink-types", methods=["POST"])
|
|
@login_required(permission=permissions.CREATE_TYPE)
|
|
def new_drink_type(current_session):
|
|
data = request.get_json()
|
|
if "name" not in data:
|
|
raise BadRequest
|
|
drink_type = pricelist_controller.create_drink_type(data["name"])
|
|
return jsonify(drink_type)
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drink-types/<int:identifier>", methods=["PUT"])
|
|
@login_required(permission=permissions.EDIT_TYPE)
|
|
def update_drink_type(identifier, current_session):
|
|
data = request.get_json()
|
|
if "name" not in data:
|
|
raise BadRequest
|
|
drink_type = pricelist_controller.rename_drink_type(identifier, data["name"])
|
|
return jsonify(drink_type)
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drink-types/<int:identifier>", methods=["DELETE"])
|
|
@login_required(permission=permissions.DELETE_TYPE)
|
|
def delete_drink_type(identifier, current_session):
|
|
pricelist_controller.delete_drink_type(identifier)
|
|
return no_content()
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/tags", methods=["GET"])
|
|
@PriceListPlugin.blueprint.route("/tags/<int:identifier>", methods=["GET"])
|
|
def get_tags(identifier=None):
|
|
if identifier:
|
|
result = pricelist_controller.get_tag(identifier)
|
|
else:
|
|
result = pricelist_controller.get_tags()
|
|
return jsonify(result)
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/tags", methods=["POST"])
|
|
@login_required(permission=permissions.CREATE_TAG)
|
|
def new_tag(current_session):
|
|
data = request.get_json()
|
|
if "name" not in data:
|
|
raise BadRequest
|
|
drink_type = pricelist_controller.create_tag(data["name"])
|
|
return jsonify(drink_type)
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/tags/<int:identifier>", methods=["PUT"])
|
|
@login_required(permission=permissions.EDIT_TAG)
|
|
def update_tag(identifier, current_session):
|
|
data = request.get_json()
|
|
if "name" not in data:
|
|
raise BadRequest
|
|
tag = pricelist_controller.rename_tag(identifier, data["name"])
|
|
return jsonify(tag)
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/tags/<int:identifier>", methods=["DELETE"])
|
|
@login_required(permission=permissions.DELETE_TAG)
|
|
def delete_tag(identifier, current_session):
|
|
pricelist_controller.delete_tag(identifier)
|
|
return no_content()
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drinks", methods=["GET"])
|
|
@PriceListPlugin.blueprint.route("/drinks/<int:identifier>", methods=["GET"])
|
|
def get_drinks(identifier=None):
|
|
if identifier:
|
|
result = pricelist_controller.get_drink(identifier)
|
|
else:
|
|
result = pricelist_controller.get_drinks()
|
|
return jsonify(result)
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drinks/search/<string:name>", methods=["GET"])
|
|
def search_drinks(name):
|
|
return jsonify(pricelist_controller.get_drinks(name))
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drinks", methods=["POST"])
|
|
@login_required(permission=permissions.CREATE)
|
|
def create_drink(current_session):
|
|
data = request.get_json()
|
|
return jsonify(pricelist_controller.set_drink(data))
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drinks/<int:identifier>", methods=["PUT"])
|
|
def update_drink(identifier):
|
|
data = request.get_json()
|
|
return jsonify(pricelist_controller.update_drink(identifier, data))
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/drinks/<int:identifier>", methods=["DELETE"])
|
|
def delete_drink(identifier):
|
|
pricelist_controller.delete_drink(identifier)
|
|
return no_content()
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/prices/<int:identifier>", methods=["DELETE"])
|
|
def delete_price(identifier):
|
|
pricelist_controller.delete_price(identifier)
|
|
return no_content()
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/volumes/<int:identifier>", methods=["DELETE"])
|
|
def delete_volume(identifier):
|
|
pricelist_controller.delete_volume(identifier)
|
|
return no_content()
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/ingredients/extraIngredients", methods=["GET"])
|
|
def get_extra_ingredients():
|
|
return jsonify(pricelist_controller.get_extra_ingredients())
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/ingredients/<int:identifier>", methods=["DELETE"])
|
|
def delete_ingredient(identifier):
|
|
pricelist_controller.delete_ingredient(identifier)
|
|
return no_content()
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/ingredients/extraIngredients", methods=["POST"])
|
|
def set_extra_ingredient():
|
|
data = request.get_json()
|
|
return jsonify(pricelist_controller.set_extra_ingredient(data))
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/ingredients/extraIngredients/<int:identifier>", methods=["PUT"])
|
|
def update_extra_ingredient(identifier):
|
|
data = request.get_json()
|
|
return jsonify(pricelist_controller.update_extra_ingredient(identifier, data))
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/ingredients/extraIngredients/<int:identifier>", methods=["DELETE"])
|
|
def delete_extra_ingredient(identifier):
|
|
pricelist_controller.delete_extra_ingredient(identifier)
|
|
return no_content()
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/settings/min_prices", methods=["POST", "GET"])
|
|
def pricelist_settings_min_prices():
|
|
if request.method == "GET":
|
|
# TODO: Handle if no prices are set!
|
|
return jsonify(PriceListPlugin.plugin.get_setting("min_prices"))
|
|
else:
|
|
data = request.get_json()
|
|
if not isinstance(data, list) or not all(isinstance(n, int) for n in data):
|
|
raise BadRequest
|
|
data.sort()
|
|
PriceListPlugin.plugin.set_setting("min_prices", data)
|
|
return no_content()
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/users/<userid>/pricecalc_columns", methods=["GET", "PUT"])
|
|
@login_required()
|
|
def get_columns(userid, current_session: Session):
|
|
"""Get pricecalc_columns of an user
|
|
|
|
Route: ``/users/<userid>/pricelist/pricecac_columns`` | Method: ``GET`` or ``PUT``
|
|
POST-data: On ``PUT`` json encoded array of floats
|
|
|
|
Args:
|
|
userid: Userid identifying the user
|
|
current_session: Session sent with Authorization Header
|
|
|
|
Returns:
|
|
GET: JSON object containing the shortcuts as float array or HTTP error
|
|
PUT: HTTP-created or HTTP error
|
|
"""
|
|
if userid != current_session.user_.userid:
|
|
raise Forbidden
|
|
|
|
user = userController.get_user(userid)
|
|
if request.method == "GET":
|
|
return jsonify(user.get_attribute("pricecalc_columns", []))
|
|
else:
|
|
data = request.get_json()
|
|
if not isinstance(data, list) or not all(isinstance(n, str) for n in data):
|
|
raise BadRequest
|
|
data.sort(reverse=True)
|
|
user.set_attribute("pricecalc_columns", data)
|
|
userController.persist()
|
|
return no_content()
|