Implemented default images.
This commit is contained in:
parent
f2f8828db4
commit
4ecc848eac
2 changed files with 72 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
from typing import Any, Dict, Union
|
from typing import Any, Dict, Union
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from shutil import copyfile
|
||||||
import magic
|
import magic
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
@ -173,7 +174,18 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase, config: Dic
|
||||||
is_member = 'ismember' in args
|
is_member = 'ismember' in args
|
||||||
is_admin = 'isadmin' in args
|
is_admin = 'isadmin' in args
|
||||||
# Create the user in the database
|
# 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
|
# The user requested to create a new product
|
||||||
elif change == 'newproduct':
|
elif change == 'newproduct':
|
||||||
|
@ -187,12 +199,9 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase, config: Dic
|
||||||
# Create the user in the database
|
# Create the user in the database
|
||||||
newproduct = db.create_product(name, price_member, price_non_member)
|
newproduct = db.create_product(name, price_member, price_non_member)
|
||||||
# If a new product image was uploaded, process it
|
# 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
|
# Read the raw image data from the request
|
||||||
avatar = bytes(args.image)
|
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
|
# Detect the MIME type
|
||||||
filemagic: magic.FileMagic = magic.detect_from_content(avatar)
|
filemagic: magic.FileMagic = magic.detect_from_content(avatar)
|
||||||
if not filemagic.mime_type.startswith('image/'):
|
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')
|
image.save(os.path.join(abspath, f'{newproduct.id}.png'), 'PNG')
|
||||||
except OSError:
|
except OSError:
|
||||||
return
|
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
|
# The user requested to restock a product
|
||||||
elif change == 'restock':
|
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
|
# Write the new stock count to the database
|
||||||
db.restock(product, amount)
|
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:
|
except UnicodeDecodeError:
|
||||||
raise ValueError('an argument not a string')
|
raise ValueError('an argument not a string')
|
||||||
|
|
|
@ -88,3 +88,21 @@
|
||||||
<input type="submit" value="Go">
|
<input type="submit" value="Go">
|
||||||
</form>
|
</form>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
<section id="admin-restricted-default-images">
|
||||||
|
<h2>Set default images</h2>
|
||||||
|
|
||||||
|
<form id="admin-default-images-form" method="post" action="/admin?adminchange=defaultimg" enctype="multipart/form-data" accept-charset="UTF-8">
|
||||||
|
<label for="admin-default-images-user">
|
||||||
|
<img src="/upload/thumbnails/users/default.png" alt="Default user avatar" />
|
||||||
|
</label><br/>
|
||||||
|
<input id="admin-default-images-user" type="file" name="users" accept="image/*" /><br/>
|
||||||
|
|
||||||
|
<label for="admin-default-images-product">
|
||||||
|
<img src="/upload/thumbnails/products/default.png" alt="Default product avatar" />
|
||||||
|
</label><br/>
|
||||||
|
<input id="admin-default-images-product" type="file" name="products" accept="image/*" /><br/>
|
||||||
|
|
||||||
|
<input type="submit" value="Save changes">
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
|
Loading…
Reference in a new issue