From 8cd9ff3f279e6604357391cbfbd3c8e23cceddb8 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 28 Jun 2023 21:18:39 +0000 Subject: [PATCH] Created `Profile` model --- src/apps/account/admin.py | 23 +++++++-------- ...003_remove_user_profile_picture_profile.py | 28 ++++++++++++++++++ ..._rename_profile_picture_profile_picture.py | 18 ++++++++++++ src/apps/account/models.py | 29 +++++++++++++++++-- src/apps/tokyonight_night/templates/base.html | 17 +++++++++-- 5 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 src/apps/account/migrations/0003_remove_user_profile_picture_profile.py create mode 100644 src/apps/account/migrations/0004_rename_profile_picture_profile_picture.py diff --git a/src/apps/account/admin.py b/src/apps/account/admin.py index 867f339..8d46c1b 100644 --- a/src/apps/account/admin.py +++ b/src/apps/account/admin.py @@ -1,19 +1,18 @@ from django.contrib import admin -from django.contrib.auth.admin import UserAdmin +from django.contrib.auth.admin import UserAdmin as BaseUserAdmin -from .models import User +from .models import Profile, User -class CustomUserAdmin(UserAdmin): +class ProfileInline(admin.StackedInline): + model = Profile + can_delete = False + verbose_name_plural = "Profile" + + +class UserAdmin(BaseUserAdmin): model = User - fieldsets = UserAdmin.fieldsets + ( - ( - None, - { - "fields": ("profile_picture",), - }, - ), - ) + inlines = (ProfileInline,) -admin.site.register(User, CustomUserAdmin) +admin.site.register(User, UserAdmin) diff --git a/src/apps/account/migrations/0003_remove_user_profile_picture_profile.py b/src/apps/account/migrations/0003_remove_user_profile_picture_profile.py new file mode 100644 index 0000000..0eb8801 --- /dev/null +++ b/src/apps/account/migrations/0003_remove_user_profile_picture_profile.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.2 on 2023-06-28 20:26 + +import account.models +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0002_alter_user_profile_picture'), + ] + + operations = [ + migrations.RemoveField( + model_name='user', + name='profile_picture', + ), + migrations.CreateModel( + name='Profile', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('profile_picture', models.ImageField(default='profile-pics/default.jpg', upload_to=account.models.get_profile_pic_path)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/src/apps/account/migrations/0004_rename_profile_picture_profile_picture.py b/src/apps/account/migrations/0004_rename_profile_picture_profile_picture.py new file mode 100644 index 0000000..f871c56 --- /dev/null +++ b/src/apps/account/migrations/0004_rename_profile_picture_profile_picture.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-06-28 20:32 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('account', '0003_remove_user_profile_picture_profile'), + ] + + operations = [ + migrations.RenameField( + model_name='profile', + old_name='profile_picture', + new_name='picture', + ), + ] diff --git a/src/apps/account/models.py b/src/apps/account/models.py index 49c64da..a447cce 100644 --- a/src/apps/account/models.py +++ b/src/apps/account/models.py @@ -4,6 +4,8 @@ from pathlib import Path from django.contrib.auth.models import AbstractUser from django.core.exceptions import ObjectDoesNotExist from django.db import models +from django.db.models.signals import post_save +from django.dispatch import receiver def get_profile_pic_path(instance, filename) -> Path: @@ -14,7 +16,6 @@ def get_profile_pic_path(instance, filename) -> Path: class User(AbstractUser): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name="ID") - profile_picture = models.ImageField(upload_to=get_profile_pic_path, default="profile-pics/default.jpg") @property def full_name(self): @@ -27,6 +28,17 @@ class User(AbstractUser): else: return f"{self.first_name} {self.last_name}" + def __str__(self): + return self.username + + +class Profile(models.Model): + user = models.OneToOneField(User, on_delete=models.CASCADE) + picture = models.ImageField(upload_to=get_profile_pic_path, default="profile-pics/default.jpg") + + def __str__(self): + return f"{self.user.username} Profile" + def save(self, *args, **kwags): old_instance = None if self.pk: @@ -34,9 +46,20 @@ class User(AbstractUser): old_instance = User.objects.get(pk=self.pk) except ObjectDoesNotExist: pass - super(User, self).save(*args, **kwags) + super(Profile, self).save(*args, **kwags) # Check if old instance exists and profile picture is different if old_instance is not None: - if old_instance.profile_picture and self.profile_picture and old_instance.profile_picture.url != self.profile_picture.url: + if old_instance.profile_picture and self.picture and old_instance.profile_picture.url != self.picture.url: old_instance.profile_picture.delete(save=False) + + +@receiver(post_save, sender=User) +def create_profile(sender, instance, created, **kwags): + if created: + Profile.objects.create(user=instance) + + +@receiver(post_save, sender=User) +def save_profile(sender, instance, **kwags): + instance.profile.save() diff --git a/src/apps/tokyonight_night/templates/base.html b/src/apps/tokyonight_night/templates/base.html index b2674ce..912a5ba 100644 --- a/src/apps/tokyonight_night/templates/base.html +++ b/src/apps/tokyonight_night/templates/base.html @@ -54,10 +54,21 @@ class="hover:text-skyblue-300 transform duration-200 ease-in-out">help -
+
- + {% if user.is_authenticated %} +
+

{{ user.username }}

+ + {{ user.username }}'s profile picture + +
+ {% else %} + + {% endif %}