[pricelist] now can delete pictures, add no-image

This commit is contained in:
Tim Gröger 2021-03-25 23:05:17 +01:00
parent 6dbb135621
commit 6fce88c120
3 changed files with 16 additions and 8 deletions

View File

@ -6,7 +6,7 @@ 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 flaschengeist.utils.picture import save_picture, get_picture, delete_picture from flaschengeist.utils.picture import save_picture, get_picture, delete_picture, get_no_image
from uuid import uuid4 from uuid import uuid4
@ -383,13 +383,13 @@ def save_drink_picture(identifier, file):
def get_drink_picture(identifier, size=None): def get_drink_picture(identifier, size=None):
drink = get_drink(identifier) drink = get_drink(identifier)
if not drink.uuid: if not drink.uuid:
raise FileNotFoundError return get_no_image()
path = config["pricelist"]["path"] path = config["pricelist"]["path"]
return get_picture(f"{path}/{drink.uuid}") return get_picture(f"{path}/{drink.uuid}")
def delete_drink_picture(identifier): def delete_drink_picture(identifier):
drink = get_drink(identifier) drink = get_drink(identifier)
if not drink.uuid: if drink.uuid:
delete_picture(f"{config['pricelist']['path']}/{drink.uuid}") delete_picture(f"{config['pricelist']['path']}/{drink.uuid}")
drink.uuid = None drink.uuid = None
db.session.commit() db.session.commit()

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

View File

@ -1,4 +1,4 @@
import os, sys import os, sys, shutil
from PIL import Image from PIL import Image
from flask import Response from flask import Response
from werkzeug.exceptions import BadRequest from werkzeug.exceptions import BadRequest
@ -20,12 +20,18 @@ def save_picture(picture, path):
if file_type != "png": if file_type != "png":
image.save(f"{filename}.png", "PNG") image.save(f"{filename}.png", "PNG")
os.remove(f"{filename}.{file_type}") os.remove(f"{filename}.{file_type}")
image.show()
for thumbnail_size in thumbnail_sizes: for thumbnail_size in thumbnail_sizes:
work_image = image.copy() work_image = image.copy()
work_image.thumbnail(thumbnail_size) work_image.thumbnail(thumbnail_size)
work_image.save(f"{filename}-{thumbnail_size[0]}.png", "PNG") 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): def get_picture(path, size=None):
try: try:
@ -45,8 +51,10 @@ def get_picture(path, size=None):
response.add_etag() response.add_etag()
return response return response
except: except:
raise FileNotFoundError get_no_image()
def delete_picture(path): def delete_picture(path):
os.remove(path) shutil.rmtree(path)