From 6dbb135621ab69b4a6a6558fca3efb1ce415b249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Thu, 25 Mar 2021 20:32:21 +0100 Subject: [PATCH] [pricelist] delete picture (not ready yet) --- flaschengeist/plugins/pricelist/__init__.py | 28 +++++++++++++++- .../plugins/pricelist/pricelist_controller.py | 12 +++++-- flaschengeist/utils/picture.py | 32 +++++++++++++------ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/flaschengeist/plugins/pricelist/__init__.py b/flaschengeist/plugins/pricelist/__init__.py index 5a8a831..d53dd30 100644 --- a/flaschengeist/plugins/pricelist/__init__.py +++ b/flaschengeist/plugins/pricelist/__init__.py @@ -11,7 +11,7 @@ from . import models from . import pricelist_controller, permissions from ...controller import userController from ...models.session import Session -from ...utils.HTTP import no_content +from ...utils.HTTP import no_content, created pricelist_bp = Blueprint("pricelist", __name__, url_prefix="/pricelist") @@ -217,3 +217,29 @@ def get_columns(userid, current_session: Session): user.set_attribute("pricecalc_columns", data) userController.persist() return no_content() + +@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() + + file = request.files.get("file") + if file: + picture = models._Picture() + picture.mimetype = file.content_type + picture.binary = bytearray(file.stream.read()) + pricelist_controller.save_drink_picture(identifier, picture) + else: + raise BadRequest + + return created() \ No newline at end of file diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index d2a88be..e2c8469 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 +from flaschengeist.utils.picture import save_picture, get_picture, delete_picture from uuid import uuid4 @@ -383,6 +383,14 @@ def save_drink_picture(identifier, file): def get_drink_picture(identifier, size=None): drink = get_drink(identifier) if not drink.uuid: - raise BadRequest + raise FileNotFoundError path = config["pricelist"]["path"] return get_picture(f"{path}/{drink.uuid}") + +def delete_drink_picture(identifier): + drink = get_drink(identifier) + if not drink.uuid: + delete_picture(f"{config['pricelist']['path']}/{drink.uuid}") + drink.uuid = None + db.session.commit() + diff --git a/flaschengeist/utils/picture.py b/flaschengeist/utils/picture.py index 968e764..529d322 100644 --- a/flaschengeist/utils/picture.py +++ b/flaschengeist/utils/picture.py @@ -2,6 +2,7 @@ import os, sys from PIL import Image from flask import Response from werkzeug.exceptions import BadRequest +from ..utils.HTTP import no_content thumbnail_sizes = ((32, 32), (64, 64), (128, 128), (256, 256), (512, 512)) @@ -27,12 +28,25 @@ def save_picture(picture, path): def get_picture(path, size=None): - if size: - with open(f"{path}/drink-{size}.png", "rb") as file: - image = file.read() - else: - with open(f"{path}/drink.png", "rb") as file: - image = file.read() - response = Response(image, mimetype="image/png") - response.add_etag() - return response + try: + if size: + if os.path.isfile(f"{path}/drink-{size}.png"): + with open(f"{path}/drink-{size}.png", "rb") as file: + image = file.read() + else: + _image = Image.open(f"{path}/drink.png") + _image.thumbnail((size, size)) + image = bytearray() + _image.save(bytearray, format='PNG') + else: + with open(f"{path}/drink.png", "rb") as file: + image = file.read() + response = Response(image, mimetype="image/png") + response.add_etag() + return response + except: + raise FileNotFoundError + +def delete_picture(path): + os.remove(path) +