Limit image file size

This commit is contained in:
Gabriel Augendre 2022-12-09 19:00:20 +01:00
parent c8d369134a
commit 5ff9742a60

View file

@ -1,8 +1,10 @@
import collections import collections
from collections.abc import Iterable from collections.abc import Iterable
from dataclasses import dataclass from dataclasses import dataclass
from functools import partial
import markdown import markdown
from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.db.models import Q from django.db.models import Q
from django.db.models.functions import Lower from django.db.models.functions import Lower
@ -121,6 +123,12 @@ class CharacterCapability:
known: bool = False known: bool = False
def validate_image(fieldfile_obj, megabytes_limit: float):
filesize = fieldfile_obj.file.size
if filesize > megabytes_limit * 1024 * 1024:
raise ValidationError("La taille maximale est de %sMo" % str(megabytes_limit))
class Character(models.Model): class Character(models.Model):
class Gender(models.TextChoices): class Gender(models.TextChoices):
MALE = "M", "Mâle" MALE = "M", "Mâle"
@ -139,6 +147,7 @@ class Character(models.Model):
upload_to="profile_pictures", upload_to="profile_pictures",
blank=True, blank=True,
null=True, null=True,
validators=[partial(validate_image, megabytes_limit=2)],
) )
race = models.ForeignKey( race = models.ForeignKey(