[pricelist] delete picture (not ready yet)
This commit is contained in:
parent
114e12f151
commit
6dbb135621
|
@ -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/<int:identifier>/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()
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue