From 7762f72ce2e143ec27f7bd880074edc5468a5d7d Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Mon, 26 Jun 2023 21:39:59 +0800 Subject: [PATCH] Polished avatar photo uploads for users --- stude/accounts/migrations/0001_initial.py | 10 +++++-- ...ng_alter_customuser_first_name_and_more.py | 28 ------------------- .../migrations/0003_customuser_avatar.py | 18 ------------ .../0004_alter_customuser_avatar.py | 18 ------------ stude/accounts/models.py | 24 +++++++++++++++- stude/accounts/serializers.py | 2 +- 6 files changed, 31 insertions(+), 69 deletions(-) delete mode 100644 stude/accounts/migrations/0002_customuser_is_studying_alter_customuser_first_name_and_more.py delete mode 100644 stude/accounts/migrations/0003_customuser_avatar.py delete mode 100644 stude/accounts/migrations/0004_alter_customuser_avatar.py diff --git a/stude/accounts/migrations/0001_initial.py b/stude/accounts/migrations/0001_initial.py index afd21e8..3b66ba3 100644 --- a/stude/accounts/migrations/0001_initial.py +++ b/stude/accounts/migrations/0001_initial.py @@ -1,5 +1,6 @@ -# Generated by Django 4.2.2 on 2023-06-26 12:08 +# Generated by Django 4.2.2 on 2023-06-26 13:38 +import accounts.models import django.contrib.auth.models import django.contrib.auth.validators from django.db import migrations, models @@ -23,16 +24,19 @@ class Migration(migrations.Migration): ('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')), + ('first_name', models.CharField(max_length=100)), + ('last_name', models.CharField(max_length=100)), ('is_student', models.BooleanField(default=True)), + ('is_studying', models.BooleanField(default=False)), ('is_banned', models.BooleanField(default=False)), + ('student_id_number', models.CharField(max_length=16, validators=[accounts.models.validate_student_id])), ('year_level', models.CharField(choices=[('1st', '1st year'), ('2nd', '2nd year'), ('3rd', '3rd year'), ('4th', '4th year'), ('5th', '5th Year'), ('Irreg', 'Irregular')], max_length=50)), ('semester', models.CharField(choices=[('1st', '1st semester'), ('2nd', '2nd semester')], max_length=50)), + ('avatar', models.ImageField(null=True, upload_to=accounts.models.CustomUser._get_upload_to)), ('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')), ], diff --git a/stude/accounts/migrations/0002_customuser_is_studying_alter_customuser_first_name_and_more.py b/stude/accounts/migrations/0002_customuser_is_studying_alter_customuser_first_name_and_more.py deleted file mode 100644 index 1c76ccc..0000000 --- a/stude/accounts/migrations/0002_customuser_is_studying_alter_customuser_first_name_and_more.py +++ /dev/null @@ -1,28 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-26 12:23 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='customuser', - name='is_studying', - field=models.BooleanField(default=False), - ), - migrations.AlterField( - model_name='customuser', - name='first_name', - field=models.CharField(max_length=100), - ), - migrations.AlterField( - model_name='customuser', - name='last_name', - field=models.CharField(max_length=100), - ), - ] diff --git a/stude/accounts/migrations/0003_customuser_avatar.py b/stude/accounts/migrations/0003_customuser_avatar.py deleted file mode 100644 index 6ec8b03..0000000 --- a/stude/accounts/migrations/0003_customuser_avatar.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-26 12:29 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0002_customuser_is_studying_alter_customuser_first_name_and_more'), - ] - - operations = [ - migrations.AddField( - model_name='customuser', - name='avatar', - field=models.ImageField(null=True, upload_to='images'), - ), - ] diff --git a/stude/accounts/migrations/0004_alter_customuser_avatar.py b/stude/accounts/migrations/0004_alter_customuser_avatar.py deleted file mode 100644 index 0b6e4d9..0000000 --- a/stude/accounts/migrations/0004_alter_customuser_avatar.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-26 12:41 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('accounts', '0003_customuser_avatar'), - ] - - operations = [ - migrations.AlterField( - model_name='customuser', - name='avatar', - field=models.ImageField(null=True, upload_to='media/avatars'), - ), - ] diff --git a/stude/accounts/models.py b/stude/accounts/models.py index 8e7fcd3..218b8ba 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -1,8 +1,28 @@ +import time from django.contrib.auth.models import AbstractUser +from django.core.exceptions import ValidationError +from django.utils.text import slugify from django.db import models +import os + + +def validate_student_id(value): + try: + int(value) + except (ValueError, TypeError): + raise ValidationError('Student ID must be a valid integer.') class CustomUser(AbstractUser): + def _get_upload_to(instance, filename): + base_filename, file_extension = os.path.splitext(filename) + # Convert filename to a slug format + cleaned_filename = slugify(base_filename) + # Get the student ID number + student_id = str(instance.student_id_number) + new_filename = f"{student_id}_{cleaned_filename}{file_extension}" + return os.path.join('avatars', new_filename) + YEAR_LEVELS = ( ('1st', '1st year'), ('2nd', '2nd year'), @@ -24,9 +44,11 @@ class CustomUser(AbstractUser): is_student = models.BooleanField(default=True) is_studying = models.BooleanField(default=False) is_banned = models.BooleanField(default=False) + student_id_number = models.CharField( + max_length=16, validators=[validate_student_id], null=False) year_level = models.CharField( max_length=50, choices=YEAR_LEVELS) semester = models.CharField( max_length=50, choices=SEMESTERS) - avatar = models.ImageField(upload_to='avatars', null=True) + avatar = models.ImageField(upload_to=_get_upload_to, null=True) pass diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index c9a3c0c..7d07f33 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -12,4 +12,4 @@ class CustomUserSerializer(BaseUserSerializer): class UserRegistrationSerializer(BaseUserRegistrationSerializer): class Meta(BaseUserRegistrationSerializer.Meta): fields = ('username', 'email', 'password', - 'is_student', 'year_level', 'semester', 'avatar') + 'is_student', 'student_id_number', 'year_level', 'semester', 'avatar')