Skip to content

Commit

Permalink
Add a new field to convert images to webp
Browse files Browse the repository at this point in the history
  • Loading branch information
KwikKill committed Oct 4, 2024
1 parent 2d84dd6 commit 07a164f
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
33 changes: 33 additions & 0 deletions insalan/components/image_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
from io import BytesIO

from django.core.exceptions import ValidationError
from django.db import models
from PIL import Image

class ImageField(models.ImageField):
"""
A subclass of Django's ImageField that convert the image to a webp format
"""

def __init__(self, *args, **kwargs):
self.convert_to_webp = kwargs.pop("convert_to_webp", True)
super().__init__(*args, **kwargs)

def pre_save(self, model_instance, add):
file = super().pre_save(model_instance, add)
if self.convert_to_webp:
try:
img = Image.open(file)
if img.format != "WEBP":
buffer = BytesIO()
img.save(buffer, format="WEBP")
buffer.seek(0)
file.save(
os.path.basename(os.path.splitext(file.name)[0]) + ".webp",
buffer,
save=False,
)
except Exception as e:
raise ValidationError(f"Invalid image file: {e}") from e
return file
4 changes: 3 additions & 1 deletion insalan/partner/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from insalan.components.image_field import ImageField

class Partner(models.Model):
class PartnerType(models.TextChoices):
"""There are two types of sponsors"""
Expand All @@ -22,7 +24,7 @@ class Meta:
max_length=200, verbose_name=_("Nom du partenaire/sponsor")
)
url: models.URLField = models.URLField(verbose_name=_("URL"))
logo: models.FileField = models.FileField(
logo: models.FileField = ImageField(
verbose_name=_("Logo"),
upload_to="partners",
validators=[
Expand Down
4 changes: 3 additions & 1 deletion insalan/pizza/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from django.utils.translation import gettext_lazy as _
from django.contrib.postgres.fields import ArrayField

from insalan.components.image_field import ImageField

class PaymentMethod(models.TextChoices):
"""
Payment method choices
Expand Down Expand Up @@ -52,7 +54,7 @@ class Meta:
blank=True,
null=True,
)
image: models.FileField = models.FileField(
image: models.FileField = ImageField(
verbose_name=_("Image"),
upload_to="pizzas",
validators=[
Expand Down
4 changes: 3 additions & 1 deletion insalan/tournament/models/caster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.core.validators import FileExtensionValidator
from django.utils.translation import gettext_lazy as _

from insalan.components.image_field import ImageField

class Caster(models.Model):
"""
A Caster is someone that can cast a tournament.
Expand All @@ -12,7 +14,7 @@ class Caster(models.Model):
blank=False,
verbose_name=_("Nom du casteur")
)
image = models.FileField(
image = ImageField(
verbose_name=_("Photo de profil"),
blank=True,
null=True,
Expand Down
4 changes: 3 additions & 1 deletion insalan/tournament/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from . import tournament

from insalan.components.image_field import ImageField

class Event(models.Model):
"""
An Event is any single event that is characterized by the following:
Expand Down Expand Up @@ -41,7 +43,7 @@ class Event(models.Model):
validators=[MinValueValidator(1), MaxValueValidator(12)],
)
ongoing = models.BooleanField(verbose_name=_("En cours"), default=False)
logo: models.FileField = models.FileField(
logo: models.FileField = ImageField(
verbose_name=_("Logo"),
blank=True,
null=True,
Expand Down
3 changes: 2 additions & 1 deletion insalan/tournament/models/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.contrib.postgres.fields import ArrayField

from . import team, caster, group, bracket, swiss
from insalan.components.image_field import ImageField

def in_thirty_days():
"""Return now + 30 days"""
Expand Down Expand Up @@ -51,7 +52,7 @@ class Tournament(models.Model):
default=in_thirty_days,
null=False,
)
logo: models.FileField = models.FileField(
logo: models.FileField = ImageField(
verbose_name=_("Logo"),
blank=True,
null=True,
Expand Down
4 changes: 3 additions & 1 deletion insalan/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from django.utils.translation import gettext_lazy as _
from django.core.validators import FileExtensionValidator

from insalan.components.image_field import ImageField

class UserManager(BaseUserManager):
"""
Managers the User objects (kind of like a serializer but not quite that)
Expand Down Expand Up @@ -75,7 +77,7 @@ def __init__(self, *args, **kwargs):
USERNAME_FIELD = "username"
EMAIL_FIELD = "email"

image = models.FileField(
image = ImageField(
verbose_name=_("photo de profil"),
blank=True,
null=True,
Expand Down

0 comments on commit 07a164f

Please sign in to comment.