Convert image field to webp format

This commit is contained in:
Keannu Bernasol 2024-01-06 12:35:36 +08:00
parent 55f16b15fe
commit 2844f5d19a
2 changed files with 5 additions and 4 deletions

View file

@ -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')),
],

View file

@ -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)}'