From 2ae8bc7e0c9e06d055c98b24cf427187543f853f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Sun, 28 Mar 2021 12:47:02 +0200 Subject: [PATCH] [pricelist][picture] delete picture, if not found --- flaschengeist/plugins/pricelist/__init__.py | 21 +++++++------- flaschengeist/plugins/pricelist/models.py | 2 +- .../plugins/pricelist/pricelist_controller.py | 29 ++++++++++++++----- flaschengeist/utils/picture.py | 15 ++++------ 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index d53dd30..c20e564 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -6,12 +6,13 @@ 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.config import config from . import models from . import pricelist_controller, permissions from ...controller import userController from ...models.session import Session -from ...utils.HTTP import no_content, created +from ...utils.HTTP import no_content pricelist_bp = Blueprint("pricelist", __name__, url_prefix="/pricelist") @@ -221,14 +222,6 @@ def get_columns(userid, current_session: Session): @pricelist_bp.route("/drinks//picture", methods=["POST", "GET", "DELETE"]) def set_picture(identifier): - if request.method == "GET": - try: - size = request.args.get("size") - response = pricelist_controller.get_drink_picture(identifier, size) - return response.make_conditional(request) - except FileNotFoundError: - return no_content() - if request.method == "DELETE": pricelist_controller.delete_drink_picture(identifier) return no_content() @@ -238,8 +231,14 @@ def set_picture(identifier): picture = models._Picture() picture.mimetype = file.content_type picture.binary = bytearray(file.stream.read()) - pricelist_controller.save_drink_picture(identifier, picture) + return jsonify(pricelist_controller.save_drink_picture(identifier, picture)) else: raise BadRequest - return created() \ No newline at end of file +@pricelist_bp.route("/picture/", methods=["GET"]) +def _get_picture(identifier): + if request.method == "GET": + size = request.args.get("size") + path = config["pricelist"]["path"] + response = pricelist_controller.get_drink_picture(identifier, size) + return response.make_conditional(request) \ No newline at end of file diff --git a/flaschengeist/plugins/pricelist/models.py b/flaschengeist/plugins/pricelist/models.py index 3d7c452..ff3fb72 100644 --- a/flaschengeist/plugins/pricelist/models.py +++ b/flaschengeist/plugins/pricelist/models.py @@ -137,7 +137,7 @@ class Drink(db.Model, ModelSerializeMixin): cost_price_pro_volume: Optional[float] = db.Column(db.Numeric(precision=5, scale=3, asdecimal=False)) cost_price_package_netto: Optional[float] = db.Column(db.Numeric(precision=5, scale=3, asdecimal=False)) - uuid = db.Column(db.String(36)) + uuid: str = db.Column(db.String(36)) _type_id = db.Column("type_id", db.Integer, db.ForeignKey("drink_type.id")) diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index 71f9488..0ce42dd 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -6,7 +6,7 @@ from flaschengeist.config import config from flaschengeist.database import db from .models import Drink, DrinkPrice, Ingredient, Tag, DrinkType, DrinkPriceVolume, DrinkIngredient, ExtraIngredient -from flaschengeist.utils.picture import save_picture, get_picture, delete_picture, get_no_image +from flaschengeist.utils.picture import save_picture, get_picture, delete_picture from uuid import uuid4 @@ -373,19 +373,32 @@ def delete_extra_ingredient(identifier): def save_drink_picture(identifier, file): drink = get_drink(identifier) - if not drink.uuid: - drink.uuid = str(uuid4()) - db.session.commit() + old_uuid = None + if drink.uuid: + old_uuid = drink.uuid + drink.uuid = str(uuid4()) + db.session.commit() path = config["pricelist"]["path"] save_picture(file, f"{path}/{drink.uuid}") + if old_uuid: + delete_picture(f"{path}/{old_uuid}") + return drink def get_drink_picture(identifier, size=None): - drink = get_drink(identifier) - if not drink.uuid: - return get_no_image() path = config["pricelist"]["path"] - return get_picture(f"{path}/{drink.uuid}") + drink = None + if isinstance(identifier, int): + drink = get_drink(identifier) + if isinstance(identifier, str): + drink = Drink.query.filter(Drink.uuid==identifier).one_or_none() + try: + if drink: + return get_picture(f"{path}/{drink.uuid}", size) + except FileNotFoundError: + drink.uuid = None + db.session.commit() + raise FileNotFoundError def delete_drink_picture(identifier): drink = get_drink(identifier) diff --git a/flaschengeist/utils/picture.py b/flaschengeist/utils/picture.py index 89de66f..dc896f5 100644 --- a/flaschengeist/utils/picture.py +++ b/flaschengeist/utils/picture.py @@ -25,14 +25,6 @@ def save_picture(picture, path): work_image.thumbnail(thumbnail_size) work_image.save(f"{filename}-{thumbnail_size[0]}.png", "PNG") -def get_no_image(): - path = os.path.dirname(__file__) - with open(f"{path}/no-image.png", "rb") as file: - image = file.read() - response = Response(image, mimetype="image/png") - response.add_etag() - return response - def get_picture(path, size=None): try: if size: @@ -51,10 +43,13 @@ def get_picture(path, size=None): response.add_etag() return response except: - get_no_image() + raise FileNotFoundError def delete_picture(path): - shutil.rmtree(path) + try: + shutil.rmtree(path) + except FileNotFoundError: + pass