[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 flaschengeist.plugins import Plugin
from flaschengeist.utils.decorators import login_required
from werkzeug.exceptions import BadRequest, Forbidden
from flaschengeist.utils.decorators import login_required,extract_session
from werkzeug.exceptions import BadRequest, Forbidden, Unauthorized
from flaschengeist.config import config
from . import models
@ -103,12 +103,18 @@ def delete_tag(identifier, current_session):
@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)
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)
return jsonify(result)
@pricelist_bp.route("/drinks/search/<string:name>", methods=["GET"])
def search_drinks(name):

View File

@ -105,21 +105,44 @@ def delete_drink_type(identifier):
except IntegrityError:
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:
return Drink.query.filter(Drink.name.contains(name)).all()
return Drink.query.all()
drinks = Drink.query.filter(Drink.name.contains(name)).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):
return Drink.query.get(identifier)
drink = Drink.query.get(identifier)
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:
logger.debug("Invalid identifier type for Drink")
raise BadRequest
if drink:
if public:
return _create_public_drink(drink)
return drink
raise NotFound