From 418d7ffea99d464678d698f6415477880de5638b Mon Sep 17 00:00:00 2001 From: s3lph Date: Wed, 11 Dec 2024 00:04:36 +0100 Subject: [PATCH] feat: load default thumbnail on fetch rather than copying default thumbnail to user/product on creation --- matemat/__main__.py | 11 +++++++++++ matemat/webserver/pagelets/admin.py | 22 ---------------------- matemat/webserver/pagelets/signup.py | 11 +---------- templates/admin.html | 4 ++-- 4 files changed, 14 insertions(+), 34 deletions(-) diff --git a/matemat/__main__.py b/matemat/__main__.py index c9ef2ea..bcfd877 100644 --- a/matemat/__main__.py +++ b/matemat/__main__.py @@ -61,6 +61,17 @@ def _init(config: Dict[str, Any]): template_init(config) +@bottle.route('/static/upload/thumbnails//') +def get_thumbnail(resource: str, filename: str): + config = get_config() + static: str = os.path.abspath(config['staticroot']) + rpath: str = os.path.join(static, 'upload/thumbnails', resource) + resp = bottle.static_file(filename, root=rpath) + if resp.status_code != 200: + resp = bottle.static_file('default.png', root=rpath) + return resp + + @bottle.route('/static/') def serve_static_files(filename: str): config = get_config() diff --git a/matemat/webserver/pagelets/admin.py b/matemat/webserver/pagelets/admin.py index cc440ac..7c856f2 100644 --- a/matemat/webserver/pagelets/admin.py +++ b/matemat/webserver/pagelets/admin.py @@ -84,17 +84,6 @@ def handle_change(args: FormsDict, files: FormsDict, db: MatematDatabase): newuser: User = db.create_user(username, password, email, member=is_member, admin=is_admin, logout_after_purchase=logout_after_purchase) - # 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': # Only create a new product if all required properties of the product are present in the request arguments @@ -130,17 +119,6 @@ def handle_change(args: FormsDict, files: FormsDict, db: MatematDatabase): except OSError as e: Notification.error(str(e), decay=True) 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': diff --git a/matemat/webserver/pagelets/signup.py b/matemat/webserver/pagelets/signup.py index a5ab403..5f4a102 100644 --- a/matemat/webserver/pagelets/signup.py +++ b/matemat/webserver/pagelets/signup.py @@ -48,16 +48,7 @@ def signup_user(args: FormsDict, files: FormsDict, db: MatematDatabase) -> User: image.thumbnail((150, 150), Image.LANCZOS) # Write the image to the file image.save(os.path.join(abspath, f'{new_user.id}.png'), 'PNG') - else: - # 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'{new_user.id}.png') - # Copy the default image, if it exists - if os.path.exists(default): - copyfile(default, userimg, follow_symlinks=True) + return new_user diff --git a/templates/admin.html b/templates/admin.html index 71aa309..2472676 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -134,7 +134,7 @@
- Default user avatar + Default user avatar
@@ -146,7 +146,7 @@
- Default product image + Default product image