From 2844f5d19a499c3d940cac3cb640440f5ed9e558 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sat, 6 Jan 2024 12:35:36 +0800 Subject: [PATCH] Convert image field to webp format --- backend/accounts/migrations/0001_initial.py | 5 +++-- backend/accounts/models.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/backend/accounts/migrations/0001_initial.py b/backend/accounts/migrations/0001_initial.py index cc7cdcb..189e3d5 100644 --- a/backend/accounts/migrations/0001_initial.py +++ b/backend/accounts/migrations/0001_initial.py @@ -1,8 +1,9 @@ -# Generated by Django 5.0.1 on 2024-01-06 04:09 +# Generated by Django 5.0.1 on 2024-01-06 04:34 import django.contrib.auth.models import django.contrib.auth.validators import django.utils.timezone +import django_resized.forms from django.db import migrations, models @@ -29,7 +30,7 @@ class Migration(migrations.Migration): ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), - ('avatar', models.ImageField(null=True, upload_to='avatars/')), + ('avatar', django_resized.forms.ResizedImageField(crop=None, force_format='WEBP', keep_meta=True, null=True, quality=100, scale=None, size=[1920, 1080], upload_to='avatars/')), ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), ], diff --git a/backend/accounts/models.py b/backend/accounts/models.py index 6fe4041..4b7716b 100644 --- a/backend/accounts/models.py +++ b/backend/accounts/models.py @@ -15,8 +15,8 @@ class CustomUser(AbstractUser): # password inherited from base user class # is_admin inherited from base user class - avatar = models.ImageField( - null=True, upload_to='avatars/') + avatar = ResizedImageField( + null=True, force_format="WEBP", quality=100, upload_to='avatars/') def avatar_url(self): return f'/api/v1/media/avatars/{self.avatar.field.storage.name(self.avatar.path)}'