Borrowing-TrackerBackend/equipment_tracker/accounts/models.py
2024-01-24 22:38:59 +08:00

104 lines
3.9 KiB
Python

from django.db.models.signals import post_migrate
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.dispatch import receiver
import os
class CustomUser(AbstractUser):
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
is_active = models.BooleanField(default=False)
is_technician = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=False)
COURSE_CHOICES = (
('BS Chemistry', 'BS Chemistry'),
('BS Food Technology', 'BS Food Technology'),
('BS Applied Physics', 'BS Applied Physics'),
('BS Environmental Science', 'BS Environmental Science'),
('Not Applicable', 'Not Applicable'),
)
course = models.CharField(
max_length=60, null=True, choices=COURSE_CHOICES, default=None)
section = models.CharField(max_length=60, null=True, default=None)
@property
def full_name(self):
return f"{self.first_name} {self.last_name}"
pass
@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')
first_name = 'Admin'
last_name = 'Admin'
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, first_name=first_name, last_name=last_name, is_technician=True)
# Activate the superuser
superuser.is_active = True
print('Created admin account')
superuser.save()
username = 'test-technician'
email = os.getenv('DJANGO_ADMIN_EMAIL')
password = os.getenv('DJANGO_ADMIN_PASSWORD')
first_name = 'Test'
last_name = 'Technician'
if not User.objects.filter(username=username).exists():
# Create the superuser with is_active set to False
user = User.objects.create_user(
username=username, email=email, password=password, first_name=first_name, last_name=last_name, is_technician=True)
# Activate the user
user.is_active = True
print('Created debug technician account')
user.save()
username = 'test-teacher'
email = os.getenv('DJANGO_ADMIN_EMAIL')
password = os.getenv('DJANGO_ADMIN_PASSWORD')
first_name = 'Test'
last_name = 'Teacher'
if not User.objects.filter(username=username).exists():
# Create the superuser with is_active set to False
user = User.objects.create_user(
username=username, email=email, password=password, first_name=first_name, last_name=last_name, is_teacher=True)
# Activate the user
user.is_active = True
print('Created debug teacher account')
user.save()
username = 'test-student'
email = os.getenv('DJANGO_ADMIN_EMAIL')
password = os.getenv('DJANGO_ADMIN_PASSWORD')
first_name = 'Test'
last_name = 'Student'
if not User.objects.filter(username=username).exists():
# Create the superuser with is_active set to False
user = User.objects.create_user(
username=username, email=email, password=password, first_name=first_name, last_name=last_name, course="BS Chemistry")
# Activate the user
user.is_active = True
print('Created debug student account')
user.save()