From de1dabf53c2b7d340c38769a478afb97f716d67c Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 27 Jun 2023 15:48:43 +0800 Subject: [PATCH] Added semesters --- ...0006_alter_customuser_semester_and_more.py | 26 +++++++++++++++++++ stude/accounts/models.py | 25 +++++------------- stude/api/urls.py | 3 ++- stude/config/settings.py | 1 + stude/courses/models.py | 2 +- stude/semesters/__init__.py | 0 stude/semesters/admin.py | 5 ++++ stude/semesters/apps.py | 6 +++++ stude/semesters/migrations/0001_initial.py | 22 ++++++++++++++++ stude/semesters/migrations/__init__.py | 0 stude/semesters/models.py | 23 ++++++++++++++++ stude/semesters/serializers.py | 8 ++++++ stude/semesters/tests.py | 3 +++ stude/semesters/urls.py | 6 +++++ stude/semesters/views.py | 8 ++++++ stude/year_levels/models.py | 2 +- 16 files changed, 119 insertions(+), 21 deletions(-) create mode 100644 stude/accounts/migrations/0006_alter_customuser_semester_and_more.py create mode 100644 stude/semesters/__init__.py create mode 100644 stude/semesters/admin.py create mode 100644 stude/semesters/apps.py create mode 100644 stude/semesters/migrations/0001_initial.py create mode 100644 stude/semesters/migrations/__init__.py create mode 100644 stude/semesters/models.py create mode 100644 stude/semesters/serializers.py create mode 100644 stude/semesters/tests.py create mode 100644 stude/semesters/urls.py create mode 100644 stude/semesters/views.py diff --git a/stude/accounts/migrations/0006_alter_customuser_semester_and_more.py b/stude/accounts/migrations/0006_alter_customuser_semester_and_more.py new file mode 100644 index 0000000..4aa42ae --- /dev/null +++ b/stude/accounts/migrations/0006_alter_customuser_semester_and_more.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2.2 on 2023-06-27 07:45 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('year_levels', '0001_initial'), + ('semesters', '0001_initial'), + ('accounts', '0005_alter_customuser_semester'), + ] + + operations = [ + migrations.AlterField( + model_name='customuser', + name='semester', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='semesters.semester'), + ), + migrations.AlterField( + model_name='customuser', + name='year_level', + 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 320b40f..839abdf 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -6,6 +6,7 @@ from django.db import models import os from courses.models import Course from year_levels.models import Year_Level +from semesters.models import Semester from django.db.models.signals import post_migrate from django.dispatch import receiver from dotenv import load_dotenv @@ -19,19 +20,6 @@ def validate_student_id(value): class CustomUser(AbstractUser): - YEAR_LEVELS = ( - ('1st', '1st year'), - ('2nd', '2nd year'), - ('3rd', '3rd year'), - ('4th', '4th year'), - ('5th', '5th Year'), - ('Irreg', 'Irregular'), - ) - SEMESTERS = ( - ('1st', '1st semester'), - ('2nd', '2nd semester'), - ) - def _get_upload_to(instance, filename): base_filename, file_extension = os.path.splitext(filename) # Convert filename to a slug format @@ -52,21 +40,22 @@ class CustomUser(AbstractUser): is_banned = models.BooleanField(default=False) student_id_number = models.CharField( max_length=16, validators=[validate_student_id], null=False) - year_level = models.CharField( - max_length=50, choices=YEAR_LEVELS) - semester = models.CharField( - max_length=50, choices=SEMESTERS) avatar = models.ImageField(upload_to=_get_upload_to, null=True) course = models.ForeignKey( Course, on_delete=models.CASCADE, null=True ) - semester = models.ForeignKey( + year_level = models.ForeignKey( Year_Level, on_delete=models.CASCADE, null=True ) + semester = models.ForeignKey( + Semester, + on_delete=models.CASCADE, + null=True + ) @property def full_name(self): diff --git a/stude/api/urls.py b/stude/api/urls.py index 6cde9c0..198d773 100644 --- a/stude/api/urls.py +++ b/stude/api/urls.py @@ -5,5 +5,6 @@ urlpatterns = [ path('accounts/', include('accounts.urls')), path('student_status/', include('student_status.urls')), path('courses/', include('courses.urls')), - path('year_levels/', include('year_levels.urls')) + path('year_levels/', include('year_levels.urls')), + path('semesters/', include('semesters.urls')) ] diff --git a/stude/config/settings.py b/stude/config/settings.py index 394d177..b3d4740 100644 --- a/stude/config/settings.py +++ b/stude/config/settings.py @@ -65,6 +65,7 @@ INSTALLED_APPS = [ 'student_status', 'courses', 'year_levels', + 'semesters', ] MIDDLEWARE = [ diff --git a/stude/courses/models.py b/stude/courses/models.py index 9523b34..b139d56 100644 --- a/stude/courses/models.py +++ b/stude/courses/models.py @@ -9,7 +9,7 @@ class Course(models.Model): shortname = models.CharField(max_length=16) def __str__(self): - return self.shortname + return self.name @receiver(post_migrate) diff --git a/stude/semesters/__init__.py b/stude/semesters/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/semesters/admin.py b/stude/semesters/admin.py new file mode 100644 index 0000000..a36cc63 --- /dev/null +++ b/stude/semesters/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Semester + + +admin.site.register(Semester) diff --git a/stude/semesters/apps.py b/stude/semesters/apps.py new file mode 100644 index 0000000..336cf16 --- /dev/null +++ b/stude/semesters/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SemestersConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'semesters' diff --git a/stude/semesters/migrations/0001_initial.py b/stude/semesters/migrations/0001_initial.py new file mode 100644 index 0000000..f2c6632 --- /dev/null +++ b/stude/semesters/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.2 on 2023-06-27 07:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Semester', + 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/semesters/migrations/__init__.py b/stude/semesters/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/semesters/models.py b/stude/semesters/models.py new file mode 100644 index 0000000..0f69abd --- /dev/null +++ b/stude/semesters/models.py @@ -0,0 +1,23 @@ +from django.db import models +from django.db.models.signals import post_migrate +from django.dispatch import receiver + +# Create your models here. + + +class Semester(models.Model): + name = models.CharField(max_length=64) + shortname = models.CharField(max_length=16) + + def __str__(self): + return self.name + + +@receiver(post_migrate) +def populate_courses(sender, **kwargs): + if sender.name == 'semesters': + Semester.objects.get_or_create( + name='1st Semester', shortname='1st') + Semester.objects.get_or_create( + name='2nd Semester', shortname='2nd') + # Add more predefined records as needed diff --git a/stude/semesters/serializers.py b/stude/semesters/serializers.py new file mode 100644 index 0000000..41c35ca --- /dev/null +++ b/stude/semesters/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from .models import Semester + + +class SemesterSerializer(serializers.ModelSerializer): + class Meta: + model = Semester + fields = '__all__' diff --git a/stude/semesters/tests.py b/stude/semesters/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/stude/semesters/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/stude/semesters/urls.py b/stude/semesters/urls.py new file mode 100644 index 0000000..6e4d56a --- /dev/null +++ b/stude/semesters/urls.py @@ -0,0 +1,6 @@ +from django.urls import include, path +from .views import SemesterListView + +urlpatterns = [ + path('', SemesterListView.as_view()), +] diff --git a/stude/semesters/views.py b/stude/semesters/views.py new file mode 100644 index 0000000..d7ab49c --- /dev/null +++ b/stude/semesters/views.py @@ -0,0 +1,8 @@ +from rest_framework import generics +from .models import Semester +from .serializers import SemesterSerializer + + +class SemesterListView(generics.ListAPIView): + serializer_class = SemesterSerializer + queryset = Semester.objects.all() diff --git a/stude/year_levels/models.py b/stude/year_levels/models.py index 3eb5422..a0e2577 100644 --- a/stude/year_levels/models.py +++ b/stude/year_levels/models.py @@ -10,7 +10,7 @@ class Year_Level(models.Model): shortname = models.CharField(max_length=16) def __str__(self): - return self.shortname + return self.name @receiver(post_migrate)