From 5b8d4642cabbfa939d24d4e93e2d3b4de1413bb3 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Wed, 7 Aug 2024 20:00:55 +1200 Subject: [PATCH] svg logo #282 --- app/admin/forms.py | 5 ++--- app/admin/routes.py | 48 ++++++++++++++++++++++------------------- app/templates/base.html | 2 +- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/app/admin/forms.py b/app/admin/forms.py index e85add63..1a1d649d 100644 --- a/app/admin/forms.py +++ b/app/admin/forms.py @@ -13,9 +13,8 @@ from app.models import Community, User class SiteProfileForm(FlaskForm): name = StringField(_l('Site Name')) description = StringField(_l('Tagline')) - icon = FileField(_l('Icon'), validators=[ - FileAllowed(['jpg', 'jpeg', 'png', 'webp'], 'Images only!') - ], render_kw={'accept': 'image/*'}) + icon = FileField(_l('Icon'), validators=[FileAllowed(['jpg', 'jpeg', 'png', 'webp', 'svg'], 'Images only!')], + render_kw={'accept': 'image/*'}) sidebar = TextAreaField(_l('Sidebar')) about = TextAreaField(_l('About')) announcement = TextAreaField(_l('Announcement at top of home page')) diff --git a/app/admin/routes.py b/app/admin/routes.py index 164e8833..5dd8e4e3 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -60,7 +60,7 @@ def admin_site(): # Save site icon uploaded_icon = request.files['icon'] if uploaded_icon and uploaded_icon.filename != '': - allowed_extensions = ['.gif', '.jpg', '.jpeg', '.png', '.webp'] + allowed_extensions = ['.gif', '.jpg', '.jpeg', '.png', '.webp', '.svg'] file_ext = os.path.splitext(uploaded_icon.filename)[1] if file_ext.lower() not in allowed_extensions: abort(400) @@ -80,31 +80,35 @@ def admin_site(): # Save logo file base_filename = f'logo_{gibberish(5)}' uploaded_icon.save(f'{directory}/{base_filename}{file_ext}') - img = Image.open(f'{directory}/{base_filename}{file_ext}') - if img.width > 100: - img.thumbnail((100, 100)) - img.save(f'{directory}/{base_filename}_100{file_ext}') - site.logo = f'/static/media/{base_filename}_100{file_ext}' - delete_original = True - else: - site.logo = f'/static/media/{base_filename}{file_ext}' + if file_ext == '.svg': delete_original = False + site.logo = site.logo_152 = site.logo_32 = site.logo_16 = f'/static/media/{base_filename}{file_ext}' + else: + img = Image.open(f'{directory}/{base_filename}{file_ext}') + if img.width > 100: + img.thumbnail((100, 100)) + img.save(f'{directory}/{base_filename}_100{file_ext}') + site.logo = f'/static/media/{base_filename}_100{file_ext}' + delete_original = True + else: + site.logo = f'/static/media/{base_filename}{file_ext}' + delete_original = False - # Save multiple copies of the logo - different sizes - img = Image.open(f'{directory}/{base_filename}{file_ext}') - img.thumbnail((152, 152)) - img.save(f'{directory}/{base_filename}_152{file_ext}') - site.logo_152 = f'/static/media/{base_filename}_152{file_ext}' + # Save multiple copies of the logo - different sizes + img = Image.open(f'{directory}/{base_filename}{file_ext}') + img.thumbnail((152, 152)) + img.save(f'{directory}/{base_filename}_152{file_ext}') + site.logo_152 = f'/static/media/{base_filename}_152{file_ext}' - img = Image.open(f'{directory}/{base_filename}{file_ext}') - img.thumbnail((32, 32)) - img.save(f'{directory}/{base_filename}_32{file_ext}') - site.logo_32 = f'/static/media/{base_filename}_32{file_ext}' + img = Image.open(f'{directory}/{base_filename}{file_ext}') + img.thumbnail((32, 32)) + img.save(f'{directory}/{base_filename}_32{file_ext}') + site.logo_32 = f'/static/media/{base_filename}_32{file_ext}' - img = Image.open(f'{directory}/{base_filename}{file_ext}') - img.thumbnail((16, 16)) - img.save(f'{directory}/{base_filename}_16{file_ext}') - site.logo_16 = f'/static/media/{base_filename}_16{file_ext}' + img = Image.open(f'{directory}/{base_filename}{file_ext}') + img.thumbnail((16, 16)) + img.save(f'{directory}/{base_filename}_16{file_ext}') + site.logo_16 = f'/static/media/{base_filename}_16{file_ext}' if delete_original: os.unlink(f'app/static/media/{base_filename}{file_ext}') diff --git a/app/templates/base.html b/app/templates/base.html index c5c42b8b..442cf6ec 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -102,7 +102,7 @@ {% block navbar -%}