flaschengeist/flaschengeist/plugins/pricelist/__init__.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

197 lines
6.2 KiB
Python
Raw Normal View History

"""Pricelist plugin"""
from flask import Blueprint, jsonify, request
from http.client import NO_CONTENT
from flaschengeist.plugins import Plugin
from flaschengeist.utils.decorators import login_required
from werkzeug.exceptions import BadRequest
from . import models
from . import pricelist_controller, permissions
pricelist_bp = Blueprint("pricelist", __name__, url_prefix="/pricelist")
class PriceListPlugin(Plugin):
models = models
def __init__(self, cfg):
super().__init__(blueprint=pricelist_bp, permissions=permissions.permissions)
config = {"discount": 0}
config.update(cfg)
def install(self):
from flaschengeist.database import db
db.create_all()
@pricelist_bp.route("/drink-types", methods=["GET"])
@pricelist_bp.route("/drink-types/<int:identifier>", methods=["GET"])
def get_drink_types(identifier=None):
if identifier:
result = pricelist_controller.get_drink_type(identifier)
else:
result = pricelist_controller.get_drink_types()
return jsonify(result)
@pricelist_bp.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)
@pricelist_bp.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(data["id"], data["name"])
return jsonify(drink_type)
@pricelist_bp.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
@pricelist_bp.route("/tags", methods=["GET"])
@pricelist_bp.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)
@pricelist_bp.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)
@pricelist_bp.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
drink_type = pricelist_controller.rename_tag(data["name"])
return jsonify(drink_type)
@pricelist_bp.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
@pricelist_bp.route("/drinks", methods=["GET"])
@pricelist_bp.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)
@pricelist_bp.route("/drinks/search/<string:name>", methods=["GET"])
def search_drinks(name):
return jsonify(pricelist_controller.get_drinks(name))
@pricelist_bp.route("/drinks", methods=["POST"])
@login_required(permission=permissions.CREATE)
def create_drink(current_session):
data = request.get_json()
if not all(item in data for item in ["name", "volume", "cost_price"]) or not all(
item in data for item in ["name", "ingredients"]
):
raise BadRequest("No correct Keys to create drink")
2021-03-15 18:56:51 +00:00
return "jsonify(pricelist_controller.create_drink(data))"
@pricelist_bp.route("/prices", methods=["GET"])
@pricelist_bp.route("/prices/<int:identifier>", methods=["GET"])
def get_prices(identifier=None):
if identifier:
result = pricelist_controller.get_price(identifier)
else:
result = pricelist_controller.get_prices()
return jsonify(result)
@pricelist_bp.route("/volumes/<int:identifier>/prices", methods=["POST"])
2021-03-15 18:56:51 +00:00
def create_price(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_price(identifier, data))
@pricelist_bp.route("/prices/<int:identifier>", methods=["PUT"])
def modify_price(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_price(identifier, data))
@pricelist_bp.route("/prices/<int:identifier>", methods=["DELETE"])
def delete_price(identifier):
pricelist_controller.delete_price(identifier)
return "", NO_CONTENT
@pricelist_bp.route("/drinks/<int:identifier>/volumes", methods=["POST"])
def set_volume(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_volume(identifier, data))
@pricelist_bp.route("/volumes/<int:identifier>/ingredients", methods=["POST"])
def set_ingredient(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_ingredient(data, identifier))
@pricelist_bp.route("/volumes/<int:identifier>", methods=["PUT"])
def update_volume(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_volume(identifier, data))
@pricelist_bp.route("/volumes/<int:identifier>", methods=["DELETE"])
def delete_volume(identifier):
pricelist_controller.delete_volume(identifier)
return "", NO_CONTENT
@pricelist_bp.route("/ingredients/extraIngredients", methods=["GET"])
def get_extraIngredients():
return jsonify(pricelist_controller.get_extra_ingredients())
@pricelist_bp.route("/ingredients/<int:identifier>", methods=["PUT"])
def update_ingredient(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_ingredient(identifier, data))
@pricelist_bp.route("/ingredients/<int:identifier>", methods=["DELETE"])
def delete_ingredient(identifier):
pricelist_controller.delete_ingredient(identifier)
return "", NO_CONTENT