Fix emoji file size validation and help text and add image size validation (#410)

This commit is contained in:
TAKAHASHI Shuuji 2023-01-15 02:33:36 +09:00 committed by GitHub
parent 3679448cdf
commit 71ab6b98a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -49,13 +49,19 @@ class EmojiCreate(FormView):
help_text="What users type to use the emoji :likethis:", help_text="What users type to use the emoji :likethis:",
) )
image = forms.ImageField( image = forms.ImageField(
help_text="The emoji image\nShould be at least 40 x 40 pixels, and under 50kb", help_text=f"The emoji image\nShould be at least 40 x 40 pixels, and under {settings.SETUP.EMOJI_MAX_IMAGE_FILESIZE_KB}KB",
) )
def clean_image(self): def clean_image(self):
data = self.cleaned_data["image"] data = self.cleaned_data["image"]
if data.size > settings.SETUP.EMOJI_MAX_IMAGE_FILESIZE_KB: if data.size > settings.SETUP.EMOJI_MAX_IMAGE_FILESIZE_KB * 1024:
raise forms.ValidationError("Image filesize is too large") raise forms.ValidationError(
f"Image filesize is too large (actual: {data.size // 1024}KB)"
)
if data.image.width < 40 or data.image.height < 40:
raise forms.ValidationError(
f"Image should at least 40 x 40 pixels (actual: {data.image.width} x {data.image.height} pixels)"
)
return data return data
def form_valid(self, form): def form_valid(self, form):