From 06441702c70c5944c373f8749d086240549f80c2 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sun, 6 Aug 2023 14:19:55 +0800 Subject: [PATCH] CustomUser and StudentStatus improvements --- stude/accounts/migrations/0001_initial.py | 6 ++--- stude/accounts/migrations/0002_initial.py | 8 +++---- stude/accounts/models.py | 22 ++++++------------- stude/config/settings.py | 2 +- stude/courses/migrations/0001_initial.py | 2 +- stude/landmarks/migrations/0001_initial.py | 2 +- stude/semesters/migrations/0001_initial.py | 2 +- .../student_status/migrations/0001_initial.py | 4 ++-- .../student_status/migrations/0002_initial.py | 4 ++-- stude/student_status/models.py | 11 ++++++++-- stude/student_status/urls.py | 3 ++- stude/student_status/views.py | 7 +++--- stude/study_groups/migrations/0001_initial.py | 4 ++-- .../migrations/0001_initial.py | 4 ++-- stude/subjects/migrations/0001_initial.py | 9 +++++--- .../migrations/0002_alter_subject_code.py | 18 --------------- .../0003_alter_subject_unique_together.py | 20 ----------------- stude/year_levels/migrations/0001_initial.py | 2 +- 18 files changed, 48 insertions(+), 82 deletions(-) delete mode 100644 stude/subjects/migrations/0002_alter_subject_code.py delete mode 100644 stude/subjects/migrations/0003_alter_subject_unique_together.py diff --git a/stude/accounts/migrations/0001_initial.py b/stude/accounts/migrations/0001_initial.py index f5780ff..fecce05 100644 --- a/stude/accounts/migrations/0001_initial.py +++ b/stude/accounts/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 import accounts.models import django.contrib.auth.models @@ -13,9 +13,9 @@ class Migration(migrations.Migration): initial = True dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), ('semesters', '0001_initial'), ('courses', '0001_initial'), - ('auth', '0012_alter_user_first_name_max_length'), ] operations = [ @@ -36,7 +36,7 @@ class Migration(migrations.Migration): ('is_student', models.BooleanField(default=True)), ('is_studying', models.BooleanField(default=False)), ('irregular', models.BooleanField(default=False)), - ('student_id_number', models.CharField(max_length=16, validators=[accounts.models.validate_student_id])), + ('student_id_number', models.IntegerField()), ('avatar', models.ImageField(null=True, upload_to=accounts.models.CustomUser._get_upload_to)), ('course', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='courses.course')), ('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')), diff --git a/stude/accounts/migrations/0002_initial.py b/stude/accounts/migrations/0002_initial.py index 2983227..a474d0a 100644 --- a/stude/accounts/migrations/0002_initial.py +++ b/stude/accounts/migrations/0002_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 from django.db import migrations, models import django.db.models.deletion @@ -9,10 +9,10 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('subjects', '0001_initial'), - ('accounts', '0001_initial'), - ('year_levels', '0001_initial'), ('auth', '0012_alter_user_first_name_max_length'), + ('accounts', '0001_initial'), + ('subjects', '0001_initial'), + ('year_levels', '0001_initial'), ] operations = [ diff --git a/stude/accounts/models.py b/stude/accounts/models.py index 71a8e56..7182f42 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -1,6 +1,4 @@ from django.contrib.auth.models import AbstractUser -from django.core.exceptions import ValidationError -from django.utils.text import slugify from django.db import models from courses.models import Course from year_levels.models import Year_Level @@ -9,14 +7,6 @@ from django.db.models.signals import post_migrate from django.dispatch import receiver import os from uuid import uuid4 -from django.utils.deconstruct import deconstructible - - -def validate_student_id(value): - try: - int(value) - except (ValueError, TypeError): - raise ValidationError('Student ID must be a valid integer.') class CustomUser(AbstractUser): @@ -52,8 +42,7 @@ class CustomUser(AbstractUser): is_student = models.BooleanField(default=True) is_studying = models.BooleanField(default=False) irregular = models.BooleanField(default=False) - student_id_number = models.CharField( - max_length=16, validators=[validate_student_id], null=False) + student_id_number = models.IntegerField(null=False) avatar = models.ImageField(upload_to=_get_upload_to, null=True) course = models.ForeignKey( Course, @@ -85,21 +74,23 @@ def create_superuser(sender, **kwargs): username = os.getenv('DJANGO_ADMIN_USERNAME') email = os.getenv('DJANGO_ADMIN_EMAIL') password = os.getenv('DJANGO_ADMIN_PASSWORD') + student_id_number = 0000 if not User.objects.filter(username=username).exists(): # Create the superuser with is_active set to False superuser = User.objects.create_superuser( - username=username, email=email, password=password) + username=username, email=email, password=password, is_student=False, student_id_number=student_id_number) # Activate the superuser superuser.is_active = True + print('Created admin account') superuser.save() User = CustomUser username = 'keannu125' email = os.getenv('DJANGO_ADMIN_EMAIL') password = os.getenv('DJANGO_ADMIN_PASSWORD') - student_id_number = '2020300490' + student_id_number = 2020300490 first_name = 'Keannu' last_name = 'Bernasol' # course = 'Bachelor of Science in Information Technology' @@ -111,6 +102,7 @@ def create_superuser(sender, **kwargs): user = User.objects.create_user( username=username, email=email, password=password, first_name=first_name, last_name=last_name, student_id_number=student_id_number) - # Activate the superuser + # Activate the user user.is_active = True + print('Created keannu account') user.save() diff --git a/stude/config/settings.py b/stude/config/settings.py index 5d6b3f2..da4be57 100644 --- a/stude/config/settings.py +++ b/stude/config/settings.py @@ -29,7 +29,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = str(os.getenv('SECRET_KEY')) # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = False +DEBUG = True FRONTEND_DEBUG = False ALLOWED_HOSTS = ['*'] diff --git a/stude/courses/migrations/0001_initial.py b/stude/courses/migrations/0001_initial.py index 23dbfdc..f445b71 100644 --- a/stude/courses/migrations/0001_initial.py +++ b/stude/courses/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 from django.db import migrations, models diff --git a/stude/landmarks/migrations/0001_initial.py b/stude/landmarks/migrations/0001_initial.py index d4b6d96..5c1579e 100644 --- a/stude/landmarks/migrations/0001_initial.py +++ b/stude/landmarks/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 import django.contrib.gis.db.models.fields from django.db import migrations, models diff --git a/stude/semesters/migrations/0001_initial.py b/stude/semesters/migrations/0001_initial.py index 1de922f..34af86b 100644 --- a/stude/semesters/migrations/0001_initial.py +++ b/stude/semesters/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 from django.db import migrations, models diff --git a/stude/student_status/migrations/0001_initial.py b/stude/student_status/migrations/0001_initial.py index 8daa441..f461925 100644 --- a/stude/student_status/migrations/0001_initial.py +++ b/stude/student_status/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 from django.conf import settings import django.contrib.gis.db.models.fields @@ -11,8 +11,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('landmarks', '0001_initial'), ('accounts', '0002_initial'), + ('landmarks', '0001_initial'), ] operations = [ diff --git a/stude/student_status/migrations/0002_initial.py b/stude/student_status/migrations/0002_initial.py index b55be6c..c66047f 100644 --- a/stude/student_status/migrations/0002_initial.py +++ b/stude/student_status/migrations/0002_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 from django.db import migrations, models import django.db.models.deletion @@ -9,9 +9,9 @@ class Migration(migrations.Migration): initial = True dependencies = [ + ('student_status', '0001_initial'), ('study_groups', '0001_initial'), ('subjects', '0001_initial'), - ('student_status', '0001_initial'), ] operations = [ diff --git a/stude/student_status/models.py b/stude/student_status/models.py index b7bed57..b943154 100644 --- a/stude/student_status/models.py +++ b/stude/student_status/models.py @@ -1,8 +1,8 @@ from django.db import models from accounts.models import CustomUser -from study_groups.models import StudyGroup from django.contrib.gis.db import models as gis_models -from django.contrib.gis.geos import Point +from django.db.models.signals import post_save +from django.dispatch import receiver # Create your models here. @@ -21,3 +21,10 @@ class StudentStatus(models.Model): def __str__(self): return self.user.full_name + + +@receiver(post_save, sender=CustomUser) +def create_student_status(sender, instance, created, **kwargs): + if created: + if instance.is_student: + StudentStatus.objects.create(user=instance) diff --git a/stude/student_status/urls.py b/stude/student_status/urls.py index 45c6b5c..ba5d6fa 100644 --- a/stude/student_status/urls.py +++ b/stude/student_status/urls.py @@ -1,4 +1,5 @@ -from django.urls import path +from django.urls import path, include +from rest_framework.routers import DefaultRouter from .views import StudentStatusAPIView, ActiveStudentStatusListAPIView urlpatterns = [ diff --git a/stude/student_status/views.py b/stude/student_status/views.py index 01727f0..46fd771 100644 --- a/stude/student_status/views.py +++ b/stude/student_status/views.py @@ -8,9 +8,10 @@ class StudentStatusAPIView(generics.RetrieveUpdateAPIView): serializer_class = StudentStatusSerializer permission_classes = [IsAuthenticated] - def get_object(self): + def get_queryset(self): user = self.request.user - return StudentStatus.objects.get(user=user) + queryset = StudentStatus.objects.filter(user=user) + return queryset class ActiveStudentStatusListAPIView(generics.ListAPIView): @@ -19,4 +20,4 @@ class ActiveStudentStatusListAPIView(generics.ListAPIView): def get_queryset(self): user = self.request.user - return StudentStatus.objects.filter(active=True) + return StudentStatus.objects.filter(user != user).filter(active=True) diff --git a/stude/study_groups/migrations/0001_initial.py b/stude/study_groups/migrations/0001_initial.py index 21dc0be..9f77cd8 100644 --- a/stude/study_groups/migrations/0001_initial.py +++ b/stude/study_groups/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 import django.contrib.gis.db.models.fields from django.db import migrations, models @@ -10,8 +10,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('subjects', '0001_initial'), ('student_status', '0001_initial'), + ('subjects', '0001_initial'), ] operations = [ diff --git a/stude/studygroup_messages/migrations/0001_initial.py b/stude/studygroup_messages/migrations/0001_initial.py index 52431c5..94c52b8 100644 --- a/stude/studygroup_messages/migrations/0001_initial.py +++ b/stude/studygroup_messages/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 from django.conf import settings from django.db import migrations, models @@ -10,8 +10,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('study_groups', '0001_initial'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ diff --git a/stude/subjects/migrations/0001_initial.py b/stude/subjects/migrations/0001_initial.py index 9c3b8b6..d500266 100644 --- a/stude/subjects/migrations/0001_initial.py +++ b/stude/subjects/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 from django.conf import settings from django.db import migrations, models @@ -10,9 +10,9 @@ class Migration(migrations.Migration): initial = True dependencies = [ + ('semesters', '0001_initial'), migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('courses', '0001_initial'), - ('semesters', '0001_initial'), ('year_levels', '0001_initial'), ] @@ -22,11 +22,14 @@ class Migration(migrations.Migration): fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=64)), - ('code', models.CharField(max_length=16, unique=True)), + ('code', models.CharField(max_length=16)), ('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='courses.course')), ('semester', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='semesters.semester')), ('students', models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL)), ('year_level', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='year_levels.year_level')), ], + options={ + 'unique_together': {('name', 'course', 'year_level', 'semester')}, + }, ), ] diff --git a/stude/subjects/migrations/0002_alter_subject_code.py b/stude/subjects/migrations/0002_alter_subject_code.py deleted file mode 100644 index 71cc3f8..0000000 --- a/stude/subjects/migrations/0002_alter_subject_code.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:08 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('subjects', '0001_initial'), - ] - - operations = [ - migrations.AlterField( - model_name='subject', - name='code', - field=models.CharField(max_length=16), - ), - ] diff --git a/stude/subjects/migrations/0003_alter_subject_unique_together.py b/stude/subjects/migrations/0003_alter_subject_unique_together.py deleted file mode 100644 index 3951a83..0000000 --- a/stude/subjects/migrations/0003_alter_subject_unique_together.py +++ /dev/null @@ -1,20 +0,0 @@ -# Generated by Django 4.2.3 on 2023-07-28 16:53 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('courses', '0001_initial'), - ('year_levels', '0001_initial'), - ('semesters', '0001_initial'), - ('subjects', '0002_alter_subject_code'), - ] - - operations = [ - migrations.AlterUniqueTogether( - name='subject', - unique_together={('name', 'course', 'year_level', 'semester')}, - ), - ] diff --git a/stude/year_levels/migrations/0001_initial.py b/stude/year_levels/migrations/0001_initial.py index 6c0ec08..0765c4b 100644 --- a/stude/year_levels/migrations/0001_initial.py +++ b/stude/year_levels/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.3 on 2023-07-26 12:06 +# Generated by Django 4.2.3 on 2023-08-06 05:55 from django.db import migrations, models