[pricelist] save and load pictures

This commit is contained in:
Tim Gröger 2021-03-22 23:17:44 +01:00
parent eb1e146da9
commit dca890dad9
2 changed files with 30 additions and 1 deletions

View File

@ -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_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)) 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")) _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") 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]) type: Optional[DrinkType] = db.relationship("DrinkType", foreign_keys=[_type_id])
volumes: list[DrinkPriceVolume] = db.relationship(DrinkPriceVolume) volumes: list[DrinkPriceVolume] = db.relationship(DrinkPriceVolume)
class _Picture:
"""Wrapper class for pictures binaries"""
mimetype = ""
binary = bytearray()

View File

@ -2,10 +2,13 @@ from werkzeug.exceptions import BadRequest, NotFound
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from flaschengeist import logger from flaschengeist import logger
from flaschengeist.config import config
from flaschengeist.database import db from flaschengeist.database import db
from .models import Drink, DrinkPrice, Ingredient, Tag, DrinkType, DrinkPriceVolume, DrinkIngredient, ExtraIngredient 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(): def update():
@ -366,3 +369,20 @@ def delete_extra_ingredient(identifier):
extra_ingredient = get_extra_ingredient(identifier) extra_ingredient = get_extra_ingredient(identifier)
db.session.delete(extra_ingredient) db.session.delete(extra_ingredient)
db.session.commit() 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}")