[pricelist][drinks] return only public drinkprices if not logged in

This commit is contained in:
Tim Gröger 2021-03-28 16:41:20 +02:00
parent 2ae8bc7e0c
commit e8c9c6e66c
2 changed files with 42 additions and 13 deletions

View File

@ -4,8 +4,8 @@ from flask import Blueprint, jsonify, request
from http.client import NO_CONTENT from http.client import NO_CONTENT
from flaschengeist.plugins import Plugin from flaschengeist.plugins import Plugin
from flaschengeist.utils.decorators import login_required from flaschengeist.utils.decorators import login_required,extract_session
from werkzeug.exceptions import BadRequest, Forbidden from werkzeug.exceptions import BadRequest, Forbidden, Unauthorized
from flaschengeist.config import config from flaschengeist.config import config
from . import models from . import models
@ -103,12 +103,18 @@ def delete_tag(identifier, current_session):
@pricelist_bp.route("/drinks", methods=["GET"]) @pricelist_bp.route("/drinks", methods=["GET"])
@pricelist_bp.route("/drinks/<int:identifier>", methods=["GET"]) @pricelist_bp.route("/drinks/<int:identifier>", methods=["GET"])
def get_drinks(identifier=None): def get_drinks(identifier=None):
if identifier: public = True
result = pricelist_controller.get_drink(identifier) try:
else: extract_session()
result = pricelist_controller.get_drinks() public = False
return jsonify(result) except Unauthorized:
public = True
if identifier:
result = pricelist_controller.get_drink(identifier, public=public)
else:
result = pricelist_controller.get_drinks(public=public)
return jsonify(result)
@pricelist_bp.route("/drinks/search/<string:name>", methods=["GET"]) @pricelist_bp.route("/drinks/search/<string:name>", methods=["GET"])
def search_drinks(name): def search_drinks(name):

View File

@ -105,21 +105,44 @@ def delete_drink_type(identifier):
except IntegrityError: except IntegrityError:
raise BadRequest("DrinkType still in use") raise BadRequest("DrinkType still in use")
def _create_public_drink(drink):
_volumes = []
for volume in drink.volumes:
_prices = []
for price in volume.prices:
price: DrinkPrice
if price.public:
_prices.append(price)
volume.prices = _prices
if len(volume.prices) > 0:
_volumes.append(volume)
drink.volumes = _volumes
if len(drink.volumes) > 0:
return drink
return None
def get_drinks(name=None): def get_drinks(name=None, public=False):
if name: if name:
return Drink.query.filter(Drink.name.contains(name)).all() drinks = Drink.query.filter(Drink.name.contains(name)).all()
return Drink.query.all() drinks = Drink.query.all()
if public:
return [_create_public_drink(drink) for drink in drinks if _create_public_drink(drink)]
return drinks
def get_drink(identifier): def get_drink(identifier, public=False):
drink = None
if isinstance(identifier, int): if isinstance(identifier, int):
return Drink.query.get(identifier) drink = Drink.query.get(identifier)
elif isinstance(identifier, str): elif isinstance(identifier, str):
return Drink.query.filter(Tag.name == identifier).one_or_none() drink = Drink.query.filter(Tag.name == identifier).one_or_none()
else: else:
logger.debug("Invalid identifier type for Drink") logger.debug("Invalid identifier type for Drink")
raise BadRequest raise BadRequest
if drink:
if public:
return _create_public_drink(drink)
return drink
raise NotFound raise NotFound