Improved subject model

This commit is contained in:
Keannu Bernasol 2023-07-17 21:23:27 +08:00
parent e8693547cf
commit eb26533dcd
7 changed files with 72 additions and 25 deletions

View file

@ -0,0 +1,18 @@
# Generated by Django 4.2.3 on 2023-07-17 12:43
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('subjects', '0003_remove_subject_semester_remove_subject_year_level_and_more'),
]
operations = [
migrations.RenameField(
model_name='subject',
old_name='courses',
new_name='course',
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 4.2.3 on 2023-07-17 13:11
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('subjects', '0004_rename_courses_subject_course'),
]
operations = [
migrations.RenameField(
model_name='subject',
old_name='course',
new_name='courses',
),
]

View file

@ -0,0 +1,23 @@
# Generated by Django 4.2.3 on 2023-07-17 13:16
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('subjects', '0005_rename_course_subject_courses'),
]
operations = [
migrations.RenameField(
model_name='subject',
old_name='semester',
new_name='semesters',
),
migrations.RenameField(
model_name='subject',
old_name='year_level',
new_name='year_levels',
),
]

View file

@ -12,10 +12,10 @@ class Subject(models.Model):
students = models.ManyToManyField(
'accounts.CustomUser', through='subjects.SubjectStudent', related_name='SubjectStudent_subject')
year_level = models.ManyToManyField(
year_levels = models.ManyToManyField(
'year_levels.Year_Level', through='subjects.SubjectYearLevel', related_name='SubjectYearLevel_subject')
semester = models.ManyToManyField(
semesters = models.ManyToManyField(
'semesters.Semester', through='subjects.SubjectSemester', related_name='SubjectSemester_subject')
def __str__(self):

View file

@ -1,20 +1,24 @@
from rest_framework import serializers
from .models import Subject
from courses.models import Course
from year_levels.models import Year_Level
from semesters.models import Semester
from year_levels.serializers import YearLevelSerializer
from semesters.serializers import SemesterSerializer
from courses.serializers import CourseSerializer
class SubjectSerializer(serializers.ModelSerializer):
year_level = serializers.SerializerMethodField()
semester = serializers.SerializerMethodField()
year_levels = serializers.SlugRelatedField(
queryset=Year_Level.objects.all(), many=True, slug_field='name', allow_null=True)
semesters = serializers.SlugRelatedField(
queryset=Semester.objects.all(), many=True, slug_field='name', allow_null=True)
courses = serializers.SlugRelatedField(
queryset=Course.objects.all(), many=True, slug_field='name', allow_null=True)
class Meta:
model = Subject
fields = ('name', 'code', 'courses', 'year_level', 'semester')
fields = ('name', 'code', 'courses', 'year_levels', 'semesters')
def get_year_level(self, obj):
return obj.year_level.name

View file

@ -1,11 +1,9 @@
from django.urls import include, path
from .views import SubjectListView
from .views import SubjectByYearSemesterView, SubjectByYearView
from .views import SubjectByYearSemesterView
urlpatterns = [
path('', SubjectListView.as_view()),
path('<slug:year_slug>/',
SubjectByYearView.as_view()),
path('<slug:year_slug>/<slug:semester_slug>',
path('<slug:course_slug>/<slug:year_slug>/<slug:semester_slug>',
SubjectByYearSemesterView.as_view()),
]

View file

@ -10,27 +10,13 @@ class SubjectListView(generics.ListAPIView):
queryset = Subject.objects.all()
class SubjectByYearView(generics.ListAPIView):
queryset = Subject.objects.all()
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)
class SubjectByYearSemesterView(generics.ListAPIView):
queryset = Subject.objects.all()
def get(self, request, year_slug, semester_slug):
def get(self, request, course_slug, year_slug, semester_slug):
# Retrieve the subjects based on year level and semester slugs
subjects = Subject.objects.filter(
year_level__shortname=year_slug, semester__shortname=semester_slug)
courses__shortname=course_slug, year_levels__shortname=year_slug, semesters__shortname=semester_slug)
# Serialize the subjects
serializer = SubjectSerializer(subjects, many=True)