mirror of
https://github.com/kristoferssolo/FOSSDB.git
synced 2025-10-21 17:50:35 +00:00
Created Profile model
This commit is contained in:
parent
20ab71e0c1
commit
8cd9ff3f27
@ -1,19 +1,18 @@
|
|||||||
from django.contrib import admin
|
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
|
model = User
|
||||||
fieldsets = UserAdmin.fieldsets + (
|
inlines = (ProfileInline,)
|
||||||
(
|
|
||||||
None,
|
|
||||||
{
|
|
||||||
"fields": ("profile_picture",),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(User, CustomUserAdmin)
|
admin.site.register(User, UserAdmin)
|
||||||
|
|||||||
@ -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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -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',
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -4,6 +4,8 @@ from pathlib import Path
|
|||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.db import models
|
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:
|
def get_profile_pic_path(instance, filename) -> Path:
|
||||||
@ -14,7 +16,6 @@ def get_profile_pic_path(instance, filename) -> Path:
|
|||||||
|
|
||||||
class User(AbstractUser):
|
class User(AbstractUser):
|
||||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, verbose_name="ID")
|
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
|
@property
|
||||||
def full_name(self):
|
def full_name(self):
|
||||||
@ -27,6 +28,17 @@ class User(AbstractUser):
|
|||||||
else:
|
else:
|
||||||
return f"{self.first_name} {self.last_name}"
|
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):
|
def save(self, *args, **kwags):
|
||||||
old_instance = None
|
old_instance = None
|
||||||
if self.pk:
|
if self.pk:
|
||||||
@ -34,9 +46,20 @@ class User(AbstractUser):
|
|||||||
old_instance = User.objects.get(pk=self.pk)
|
old_instance = User.objects.get(pk=self.pk)
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
pass
|
pass
|
||||||
super(User, self).save(*args, **kwags)
|
super(Profile, self).save(*args, **kwags)
|
||||||
|
|
||||||
# Check if old instance exists and profile picture is different
|
# Check if old instance exists and profile picture is different
|
||||||
if old_instance is not None:
|
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)
|
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()
|
||||||
|
|||||||
@ -54,10 +54,21 @@
|
|||||||
class="hover:text-skyblue-300 transform duration-200 ease-in-out">help</a>
|
class="hover:text-skyblue-300 transform duration-200 ease-in-out">help</a>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
<div class="">
|
<div class="flex items-center justify-between">
|
||||||
<a href="" class=""><i class="fa-solid fa-globe fa-lg"></i></a>
|
<a href="" class=""><i class="fa-solid fa-globe fa-lg"></i></a>
|
||||||
|
{% if user.is_authenticated %}
|
||||||
|
<div class="flex items-center justify-between">
|
||||||
|
<p class="mx-4">{{ user.username }}</p>
|
||||||
|
<a href="{% url 'profile' user.username %}">
|
||||||
|
<img src="{{ user.profile.picture.url }}"
|
||||||
|
class="w-[2rem]"
|
||||||
|
alt="{{ user.username }}'s profile picture" />
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
<a href="{% url 'login' %}"
|
<a href="{% url 'login' %}"
|
||||||
class="hover:text-skyblue-300 transition duration-300 ease-in-out"><i class="fa-solid fa-user fa-lg ml-4 "></i></a>
|
class="hover:text-skyblue-300 transition duration-300 ease-in-out"><i class="fa-solid fa-user fa-lg ml-4 "></i></a>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
<main class="flex flex-col flex-grow">
|
<main class="flex flex-col flex-grow">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user