From ceba84acb560b2570771d8de2017b2d25a79325b Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 27 Jun 2023 15:41:34 +0800 Subject: [PATCH] Autocreate superuser from .env file as well as autogenerate year level and course records --- .../0005_alter_customuser_semester.py | 20 +++++++++++++ stude/accounts/models.py | 22 ++++++++++++++ stude/api/urls.py | 3 +- stude/config/settings.py | 1 + ...rename_course_name_course_name_and_more.py | 23 +++++++++++++++ stude/courses/models.py | 22 ++++++++++++-- stude/year_levels/__init__.py | 0 stude/year_levels/admin.py | 5 ++++ stude/year_levels/apps.py | 6 ++++ stude/year_levels/migrations/0001_initial.py | 22 ++++++++++++++ stude/year_levels/migrations/__init__.py | 0 stude/year_levels/models.py | 29 +++++++++++++++++++ stude/year_levels/serializers.py | 8 +++++ stude/year_levels/tests.py | 3 ++ stude/year_levels/urls.py | 6 ++++ stude/year_levels/views.py | 8 +++++ 16 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 stude/accounts/migrations/0005_alter_customuser_semester.py create mode 100644 stude/courses/migrations/0002_rename_course_name_course_name_and_more.py create mode 100644 stude/year_levels/__init__.py create mode 100644 stude/year_levels/admin.py create mode 100644 stude/year_levels/apps.py create mode 100644 stude/year_levels/migrations/0001_initial.py create mode 100644 stude/year_levels/migrations/__init__.py create mode 100644 stude/year_levels/models.py create mode 100644 stude/year_levels/serializers.py create mode 100644 stude/year_levels/tests.py create mode 100644 stude/year_levels/urls.py create mode 100644 stude/year_levels/views.py diff --git a/stude/accounts/migrations/0005_alter_customuser_semester.py b/stude/accounts/migrations/0005_alter_customuser_semester.py new file mode 100644 index 0000000..c78e270 --- /dev/null +++ b/stude/accounts/migrations/0005_alter_customuser_semester.py @@ -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'), + ), + ] diff --git a/stude/accounts/models.py b/stude/accounts/models.py index 9d8fdf8..320b40f 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -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) diff --git a/stude/api/urls.py b/stude/api/urls.py index 7fa8dcc..6cde9c0 100644 --- a/stude/api/urls.py +++ b/stude/api/urls.py @@ -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')) ] diff --git a/stude/config/settings.py b/stude/config/settings.py index 39016d6..394d177 100644 --- a/stude/config/settings.py +++ b/stude/config/settings.py @@ -64,6 +64,7 @@ INSTALLED_APPS = [ 'accounts', 'student_status', 'courses', + 'year_levels', ] MIDDLEWARE = [ diff --git a/stude/courses/migrations/0002_rename_course_name_course_name_and_more.py b/stude/courses/migrations/0002_rename_course_name_course_name_and_more.py new file mode 100644 index 0000000..2760ce3 --- /dev/null +++ b/stude/courses/migrations/0002_rename_course_name_course_name_and_more.py @@ -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', + ), + ] diff --git a/stude/courses/models.py b/stude/courses/models.py index 6f49b09..9523b34 100644 --- a/stude/courses/models.py +++ b/stude/courses/models.py @@ -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 diff --git a/stude/year_levels/__init__.py b/stude/year_levels/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/year_levels/admin.py b/stude/year_levels/admin.py new file mode 100644 index 0000000..ee75669 --- /dev/null +++ b/stude/year_levels/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Year_Level + + +admin.site.register(Year_Level) diff --git a/stude/year_levels/apps.py b/stude/year_levels/apps.py new file mode 100644 index 0000000..c047d6d --- /dev/null +++ b/stude/year_levels/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class YearLevelsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'year_levels' diff --git a/stude/year_levels/migrations/0001_initial.py b/stude/year_levels/migrations/0001_initial.py new file mode 100644 index 0000000..6110790 --- /dev/null +++ b/stude/year_levels/migrations/0001_initial.py @@ -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)), + ], + ), + ] diff --git a/stude/year_levels/migrations/__init__.py b/stude/year_levels/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/year_levels/models.py b/stude/year_levels/models.py new file mode 100644 index 0000000..3eb5422 --- /dev/null +++ b/stude/year_levels/models.py @@ -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 diff --git a/stude/year_levels/serializers.py b/stude/year_levels/serializers.py new file mode 100644 index 0000000..1cb21b7 --- /dev/null +++ b/stude/year_levels/serializers.py @@ -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__' diff --git a/stude/year_levels/tests.py b/stude/year_levels/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/stude/year_levels/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/stude/year_levels/urls.py b/stude/year_levels/urls.py new file mode 100644 index 0000000..a0b193d --- /dev/null +++ b/stude/year_levels/urls.py @@ -0,0 +1,6 @@ +from django.urls import include, path +from .views import CourseListView + +urlpatterns = [ + path('', CourseListView.as_view()), +] diff --git a/stude/year_levels/views.py b/stude/year_levels/views.py new file mode 100644 index 0000000..c7b8358 --- /dev/null +++ b/stude/year_levels/views.py @@ -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()