CustomUser and StudentStatus improvements

This commit is contained in:
Keannu Bernasol 2023-08-06 14:19:55 +08:00
parent d6bef3a231
commit 06441702c7
18 changed files with 48 additions and 82 deletions

View file

@ -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 accounts.models
import django.contrib.auth.models import django.contrib.auth.models
@ -13,9 +13,9 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('semesters', '0001_initial'), ('semesters', '0001_initial'),
('courses', '0001_initial'), ('courses', '0001_initial'),
('auth', '0012_alter_user_first_name_max_length'),
] ]
operations = [ operations = [
@ -36,7 +36,7 @@ class Migration(migrations.Migration):
('is_student', models.BooleanField(default=True)), ('is_student', models.BooleanField(default=True)),
('is_studying', models.BooleanField(default=False)), ('is_studying', models.BooleanField(default=False)),
('irregular', 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)), ('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')), ('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')), ('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')),

View file

@ -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 from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
@ -9,10 +9,10 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
('subjects', '0001_initial'),
('accounts', '0001_initial'),
('year_levels', '0001_initial'),
('auth', '0012_alter_user_first_name_max_length'), ('auth', '0012_alter_user_first_name_max_length'),
('accounts', '0001_initial'),
('subjects', '0001_initial'),
('year_levels', '0001_initial'),
] ]
operations = [ operations = [

View file

@ -1,6 +1,4 @@
from django.contrib.auth.models import AbstractUser 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 django.db import models
from courses.models import Course from courses.models import Course
from year_levels.models import Year_Level from year_levels.models import Year_Level
@ -9,14 +7,6 @@ from django.db.models.signals import post_migrate
from django.dispatch import receiver from django.dispatch import receiver
import os import os
from uuid import uuid4 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): class CustomUser(AbstractUser):
@ -52,8 +42,7 @@ class CustomUser(AbstractUser):
is_student = models.BooleanField(default=True) is_student = models.BooleanField(default=True)
is_studying = models.BooleanField(default=False) is_studying = models.BooleanField(default=False)
irregular = models.BooleanField(default=False) irregular = models.BooleanField(default=False)
student_id_number = models.CharField( student_id_number = models.IntegerField(null=False)
max_length=16, validators=[validate_student_id], null=False)
avatar = models.ImageField(upload_to=_get_upload_to, null=True) avatar = models.ImageField(upload_to=_get_upload_to, null=True)
course = models.ForeignKey( course = models.ForeignKey(
Course, Course,
@ -85,21 +74,23 @@ def create_superuser(sender, **kwargs):
username = os.getenv('DJANGO_ADMIN_USERNAME') username = os.getenv('DJANGO_ADMIN_USERNAME')
email = os.getenv('DJANGO_ADMIN_EMAIL') email = os.getenv('DJANGO_ADMIN_EMAIL')
password = os.getenv('DJANGO_ADMIN_PASSWORD') password = os.getenv('DJANGO_ADMIN_PASSWORD')
student_id_number = 0000
if not User.objects.filter(username=username).exists(): if not User.objects.filter(username=username).exists():
# Create the superuser with is_active set to False # Create the superuser with is_active set to False
superuser = User.objects.create_superuser( 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 # Activate the superuser
superuser.is_active = True superuser.is_active = True
print('Created admin account')
superuser.save() superuser.save()
User = CustomUser User = CustomUser
username = 'keannu125' username = 'keannu125'
email = os.getenv('DJANGO_ADMIN_EMAIL') email = os.getenv('DJANGO_ADMIN_EMAIL')
password = os.getenv('DJANGO_ADMIN_PASSWORD') password = os.getenv('DJANGO_ADMIN_PASSWORD')
student_id_number = '2020300490' student_id_number = 2020300490
first_name = 'Keannu' first_name = 'Keannu'
last_name = 'Bernasol' last_name = 'Bernasol'
# course = 'Bachelor of Science in Information Technology' # course = 'Bachelor of Science in Information Technology'
@ -111,6 +102,7 @@ def create_superuser(sender, **kwargs):
user = User.objects.create_user( 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) 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 user.is_active = True
print('Created keannu account')
user.save() user.save()

View file

@ -29,7 +29,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = str(os.getenv('SECRET_KEY')) SECRET_KEY = str(os.getenv('SECRET_KEY'))
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False DEBUG = True
FRONTEND_DEBUG = False FRONTEND_DEBUG = False
ALLOWED_HOSTS = ['*'] ALLOWED_HOSTS = ['*']

View file

@ -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 from django.db import migrations, models

View file

@ -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 import django.contrib.gis.db.models.fields
from django.db import migrations, models from django.db import migrations, models

View file

@ -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 from django.db import migrations, models

View file

@ -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.conf import settings
import django.contrib.gis.db.models.fields import django.contrib.gis.db.models.fields
@ -11,8 +11,8 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
('landmarks', '0001_initial'),
('accounts', '0002_initial'), ('accounts', '0002_initial'),
('landmarks', '0001_initial'),
] ]
operations = [ operations = [

View file

@ -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 from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
@ -9,9 +9,9 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
('student_status', '0001_initial'),
('study_groups', '0001_initial'), ('study_groups', '0001_initial'),
('subjects', '0001_initial'), ('subjects', '0001_initial'),
('student_status', '0001_initial'),
] ]
operations = [ operations = [

View file

@ -1,8 +1,8 @@
from django.db import models from django.db import models
from accounts.models import CustomUser 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.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. # Create your models here.
@ -21,3 +21,10 @@ class StudentStatus(models.Model):
def __str__(self): def __str__(self):
return self.user.full_name 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)

View file

@ -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 from .views import StudentStatusAPIView, ActiveStudentStatusListAPIView
urlpatterns = [ urlpatterns = [

View file

@ -8,9 +8,10 @@ class StudentStatusAPIView(generics.RetrieveUpdateAPIView):
serializer_class = StudentStatusSerializer serializer_class = StudentStatusSerializer
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
def get_object(self): def get_queryset(self):
user = self.request.user user = self.request.user
return StudentStatus.objects.get(user=user) queryset = StudentStatus.objects.filter(user=user)
return queryset
class ActiveStudentStatusListAPIView(generics.ListAPIView): class ActiveStudentStatusListAPIView(generics.ListAPIView):
@ -19,4 +20,4 @@ class ActiveStudentStatusListAPIView(generics.ListAPIView):
def get_queryset(self): def get_queryset(self):
user = self.request.user user = self.request.user
return StudentStatus.objects.filter(active=True) return StudentStatus.objects.filter(user != user).filter(active=True)

View file

@ -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 import django.contrib.gis.db.models.fields
from django.db import migrations, models from django.db import migrations, models
@ -10,8 +10,8 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
('subjects', '0001_initial'),
('student_status', '0001_initial'), ('student_status', '0001_initial'),
('subjects', '0001_initial'),
] ]
operations = [ operations = [

View file

@ -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.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -10,8 +10,8 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('study_groups', '0001_initial'), ('study_groups', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
] ]
operations = [ operations = [

View file

@ -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.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -10,9 +10,9 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
('semesters', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL), migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('courses', '0001_initial'), ('courses', '0001_initial'),
('semesters', '0001_initial'),
('year_levels', '0001_initial'), ('year_levels', '0001_initial'),
] ]
@ -22,11 +22,14 @@ class Migration(migrations.Migration):
fields=[ fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64)), ('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')), ('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')), ('semester', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='semesters.semester')),
('students', models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL)), ('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')), ('year_level', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='year_levels.year_level')),
], ],
options={
'unique_together': {('name', 'course', 'year_level', 'semester')},
},
), ),
] ]

View file

@ -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),
),
]

View file

@ -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')},
),
]

View file

@ -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 from django.db import migrations, models