From dca890dad96e3625542a92f136023927db561f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Mon, 22 Mar 2021 23:17:44 +0100 Subject: [PATCH] [pricelist] save and load pictures --- flaschengeist/plugins/pricelist/models.py | 9 ++++++++ .../plugins/pricelist/pricelist_controller.py | 22 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/flaschengeist/plugins/pricelist/models.py b/flaschengeist/plugins/pricelist/models.py index b98acba..3d7c452 100644 --- a/flaschengeist/plugins/pricelist/models.py +++ b/flaschengeist/plugins/pricelist/models.py @@ -137,8 +137,17 @@ 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)) + _type_id = db.Column("type_id", db.Integer, db.ForeignKey("drink_type.id")) tags: Optional[list[Tag]] = db.relationship("Tag", secondary=drink_tag_association, cascade="save-update, merge") type: Optional[DrinkType] = db.relationship("DrinkType", foreign_keys=[_type_id]) volumes: list[DrinkPriceVolume] = db.relationship(DrinkPriceVolume) + + +class _Picture: + """Wrapper class for pictures binaries""" + + mimetype = "" + binary = bytearray() diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index e739db1..d2a88be 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -2,10 +2,13 @@ from werkzeug.exceptions import BadRequest, NotFound from sqlalchemy.exc import IntegrityError from flaschengeist import logger +from flaschengeist.config import config from flaschengeist.database import db from .models import Drink, DrinkPrice, Ingredient, Tag, DrinkType, DrinkPriceVolume, DrinkIngredient, ExtraIngredient -from math import ceil +from flaschengeist.utils.picture import save_picture, get_picture + +from uuid import uuid4 def update(): @@ -366,3 +369,20 @@ def delete_extra_ingredient(identifier): extra_ingredient = get_extra_ingredient(identifier) db.session.delete(extra_ingredient) db.session.commit() + + +def save_drink_picture(identifier, file): + drink = get_drink(identifier) + if not drink.uuid: + drink.uuid = str(uuid4()) + db.session.commit() + path = config["pricelist"]["path"] + save_picture(file, f"{path}/{drink.uuid}") + + +def get_drink_picture(identifier, size=None): + drink = get_drink(identifier) + if not drink.uuid: + raise BadRequest + path = config["pricelist"]["path"] + return get_picture(f"{path}/{drink.uuid}")