Skip to content

Commit

Permalink
Merge pull request #1000 from ae-utbm/imghdr
Browse files Browse the repository at this point in the history
Remove call to deprecated `imghdr` module
  • Loading branch information
klmp200 authored Jan 14, 2025
2 parents 8094076 + c6bb509 commit 61170c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 7 additions & 11 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import string
import unicodedata
from datetime import timedelta
from io import BytesIO
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Self

Expand All @@ -50,6 +51,7 @@
from django.utils.timezone import localdate, now
from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField
from PIL import Image

if TYPE_CHECKING:
from pydantic import NonNegativeInt
Expand Down Expand Up @@ -988,17 +990,11 @@ def clean(self):
if self.is_folder:
if self.file:
try:
import imghdr

if imghdr.what(None, self.file.read()) not in [
"gif",
"png",
"jpeg",
]:
self.file.delete()
self.file = None
except: # noqa E722 I don't know the exception that can be raised
self.file = None
Image.open(BytesIO(self.file.read()))
except Image.UnidentifiedImageError as e:
raise ValidationError(
_("This is not a valid folder thumbnail")
) from e
self.mime_type = "inode/directory"
if self.is_file and (self.file is None or self.file == ""):
raise ValidationError(_("You must provide a file"))
Expand Down
4 changes: 4 additions & 0 deletions locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,10 @@ msgstr ""
msgid "Duplicate file"
msgstr "Un fichier de ce nom existe déjà"

#: core/models.py
msgid "This is not a valid folder thumbnail"
msgstr "Ceci n'est pas une miniature de dossier valide"

#: core/models.py
msgid "You must provide a file"
msgstr "Vous devez fournir un fichier"
Expand Down

0 comments on commit 61170c0

Please sign in to comment.