Autocreate superuser from .env file as well as autogenerate year level and course records

This commit is contained in:
Keannu Bernasol 2023-06-27 15:41:34 +08:00
parent 48f6bee125
commit ceba84acb5
16 changed files with 174 additions and 4 deletions

View file

@ -0,0 +1,20 @@
# Generated by Django 4.2.2 on 2023-06-27 07:29
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('year_levels', '0001_initial'),
('accounts', '0004_alter_customuser_course'),
]
operations = [
migrations.AlterField(
model_name='customuser',
name='semester',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='year_levels.year_level'),
),
]

View file

@ -5,6 +5,10 @@ from django.utils.text import slugify
from django.db import models
import os
from courses.models import Course
from year_levels.models import Year_Level
from django.db.models.signals import post_migrate
from django.dispatch import receiver
from dotenv import load_dotenv
def validate_student_id(value):
@ -58,8 +62,26 @@ class CustomUser(AbstractUser):
on_delete=models.CASCADE,
null=True
)
semester = models.ForeignKey(
Year_Level,
on_delete=models.CASCADE,
null=True
)
@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')
if not User.objects.filter(username=username).exists():
User.objects.create_superuser(
username, email, password)

View file

@ -4,5 +4,6 @@ from django.urls import path, include
urlpatterns = [
path('accounts/', include('accounts.urls')),
path('student_status/', include('student_status.urls')),
path('courses/', include('courses.urls'))
path('courses/', include('courses.urls')),
path('year_levels/', include('year_levels.urls'))
]

View file

@ -64,6 +64,7 @@ INSTALLED_APPS = [
'accounts',
'student_status',
'courses',
'year_levels',
]
MIDDLEWARE = [

View file

@ -0,0 +1,23 @@
# Generated by Django 4.2.2 on 2023-06-27 07:31
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('courses', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='course',
old_name='course_name',
new_name='name',
),
migrations.RenameField(
model_name='course',
old_name='course_shortname',
new_name='shortname',
),
]

View file

@ -1,10 +1,26 @@
from django.db import models
from django.db.models.signals import post_migrate
from django.dispatch import receiver
# Create your models here.
class Course(models.Model):
course_name = models.CharField(max_length=64)
course_shortname = models.CharField(max_length=16)
name = models.CharField(max_length=64)
shortname = models.CharField(max_length=16)
def __str__(self):
return self.course_shortname
return self.shortname
@receiver(post_migrate)
def populate_courses(sender, **kwargs):
if sender.name == 'courses':
Course.objects.get_or_create(
name='Bachelor of Science in Information Technology', shortname='BSIT')
Course.objects.get_or_create(
name='Bachelor of Science in Computer Science', shortname='BSCS')
Course.objects.get_or_create(
name='Bachelor of Science in Computer Engineering ', shortname='BSCpE')
Course.objects.get_or_create(
name='Bachelor of Science in Data Science', shortname='BSDS')
# Add more predefined records as needed

View file

View file

@ -0,0 +1,5 @@
from django.contrib import admin
from .models import Year_Level
admin.site.register(Year_Level)

View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class YearLevelsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'year_levels'

View file

@ -0,0 +1,22 @@
# Generated by Django 4.2.2 on 2023-06-27 07:16
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Year_Level',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=64)),
('shortname', models.CharField(max_length=16)),
],
),
]

View file

View file

@ -0,0 +1,29 @@
from django.db import models
from django.db.models.signals import post_migrate
from django.dispatch import receiver
# Create your models here.
class Year_Level(models.Model):
name = models.CharField(max_length=64)
shortname = models.CharField(max_length=16)
def __str__(self):
return self.shortname
@receiver(post_migrate)
def populate_courses(sender, **kwargs):
if sender.name == 'year_levels':
Year_Level.objects.get_or_create(
name='1st Year', shortname='1st')
Year_Level.objects.get_or_create(
name='2nd Year', shortname='2nd')
Year_Level.objects.get_or_create(
name='3rd Year', shortname='3rd')
Year_Level.objects.get_or_create(
name='4th Year', shortname='3th')
Year_Level.objects.get_or_create(
name='Irregular', shortname='Irreg')
# Add more predefined records as needed

View file

@ -0,0 +1,8 @@
from rest_framework import serializers
from .models import Year_Level
class YearLevelSerializer(serializers.ModelSerializer):
class Meta:
model = Year_Level
fields = '__all__'

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,6 @@
from django.urls import include, path
from .views import CourseListView
urlpatterns = [
path('', CourseListView.as_view()),
]

View file

@ -0,0 +1,8 @@
from rest_framework import generics
from .models import Year_Level
from .serializers import YearLevelSerializer
class CourseListView(generics.ListAPIView):
serializer_class = YearLevelSerializer
queryset = Year_Level.objects.all()