Changed how the user avatar is stored

its now media/users/USER_UUID/icon.webp every time.

it also defaults to static/images/defaultuser.webp
This commit is contained in:
Corban-Lee Jones 2024-01-05 22:58:43 +00:00
parent 46fb0580b9
commit 7d52bbec66
7 changed files with 46 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# user uploaded files
media/
# byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

View File

@ -0,0 +1,19 @@
# Generated by Django 3.2.16 on 2024-01-05 22:09
import apps.authentication.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('authentication', 'default_departments'),
]
operations = [
migrations.AlterField(
model_name='user',
name='icon',
field=models.ImageField(default='users/default.webp', upload_to=apps.authentication.models.IconPathGenerator(), verbose_name='profile picture'),
),
]

View File

@ -2,10 +2,28 @@
import uuid
import os
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.utils.translation import gettext_lazy as _
from django.core.files.storage import FileSystemStorage
from django.utils.deconstruct import deconstructible
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, max_length=None):
if self.exists(name):
os.remove(os.path.join(self.location, name))
return name
@deconstructible
class IconPathGenerator:
def __call__(self, instance, filename: str) -> str:
return os.path.join("users", str(instance.id), "icon.webp")
class Department(models.Model):
@ -52,7 +70,12 @@ class UserManager(BaseUserManager):
class User(AbstractBaseUser, PermissionsMixin):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
icon = models.ImageField(_("profile picture"), upload_to="users", default="users/default.webp")
icon = models.ImageField(
_("profile picture"),
upload_to=IconPathGenerator(),
default="../static/assets/images/defaultuser.webp",
storage=OverwriteStorage()
)
email = models.EmailField(
_("email address"),
unique=True,

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB