Modified User model

Replaced `id` with `uuid` and added profile picture field
This commit is contained in:
Kristofers Solo 2023-06-27 13:57:24 +00:00
parent e087399f25
commit ffa9827f44
22 changed files with 310 additions and 9 deletions

View File

@ -0,0 +1,19 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
class CustomUserAdmin(UserAdmin):
model = User
fieldsets = UserAdmin.fieldsets + (
(
None,
{
"fields": ("profile_picture",),
},
),
)
admin.site.register(User, CustomUserAdmin)

View File

@ -1,7 +1,8 @@
from django import forms from django import forms
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import User
class SignUpForm(UserCreationForm): class SignUpForm(UserCreationForm):

View File

@ -0,0 +1,45 @@
# Generated by Django 4.2.2 on 2023-06-27 12:54
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone
import uuid
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.CreateModel(
name='CustomUser',
fields=[
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('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')),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='ID')),
('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')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-27 13:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='customuser',
name='profile_pic',
field=models.ImageField(default='default.jpg', upload_to='profile_pics/'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-27 13:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0002_customuser_profile_pic'),
]
operations = [
migrations.AlterField(
model_name='customuser',
name='profile_pic',
field=models.BinaryField(blank=True),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-27 13:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0003_alter_customuser_profile_pic'),
]
operations = [
migrations.AlterField(
model_name='customuser',
name='profile_pic',
field=models.BinaryField(blank=True, editable=True),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-27 13:33
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('account', '0004_alter_customuser_profile_pic'),
]
operations = [
migrations.RenameField(
model_name='customuser',
old_name='profile_pic',
new_name='profile_picture',
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-06-27 13:34
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0005_rename_profile_pic_customuser_profile_picture'),
]
operations = [
migrations.AlterField(
model_name='customuser',
name='profile_picture',
field=models.ImageField(default='profile_pic/default.jpg', upload_to='profile_pics/'),
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 4.2.2 on 2023-06-27 13:43
import account.models
from django.db import migrations, models
import pathlib
class Migration(migrations.Migration):
dependencies = [
('account', '0006_alter_customuser_profile_picture'),
]
operations = [
migrations.AlterField(
model_name='customuser',
name='profile_picture',
field=models.ImageField(default=pathlib.PurePosixPath('profile_pic/default.jpg'), upload_to=account.models.get_profile_pic_path),
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 4.2.2 on 2023-06-27 13:51
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('fossdb', '0001_initial'),
('auth', '0012_alter_user_first_name_max_length'),
('admin', '0003_logentry_add_action_flag_choices'),
('account', '0007_alter_customuser_profile_picture'),
]
operations = [
migrations.RenameModel(
old_name='CustomUser',
new_name='User',
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 4.2.2 on 2023-06-27 13:53
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('fossdb', '0001_initial'),
('auth', '0012_alter_user_first_name_max_length'),
('admin', '0003_logentry_add_action_flag_choices'),
('account', '0008_rename_customuser_user'),
]
operations = [
migrations.RenameModel(
old_name='User',
new_name='CustomUser',
),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 4.2.2 on 2023-06-27 13:55
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('admin', '0003_logentry_add_action_flag_choices'),
('auth', '0012_alter_user_first_name_max_length'),
('fossdb', '0001_initial'),
('account', '0009_rename_user_customuser'),
]
operations = [
migrations.RenameModel(
old_name='CustomUser',
new_name='User',
),
]

View File

@ -0,0 +1,42 @@
import uuid
from pathlib import Path
from django.contrib.auth.models import AbstractUser
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
def get_profile_pic_path(instance, filename) -> Path:
ext = filename.split(".")[-1]
filename = f"{instance.id}.{ext}"
return Path("profile_pics", filename)
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=Path("profile_pic", "default.jpg"))
@property
def full_name(self):
if not self.first_name and not self.last_name:
return ""
elif self.first_name and not self.last_name:
return self.first_name
elif not self.first_name and self.last_name:
return self.last_name
else:
return f"{self.first_name} {self.last_name}"
def save(self, *args, **kwags):
old_instance = None
if self.pk:
try:
old_instance = User.objects.get(pk=self.pk)
except ObjectDoesNotExist:
pass
super(User, 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:
old_instance.profile_picture.delete(save=False)

View File

@ -5,4 +5,5 @@ from . import views
urlpatterns = [ urlpatterns = [
path("signup/", views.signup_view, name="signup"), path("signup/", views.signup_view, name="signup"),
path("login/", views.login_view, name="login"), path("login/", views.login_view, name="login"),
path("<str:username>/", views.profile, name="profile"),
] ]

View File

@ -1,8 +1,19 @@
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from django.shortcuts import redirect, render from django.shortcuts import get_object_or_404, redirect, render
from .forms import SignUpForm from .forms import SignUpForm
from .models import User
def profile(request, username):
user = get_object_or_404(User, username=username)
context = {
"title": user.username + ("" if not user.full_name else f" ({user.full_name})"),
"user": user,
}
return render(request, "account/profile.html", context)
def signup_view(request): def signup_view(request):

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.2 on 2023-06-27 10:33 # Generated by Django 4.2.2 on 2023-06-27 12:54
from django.conf import settings from django.conf import settings
from django.db import migrations, models from django.db import migrations, models

View File

@ -1,6 +1,6 @@
import uuid import uuid
from django.contrib.auth.models import User from django.conf import settings
from django.db import models from django.db import models
@ -12,7 +12,7 @@ from .tag.models import Tag
class Project(models.Model): class Project(models.Model):
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")
owner = models.ForeignKey(User, on_delete=models.CASCADE) owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
description = models.TextField(blank=True, default="") description = models.TextField(blank=True, default="")
license = models.ManyToManyField(License, blank=True) license = models.ManyToManyField(License, blank=True)

View File

@ -1,9 +1,7 @@
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
User = settings.AUTH_USER_MODEL
class Star(models.Model): class Star(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE) user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
project = models.ForeignKey("Project", on_delete=models.CASCADE, related_name="stars") project = models.ForeignKey("Project", on_delete=models.CASCADE, related_name="stars")

View File

@ -75,6 +75,8 @@ TEMPLATES = [
WSGI_APPLICATION = "FOSSDB.wsgi.application" WSGI_APPLICATION = "FOSSDB.wsgi.application"
AUTH_USER_MODEL = "account.User"
# Database # Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases # https://docs.djangoproject.com/en/4.0/ref/settings/#databases

View File

@ -21,8 +21,10 @@ from django.urls import include, path
urlpatterns = [ urlpatterns = [
path("", include("fossdb.urls")), path("", include("fossdb.urls")),
path("", include("account.urls")), path("", include("account.urls")),
# path("", include("django.contrib.auth.urls")),
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("", include("django.contrib.auth.urls")),
] ]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

View File

@ -0,0 +1,10 @@
{% extends "layout.html" %}
{% load static %}
{% block title %}{{ title }}{% endblock %}
{% block meta %}{% endblock %}
{% block content %}
<h1>{{ user.username }}</h1>
<img src="{{ user.profile_picture.url }}"
alt="{{ user.username }}s profile picture" />
<p>{{ user.email }}</p>
{% endblock %}