mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Autocreate superuser from .env file as well as autogenerate year level and course records
This commit is contained in:
parent
48f6bee125
commit
ceba84acb5
16 changed files with 174 additions and 4 deletions
20
stude/accounts/migrations/0005_alter_customuser_semester.py
Normal file
20
stude/accounts/migrations/0005_alter_customuser_semester.py
Normal 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'),
|
||||
),
|
||||
]
|
|
@ -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)
|
||||
|
|
|
@ -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'))
|
||||
]
|
||||
|
|
|
@ -64,6 +64,7 @@ INSTALLED_APPS = [
|
|||
'accounts',
|
||||
'student_status',
|
||||
'courses',
|
||||
'year_levels',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
|
|
@ -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',
|
||||
),
|
||||
]
|
|
@ -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
|
||||
|
|
0
stude/year_levels/__init__.py
Normal file
0
stude/year_levels/__init__.py
Normal file
5
stude/year_levels/admin.py
Normal file
5
stude/year_levels/admin.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from django.contrib import admin
|
||||
from .models import Year_Level
|
||||
|
||||
|
||||
admin.site.register(Year_Level)
|
6
stude/year_levels/apps.py
Normal file
6
stude/year_levels/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class YearLevelsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'year_levels'
|
22
stude/year_levels/migrations/0001_initial.py
Normal file
22
stude/year_levels/migrations/0001_initial.py
Normal 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)),
|
||||
],
|
||||
),
|
||||
]
|
0
stude/year_levels/migrations/__init__.py
Normal file
0
stude/year_levels/migrations/__init__.py
Normal file
29
stude/year_levels/models.py
Normal file
29
stude/year_levels/models.py
Normal 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
|
8
stude/year_levels/serializers.py
Normal file
8
stude/year_levels/serializers.py
Normal 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__'
|
3
stude/year_levels/tests.py
Normal file
3
stude/year_levels/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
6
stude/year_levels/urls.py
Normal file
6
stude/year_levels/urls.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.urls import include, path
|
||||
from .views import CourseListView
|
||||
|
||||
urlpatterns = [
|
||||
path('', CourseListView.as_view()),
|
||||
]
|
8
stude/year_levels/views.py
Normal file
8
stude/year_levels/views.py
Normal 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()
|
Loading…
Reference in a new issue