2023-06-26 19:19:38 +08:00
|
|
|
from django.contrib.auth.models import AbstractUser
|
2023-06-26 21:39:59 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.utils.text import slugify
|
2023-06-26 19:10:04 +08:00
|
|
|
from django.db import models
|
2023-06-27 14:38:07 +08:00
|
|
|
from courses.models import Course
|
2023-06-27 15:41:34 +08:00
|
|
|
from year_levels.models import Year_Level
|
2023-06-27 15:48:43 +08:00
|
|
|
from semesters.models import Semester
|
2023-06-27 15:41:34 +08:00
|
|
|
from django.db.models.signals import post_migrate
|
|
|
|
from django.dispatch import receiver
|
2023-06-27 21:12:03 +08:00
|
|
|
import os
|
2023-06-26 21:39:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
def validate_student_id(value):
|
|
|
|
try:
|
|
|
|
int(value)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
raise ValidationError('Student ID must be a valid integer.')
|
2023-06-26 19:10:04 +08:00
|
|
|
|
2023-06-26 19:19:38 +08:00
|
|
|
|
|
|
|
class CustomUser(AbstractUser):
|
2023-06-26 22:06:05 +08:00
|
|
|
def _get_upload_to(instance, filename):
|
|
|
|
base_filename, file_extension = os.path.splitext(filename)
|
|
|
|
# Get the student ID number
|
|
|
|
student_id = str(instance.student_id_number)
|
2023-07-15 00:01:51 +08:00
|
|
|
new_filename = f"{student_id}_{file_extension}"
|
2023-06-26 22:06:05 +08:00
|
|
|
return os.path.join('avatars', new_filename)
|
|
|
|
|
2023-06-26 20:36:58 +08:00
|
|
|
first_name = models.CharField(max_length=100)
|
|
|
|
last_name = models.CharField(max_length=100)
|
|
|
|
# Email inherited from base user class
|
|
|
|
# Username inherited from base user class
|
|
|
|
# Password inherited from base user class
|
|
|
|
# is_admin inherited from base user class
|
2023-07-04 19:36:29 +08:00
|
|
|
is_active = models.BooleanField(default=False)
|
2023-06-26 19:19:38 +08:00
|
|
|
is_student = models.BooleanField(default=True)
|
2023-06-26 20:36:58 +08:00
|
|
|
is_studying = models.BooleanField(default=False)
|
2023-06-26 19:19:38 +08:00
|
|
|
is_banned = models.BooleanField(default=False)
|
2023-06-26 21:39:59 +08:00
|
|
|
student_id_number = models.CharField(
|
|
|
|
max_length=16, validators=[validate_student_id], null=False)
|
|
|
|
avatar = models.ImageField(upload_to=_get_upload_to, null=True)
|
2023-06-27 14:38:07 +08:00
|
|
|
course = models.ForeignKey(
|
|
|
|
Course,
|
2023-07-04 18:06:47 +08:00
|
|
|
on_delete=models.SET_NULL,
|
2023-06-27 14:38:07 +08:00
|
|
|
null=True
|
|
|
|
)
|
2023-06-27 15:48:43 +08:00
|
|
|
year_level = models.ForeignKey(
|
2023-06-27 15:41:34 +08:00
|
|
|
Year_Level,
|
2023-07-04 18:06:47 +08:00
|
|
|
on_delete=models.SET_NULL,
|
2023-06-27 15:41:34 +08:00
|
|
|
null=True
|
|
|
|
)
|
2023-06-27 15:48:43 +08:00
|
|
|
semester = models.ForeignKey(
|
|
|
|
Semester,
|
2023-07-04 18:06:47 +08:00
|
|
|
on_delete=models.SET_NULL,
|
2023-06-27 15:48:43 +08:00
|
|
|
null=True
|
|
|
|
)
|
2023-07-18 15:51:06 +08:00
|
|
|
subjects = models.ManyToManyField('subjects.Subject')
|
2023-06-26 23:50:55 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def full_name(self):
|
|
|
|
return f"{self.first_name} {self.last_name}"
|
2023-06-26 19:45:58 +08:00
|
|
|
pass
|
2023-06-27 15:41:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_migrate)
|
|
|
|
def create_superuser(sender, **kwargs):
|
|
|
|
if sender.name == 'accounts':
|
|
|
|
User = CustomUser
|
|
|
|
username = os.getenv('DJANGO_ADMIN_USERNAME')
|
|
|
|
email = os.getenv('DJANGO_ADMIN_EMAIL')
|
|
|
|
password = os.getenv('DJANGO_ADMIN_PASSWORD')
|
|
|
|
|
|
|
|
if not User.objects.filter(username=username).exists():
|
2023-07-06 18:15:41 +08:00
|
|
|
# Create the superuser with is_active set to False
|
|
|
|
superuser = User.objects.create_superuser(
|
|
|
|
username=username, email=email, password=password)
|
|
|
|
|
|
|
|
# Activate the superuser
|
|
|
|
superuser.is_active = True
|
|
|
|
superuser.save()
|
2023-07-18 15:51:06 +08:00
|
|
|
|
|
|
|
User = CustomUser
|
|
|
|
username = 'keannu125'
|
|
|
|
email = os.getenv('DJANGO_ADMIN_EMAIL')
|
|
|
|
password = os.getenv('DJANGO_ADMIN_PASSWORD')
|
|
|
|
first_name = 'Keannu'
|
|
|
|
last_name = 'Bernasol'
|
|
|
|
# course = 'Bachelor of Science in Information Technology'
|
|
|
|
# year_level = '1st Year'
|
|
|
|
# semester = '1st Semester'
|
|
|
|
|
2023-07-18 16:01:19 +08:00
|
|
|
if not User.objects.filter(username=username).exists():
|
2023-07-18 15:51:06 +08:00
|
|
|
# Create the superuser with is_active set to False
|
2023-07-18 16:01:19 +08:00
|
|
|
user = User.objects.create_user(
|
2023-07-18 15:51:06 +08:00
|
|
|
username=username, email=email, password=password, first_name=first_name, last_name=last_name)
|
|
|
|
|
|
|
|
# Activate the superuser
|
|
|
|
user.is_active = True
|
|
|
|
user.save()
|