diff --git a/matemat/webserver/pagelets/admin.py b/matemat/webserver/pagelets/admin.py index 7d04afe..7e4f95e 100644 --- a/matemat/webserver/pagelets/admin.py +++ b/matemat/webserver/pagelets/admin.py @@ -1,6 +1,7 @@ from typing import Any, Dict, Union import os +from shutil import copyfile import magic from io import BytesIO from PIL import Image @@ -173,7 +174,18 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase, config: Dic is_member = 'ismember' in args is_admin = 'isadmin' in args # Create the user in the database - db.create_user(username, password, email, member=is_member, admin=is_admin) + newuser: User = db.create_user(username, password, email, member=is_member, admin=is_admin) + + # If a default avatar is set, copy it to the user's avatar path + + # Create the absolute path of the upload directory + abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/users/') + # Derive the individual paths + default: str = os.path.join(abspath, 'default.png') + userimg: str = os.path.join(abspath, f'{newuser.id}.png') + # Copy the default image, if it exists + if os.path.exists(default): + copyfile(default, userimg, follow_symlinks=True) # The user requested to create a new product elif change == 'newproduct': @@ -187,12 +199,9 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase, config: Dic # Create the user in the database newproduct = db.create_product(name, price_member, price_non_member) # If a new product image was uploaded, process it - if 'image' in args: + if 'image' in args and len(bytes(args.image)) > 0: # Read the raw image data from the request avatar = bytes(args.image) - # Only process the image, if its size is more than zero. Zero size means no new image was uploaded - if len(avatar) == 0: - return # Detect the MIME type filemagic: magic.FileMagic = magic.detect_from_content(avatar) if not filemagic.mime_type.startswith('image/'): @@ -209,6 +218,17 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase, config: Dic image.save(os.path.join(abspath, f'{newproduct.id}.png'), 'PNG') except OSError: return + else: + # If no image was uploaded and a default avatar is set, copy it to the product's avatar path + + # Create the absolute path of the upload directory + abspath: str = os.path.join(os.path.abspath(config['UploadDir']), 'thumbnails/products/') + # Derive the individual paths + default: str = os.path.join(abspath, 'default.png') + userimg: str = os.path.join(abspath, f'{newproduct.id}.png') + # Copy the default image, if it exists + if os.path.exists(default): + copyfile(default, userimg, follow_symlinks=True) # The user requested to restock a product elif change == 'restock': @@ -223,5 +243,33 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase, config: Dic # Write the new stock count to the database db.restock(product, amount) + # The user requested to set default images + elif change == 'defaultimg': + # Iterate the possible images to set + for category in 'users', 'products': + if category not in args: + continue + # Read the raw image data from the request + default: bytes = bytes(args[category]) + # Only process the image, if its size is more than zero. Zero size means no new image was uploaded + if len(default) == 0: + continue + # Detect the MIME type + filemagic: magic.FileMagic = magic.detect_from_content(default) + if not filemagic.mime_type.startswith('image/'): + continue + # Create the absolute path of the upload directory + abspath: str = os.path.join(os.path.abspath(config['UploadDir']), f'thumbnails/{category}/') + os.makedirs(abspath, exist_ok=True) + try: + # Parse the image data + image: Image = Image.open(BytesIO(default)) + # Resize the image to 150x150 + image.thumbnail((150, 150), Image.LANCZOS) + # Write the image to the file + image.save(os.path.join(abspath, f'default.png'), 'PNG') + except OSError: + return + except UnicodeDecodeError: raise ValueError('an argument not a string') diff --git a/templates/admin_restricted.html b/templates/admin_restricted.html index 99a205d..72a262c 100644 --- a/templates/admin_restricted.html +++ b/templates/admin_restricted.html @@ -87,4 +87,22 @@ - \ No newline at end of file + + +
+

Set default images

+ +
+
+
+ +
+
+ + +
+