226 lines
7.6 KiB
Python
226 lines
7.6 KiB
Python
"""Pricelist plugin"""
|
|
|
|
from flask import Blueprint, jsonify, request, current_app
|
|
from werkzeug.local import LocalProxy
|
|
from werkzeug.exceptions import BadRequest, Forbidden, Unauthorized
|
|
|
|
from flaschengeist import logger
|
|
from flaschengeist.plugins import Plugin
|
|
from flaschengeist.utils.decorators import login_required, extract_session
|
|
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"
|
|
permissions = permissions.permissions
|
|
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()
|
|
drink_type = pricelist_controller.create_tag(data)
|
|
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()
|
|
tag = pricelist_controller.update_tag(identifier, data)
|
|
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):
|
|
public = True
|
|
try:
|
|
extract_session()
|
|
public = False
|
|
except Unauthorized:
|
|
public = True
|
|
|
|
if identifier:
|
|
result = pricelist_controller.get_drink(identifier, public=public)
|
|
else:
|
|
result = pricelist_controller.get_drinks(public=public)
|
|
logger.debug(f"GET drink {result}")
|
|
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()
|
|
logger.debug(f"update drink {data}")
|
|
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!
|
|
try:
|
|
min_prices = PriceListPlugin.plugin.get_setting("min_prices")
|
|
except KeyError:
|
|
min_prices = []
|
|
return jsonify(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("/drinks/<int:identifier>/picture", methods=["POST", "GET", "DELETE"])
|
|
def set_picture(identifier):
|
|
|
|
if request.method == "DELETE":
|
|
pricelist_controller.delete_drink_picture(identifier)
|
|
return no_content()
|
|
|
|
file = request.files.get("file")
|
|
if file:
|
|
picture = models._Picture()
|
|
picture.mimetype = file.content_type
|
|
picture.binary = bytearray(file.stream.read())
|
|
return jsonify(pricelist_controller.save_drink_picture(identifier, picture))
|
|
else:
|
|
raise BadRequest
|
|
|
|
|
|
@PriceListPlugin.blueprint.route("/picture/<identifier>", methods=["GET"])
|
|
def _get_picture(identifier):
|
|
if request.method == "GET":
|
|
size = request.args.get("size")
|
|
response = pricelist_controller.get_drink_picture(identifier, size)
|
|
return response.make_conditional(request)
|