Skip to content

Commit

Permalink
Fix favicon loading
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelm committed Dec 8, 2023
1 parent 7a8b3d0 commit 40cdb0c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/pretix/_base_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@
FILE_UPLOAD_EXTENSIONS_IMAGE = (".png", ".jpg", ".gif", ".jpeg")
PILLOW_FORMATS_IMAGE = ('PNG', 'GIF', 'JPEG')

FILE_UPLOAD_EXTENSIONS_FAVICON = (".ico", ".png", "jpg", ".gif", ".jpeg")
FILE_UPLOAD_EXTENSIONS_FAVICON = (".ico", ".png", ".jpg", ".gif", ".jpeg")
PILLOW_FORMATS_QUESTIONS_FAVICON = ('PNG', 'GIF', 'JPEG', 'ICO')

FILE_UPLOAD_EXTENSIONS_QUESTION_IMAGE = (".png", "jpg", ".gif", ".jpeg", ".bmp", ".tif", ".tiff", ".jfif")
FILE_UPLOAD_EXTENSIONS_QUESTION_IMAGE = (".png", ".jpg", ".gif", ".jpeg", ".bmp", ".tif", ".tiff", ".jfif")
PILLOW_FORMATS_QUESTIONS_IMAGE = ('PNG', 'GIF', 'JPEG', 'BMP', 'TIFF')

FILE_UPLOAD_EXTENSIONS_EMAIL_ATTACHMENT = (
Expand Down
19 changes: 19 additions & 0 deletions src/pretix/base/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2910,6 +2910,25 @@ def unserialize(cls, s):
label=_('Use header image also for events without an individually uploaded logo'),
)
},
'favicon': {
'default': None,
'type': File,
'form_class': ExtFileField,
'form_kwargs': dict(
label=_('Favicon'),
ext_whitelist=settings.FILE_UPLOAD_EXTENSIONS_FAVICON,
max_size=settings.FILE_UPLOAD_MAX_SIZE_FAVICON,
help_text=_('If you provide a favicon, we will show it instead of the default pretix icon. '
'We recommend a size of at least 200x200px to accommodate most devices.')
),
'serializer_class': UploadedFileField,
'serializer_kwargs': dict(
allowed_types=[
'image/png', 'image/jpeg', 'image/gif', 'image/x-icon', 'image/vnd.microsoft.icon',
],
max_size=settings.FILE_UPLOAD_MAX_SIZE_FAVICON,
)
},
'og_image': {
'default': None,
'type': File,
Expand Down
9 changes: 1 addition & 8 deletions src/pretix/control/forms/organizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ class OrganizerSettingsForm(SettingsForm):
'organizer_link_back',
'organizer_logo_image_large',
'organizer_logo_image_inherit',
'favicon',
'giftcard_length',
'giftcard_expiry_years',
'locales',
Expand Down Expand Up @@ -464,14 +465,6 @@ class OrganizerSettingsForm(SettingsForm):
'can increase the size with the setting below. We recommend not using small details on the picture '
'as it will be resized on smaller screens.')
)
favicon = ExtFileField(
label=_('Favicon'),
ext_whitelist=settings.FILE_UPLOAD_EXTENSIONS_FAVICON,
required=False,
max_size=settings.FILE_UPLOAD_MAX_SIZE_FAVICON,
help_text=_('If you provide a favicon, we will show it instead of the default pretix icon. '
'We recommend a size of at least 200x200px to accommodate most devices.')
)

def __init__(self, *args, **kwargs):
is_admin = kwargs.pop('is_admin', False)
Expand Down
8 changes: 4 additions & 4 deletions src/pretix/helpers/thumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ def resize_image(image, size):
return image


def create_thumbnail(sourcename, size):
def create_thumbnail(sourcename, size, formats=None):
source = default_storage.open(sourcename)
image = Image.open(BytesIO(source.read()), formats=settings.PILLOW_FORMATS_QUESTIONS_IMAGE)
image = Image.open(BytesIO(source.read()), formats=formats or settings.PILLOW_FORMATS_QUESTIONS_IMAGE)
try:
image.load()
except:
Expand Down Expand Up @@ -208,9 +208,9 @@ def create_thumbnail(sourcename, size):
return t


def get_thumbnail(source, size):
def get_thumbnail(source, size, formats=None):
# Assumes files are immutable
try:
return Thumbnail.objects.get(source=source, size=size)
except Thumbnail.DoesNotExist:
return create_thumbnail(source, size)
return create_thumbnail(source, size, formats=formats)
5 changes: 3 additions & 2 deletions src/pretix/presale/views/organizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,8 @@ def get(self, request, *args, **kwargs):

class OrganizerFavicon(View):
def get(self, *args, **kwargs):
if self.request.organizer.settings.favicon:
return redirect_to_url(get_thumbnail(self.request.organizer.settings.favicon, '32x32^').thumb.url)
icon_file = self.request.organizer.settings.get('favicon', as_type=str, default='')[7:]
if icon_file:
return redirect_to_url(get_thumbnail(icon_file, '32x32^', formats=settings.PILLOW_FORMATS_QUESTIONS_FAVICON).thumb.url)
else:
return redirect_to_url(static("pretixbase/img/favicon.ico"))

0 comments on commit 40cdb0c

Please sign in to comment.