From 492a605557bc0f079e4f707a5dd2f66b01fde625 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 27 Jun 2023 18:15:31 +0800 Subject: [PATCH] Further polishing for subjects --- stude/accounts/migrations/0001_initial.py | 8 +--- .../0002_alter_customuser_course.py | 20 --------- stude/accounts/migrations/0002_initial.py | 45 +++++++++++++++++++ .../0003_alter_customuser_course.py | 20 --------- .../0004_alter_customuser_course.py | 20 --------- .../0005_alter_customuser_semester.py | 20 --------- ...0006_alter_customuser_semester_and_more.py | 26 ----------- stude/api/urls.py | 3 +- stude/config/settings.py | 1 + stude/courses/migrations/0001_initial.py | 14 ++++-- stude/courses/migrations/0002_initial.py | 27 +++++++++++ ...rename_course_name_course_name_and_more.py | 23 ---------- stude/courses/models.py | 10 ++++- stude/semesters/migrations/0001_initial.py | 2 +- stude/semesters/models.py | 6 +-- .../student_status/migrations/0001_initial.py | 2 +- stude/subjects/__init__.py | 0 stude/subjects/admin.py | 5 +++ stude/subjects/apps.py | 6 +++ stude/subjects/migrations/0001_initial.py | 29 ++++++++++++ stude/subjects/migrations/__init__.py | 0 stude/subjects/models.py | 26 +++++++++++ stude/subjects/serializers.py | 19 ++++++++ stude/subjects/tests.py | 3 ++ stude/subjects/urls.py | 9 ++++ stude/subjects/views.py | 22 +++++++++ stude/year_levels/migrations/0001_initial.py | 2 +- stude/year_levels/models.py | 8 ++-- 28 files changed, 225 insertions(+), 151 deletions(-) delete mode 100644 stude/accounts/migrations/0002_alter_customuser_course.py create mode 100644 stude/accounts/migrations/0002_initial.py delete mode 100644 stude/accounts/migrations/0003_alter_customuser_course.py delete mode 100644 stude/accounts/migrations/0004_alter_customuser_course.py delete mode 100644 stude/accounts/migrations/0005_alter_customuser_semester.py delete mode 100644 stude/accounts/migrations/0006_alter_customuser_semester_and_more.py create mode 100644 stude/courses/migrations/0002_initial.py delete mode 100644 stude/courses/migrations/0002_rename_course_name_course_name_and_more.py create mode 100644 stude/subjects/__init__.py create mode 100644 stude/subjects/admin.py create mode 100644 stude/subjects/apps.py create mode 100644 stude/subjects/migrations/0001_initial.py create mode 100644 stude/subjects/migrations/__init__.py create mode 100644 stude/subjects/models.py create mode 100644 stude/subjects/serializers.py create mode 100644 stude/subjects/tests.py create mode 100644 stude/subjects/urls.py create mode 100644 stude/subjects/views.py diff --git a/stude/accounts/migrations/0001_initial.py b/stude/accounts/migrations/0001_initial.py index c72d83e..3036706 100644 --- a/stude/accounts/migrations/0001_initial.py +++ b/stude/accounts/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 05:15 +# Generated by Django 4.2.2 on 2023-06-27 09:44 import accounts.models import django.contrib.auth.models @@ -12,7 +12,6 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('auth', '0012_alter_user_first_name_max_length'), ] operations = [ @@ -34,12 +33,7 @@ class Migration(migrations.Migration): ('is_studying', models.BooleanField(default=False)), ('is_banned', models.BooleanField(default=False)), ('student_id_number', models.CharField(max_length=16, validators=[accounts.models.validate_student_id])), - ('year_level', models.CharField(choices=[('1st', '1st year'), ('2nd', '2nd year'), ('3rd', '3rd year'), ('4th', '4th year'), ('5th', '5th Year'), ('Irreg', 'Irregular')], max_length=50)), - ('semester', models.CharField(choices=[('1st', '1st semester'), ('2nd', '2nd semester')], max_length=50)), - ('course', models.CharField(choices=[('BSIT', 'Bachelor of Science in Information Technology'), ('BSCS', 'Bachelor of Science in Computer Science'), ('BSCpE', 'Bachelor of Science in Computer Engineering')], max_length=50)), ('avatar', models.ImageField(null=True, upload_to=accounts.models.CustomUser._get_upload_to)), - ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), - ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), ], options={ 'verbose_name': 'user', diff --git a/stude/accounts/migrations/0002_alter_customuser_course.py b/stude/accounts/migrations/0002_alter_customuser_course.py deleted file mode 100644 index 107f8aa..0000000 --- a/stude/accounts/migrations/0002_alter_customuser_course.py +++ /dev/null @@ -1,20 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-27 06:29 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('courses', '0001_initial'), - ('accounts', '0001_initial'), - ] - - operations = [ - migrations.AlterField( - model_name='customuser', - name='course', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='courses.course'), - ), - ] diff --git a/stude/accounts/migrations/0002_initial.py b/stude/accounts/migrations/0002_initial.py new file mode 100644 index 0000000..0b53397 --- /dev/null +++ b/stude/accounts/migrations/0002_initial.py @@ -0,0 +1,45 @@ +# Generated by Django 4.2.2 on 2023-06-27 09:44 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0012_alter_user_first_name_max_length'), + ('year_levels', '0001_initial'), + ('semesters', '0001_initial'), + ('accounts', '0001_initial'), + ('courses', '0002_initial'), + ] + + operations = [ + migrations.AddField( + model_name='customuser', + name='course', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.course'), + ), + migrations.AddField( + model_name='customuser', + name='groups', + field=models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups'), + ), + migrations.AddField( + model_name='customuser', + name='semester', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='semesters.semester'), + ), + migrations.AddField( + model_name='customuser', + name='user_permissions', + field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions'), + ), + migrations.AddField( + 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/migrations/0003_alter_customuser_course.py b/stude/accounts/migrations/0003_alter_customuser_course.py deleted file mode 100644 index 377f458..0000000 --- a/stude/accounts/migrations/0003_alter_customuser_course.py +++ /dev/null @@ -1,20 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-27 06:31 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('courses', '0001_initial'), - ('accounts', '0002_alter_customuser_course'), - ] - - operations = [ - migrations.AlterField( - model_name='customuser', - name='course', - field=models.ForeignKey(default='BSIT', on_delete=django.db.models.deletion.CASCADE, to='courses.course'), - ), - ] diff --git a/stude/accounts/migrations/0004_alter_customuser_course.py b/stude/accounts/migrations/0004_alter_customuser_course.py deleted file mode 100644 index b19ea6d..0000000 --- a/stude/accounts/migrations/0004_alter_customuser_course.py +++ /dev/null @@ -1,20 +0,0 @@ -# Generated by Django 4.2.2 on 2023-06-27 06:34 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('courses', '0001_initial'), - ('accounts', '0003_alter_customuser_course'), - ] - - operations = [ - migrations.AlterField( - model_name='customuser', - name='course', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.course'), - ), - ] diff --git a/stude/accounts/migrations/0005_alter_customuser_semester.py b/stude/accounts/migrations/0005_alter_customuser_semester.py deleted file mode 100644 index c78e270..0000000 --- a/stude/accounts/migrations/0005_alter_customuser_semester.py +++ /dev/null @@ -1,20 +0,0 @@ -# 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/migrations/0006_alter_customuser_semester_and_more.py b/stude/accounts/migrations/0006_alter_customuser_semester_and_more.py deleted file mode 100644 index 4aa42ae..0000000 --- a/stude/accounts/migrations/0006_alter_customuser_semester_and_more.py +++ /dev/null @@ -1,26 +0,0 @@ -# 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/api/urls.py b/stude/api/urls.py index 198d773..92865d0 100644 --- a/stude/api/urls.py +++ b/stude/api/urls.py @@ -6,5 +6,6 @@ urlpatterns = [ path('student_status/', include('student_status.urls')), path('courses/', include('courses.urls')), path('year_levels/', include('year_levels.urls')), - path('semesters/', include('semesters.urls')) + path('semesters/', include('semesters.urls')), + path('subjects/', include('subjects.urls')) ] diff --git a/stude/config/settings.py b/stude/config/settings.py index b3d4740..bfee7f0 100644 --- a/stude/config/settings.py +++ b/stude/config/settings.py @@ -66,6 +66,7 @@ INSTALLED_APPS = [ 'courses', 'year_levels', 'semesters', + 'subjects', ] MIDDLEWARE = [ diff --git a/stude/courses/migrations/0001_initial.py b/stude/courses/migrations/0001_initial.py index 29cd714..f9bf4c5 100644 --- a/stude/courses/migrations/0001_initial.py +++ b/stude/courses/migrations/0001_initial.py @@ -1,6 +1,7 @@ -# Generated by Django 4.2.2 on 2023-06-27 05:57 +# Generated by Django 4.2.2 on 2023-06-27 09:44 from django.db import migrations, models +import django.db.models.deletion class Migration(migrations.Migration): @@ -15,8 +16,15 @@ class Migration(migrations.Migration): name='Course', fields=[ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('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)), + ], + ), + migrations.CreateModel( + name='SubjectCourse', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('course', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='courses.course')), ], ), ] diff --git a/stude/courses/migrations/0002_initial.py b/stude/courses/migrations/0002_initial.py new file mode 100644 index 0000000..f69407a --- /dev/null +++ b/stude/courses/migrations/0002_initial.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2.2 on 2023-06-27 09:44 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('courses', '0001_initial'), + ('subjects', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='subjectcourse', + name='subject', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='subjects.subject'), + ), + migrations.AddField( + model_name='course', + name='subjects', + field=models.ManyToManyField(related_name='SubjectCourse_course', through='courses.SubjectCourse', to='subjects.subject'), + ), + ] 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 deleted file mode 100644 index 2760ce3..0000000 --- a/stude/courses/migrations/0002_rename_course_name_course_name_and_more.py +++ /dev/null @@ -1,23 +0,0 @@ -# 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 b139d56..f31467c 100644 --- a/stude/courses/models.py +++ b/stude/courses/models.py @@ -1,17 +1,25 @@ -from django.db import models from django.db.models.signals import post_migrate from django.dispatch import receiver +from django.db import models # Create your models here. class Course(models.Model): name = models.CharField(max_length=64) shortname = models.CharField(max_length=16) + subjects = models.ManyToManyField( + 'subjects.Subject', related_name='SubjectCourse_course', through='courses.SubjectCourse') def __str__(self): return self.name +class SubjectCourse(models.Model): + subject = models.ForeignKey('subjects.Subject', on_delete=models.CASCADE) + course = models.ForeignKey( + 'courses.Course', on_delete=models.CASCADE, null=True) + + @receiver(post_migrate) def populate_courses(sender, **kwargs): if sender.name == 'courses': diff --git a/stude/semesters/migrations/0001_initial.py b/stude/semesters/migrations/0001_initial.py index f2c6632..dfc4049 100644 --- a/stude/semesters/migrations/0001_initial.py +++ b/stude/semesters/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 07:45 +# Generated by Django 4.2.2 on 2023-06-27 09:44 from django.db import migrations, models diff --git a/stude/semesters/models.py b/stude/semesters/models.py index 0f69abd..162c5b8 100644 --- a/stude/semesters/models.py +++ b/stude/semesters/models.py @@ -14,10 +14,10 @@ class Semester(models.Model): @receiver(post_migrate) -def populate_courses(sender, **kwargs): +def populate_semesters(sender, **kwargs): if sender.name == 'semesters': Semester.objects.get_or_create( - name='1st Semester', shortname='1st') + name='1st Semester', shortname='1stSem') Semester.objects.get_or_create( - name='2nd Semester', shortname='2nd') + name='2nd Semester', shortname='2ndSem') # Add more predefined records as needed diff --git a/stude/student_status/migrations/0001_initial.py b/stude/student_status/migrations/0001_initial.py index 7da3ffe..a45be78 100644 --- a/stude/student_status/migrations/0001_initial.py +++ b/stude/student_status/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 05:15 +# Generated by Django 4.2.2 on 2023-06-27 09:44 from django.conf import settings from django.db import migrations, models diff --git a/stude/subjects/__init__.py b/stude/subjects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/subjects/admin.py b/stude/subjects/admin.py new file mode 100644 index 0000000..6014caa --- /dev/null +++ b/stude/subjects/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Subject + + +admin.site.register(Subject) diff --git a/stude/subjects/apps.py b/stude/subjects/apps.py new file mode 100644 index 0000000..c3bdd68 --- /dev/null +++ b/stude/subjects/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SubjectsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'subjects' diff --git a/stude/subjects/migrations/0001_initial.py b/stude/subjects/migrations/0001_initial.py new file mode 100644 index 0000000..6fef778 --- /dev/null +++ b/stude/subjects/migrations/0001_initial.py @@ -0,0 +1,29 @@ +# Generated by Django 4.2.2 on 2023-06-27 09:44 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('year_levels', '0001_initial'), + ('courses', '0001_initial'), + ('semesters', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Subject', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=64)), + ('code', models.CharField(max_length=16)), + ('courses', models.ManyToManyField(related_name='SubjectCourse_subject', through='courses.SubjectCourse', to='courses.course')), + ('semester', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='semesters.semester')), + ('year_level', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='Year_Level_name', to='year_levels.year_level')), + ], + ), + ] diff --git a/stude/subjects/migrations/__init__.py b/stude/subjects/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/stude/subjects/models.py b/stude/subjects/models.py new file mode 100644 index 0000000..ece9113 --- /dev/null +++ b/stude/subjects/models.py @@ -0,0 +1,26 @@ +from django.db import models +from year_levels.models import Year_Level +from semesters.models import Semester +# Create your models here. + + +class Subject(models.Model): + name = models.CharField(max_length=64) + code = models.CharField(max_length=16) + courses = models.ManyToManyField( + 'courses.Course', through='courses.SubjectCourse', related_name='SubjectCourse_subject') + + year_level = models.ForeignKey( + Year_Level, + on_delete=models.SET_NULL, + null=True, + related_name='Year_Level_name' + ) + semester = models.ForeignKey( + Semester, + on_delete=models.SET_NULL, + null=True + ) + + def __str__(self): + return self.name diff --git a/stude/subjects/serializers.py b/stude/subjects/serializers.py new file mode 100644 index 0000000..da21f6b --- /dev/null +++ b/stude/subjects/serializers.py @@ -0,0 +1,19 @@ +from rest_framework import serializers +from .models import Subject +from year_levels.serializers import YearLevelSerializer +from semesters.serializers import SemesterSerializer + + +class SubjectSerializer(serializers.ModelSerializer): + year_level = serializers.SerializerMethodField() + semester = serializers.SerializerMethodField() + + class Meta: + model = Subject + fields = '__all__' + + def get_year_level(self, obj): + return obj.year_level.name + + def get_semester(self, obj): + return obj.semester.name diff --git a/stude/subjects/tests.py b/stude/subjects/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/stude/subjects/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/stude/subjects/urls.py b/stude/subjects/urls.py new file mode 100644 index 0000000..f7115ff --- /dev/null +++ b/stude/subjects/urls.py @@ -0,0 +1,9 @@ +from django.urls import include, path +from .views import SubjectListView +from .views import SubjectByYearSemesterView + +urlpatterns = [ + path('', SubjectListView.as_view()), + path('subjects//', + SubjectByYearSemesterView.as_view()), +] diff --git a/stude/subjects/views.py b/stude/subjects/views.py new file mode 100644 index 0000000..c11c6dc --- /dev/null +++ b/stude/subjects/views.py @@ -0,0 +1,22 @@ +from rest_framework import generics +from .models import Subject +from .serializers import SubjectSerializer +from rest_framework.views import APIView +from rest_framework.response import Response + + +class SubjectListView(generics.ListAPIView): + serializer_class = SubjectSerializer + queryset = Subject.objects.all() + + +class SubjectByYearSemesterView(generics.ListAPIView): + def get(self, request, year_slug): + # Retrieve the subjects based on year level and semester slugs + subjects = Subject.objects.filter( + year_level__shortname=year_slug) + + # Serialize the subjects + serializer = SubjectSerializer(subjects, many=True) + + return Response(serializer.data) diff --git a/stude/year_levels/migrations/0001_initial.py b/stude/year_levels/migrations/0001_initial.py index 6110790..5ff8b57 100644 --- a/stude/year_levels/migrations/0001_initial.py +++ b/stude/year_levels/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.2 on 2023-06-27 07:16 +# Generated by Django 4.2.2 on 2023-06-27 09:44 from django.db import migrations, models diff --git a/stude/year_levels/models.py b/stude/year_levels/models.py index a0e2577..02ddafb 100644 --- a/stude/year_levels/models.py +++ b/stude/year_levels/models.py @@ -17,13 +17,13 @@ class Year_Level(models.Model): def populate_courses(sender, **kwargs): if sender.name == 'year_levels': Year_Level.objects.get_or_create( - name='1st Year', shortname='1st') + name='1st Year', shortname='1stYr') Year_Level.objects.get_or_create( - name='2nd Year', shortname='2nd') + name='2nd Year', shortname='2ndYr') Year_Level.objects.get_or_create( - name='3rd Year', shortname='3rd') + name='3rd Year', shortname='3rdYr') Year_Level.objects.get_or_create( - name='4th Year', shortname='3th') + name='4th Year', shortname='4thYr') Year_Level.objects.get_or_create( name='Irregular', shortname='Irreg') # Add more predefined records as needed