From 97f8f6b9bcf07f3ada6c76e981ea960f84f50b31 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sat, 22 Apr 2023 15:31:58 +0800 Subject: [PATCH] Moved year level endpoints to schedules --- .../migrations/0006_schedule_year_level.py | 19 ++++++++++++++ infotech/schedules/models.py | 4 +++ infotech/schedules/serializers.py | 6 ++--- infotech/schedules/urls.py | 4 +++ infotech/schedules/views.py | 26 ++++++++++++++++++- infotech/subjects/urls.py | 4 --- 6 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 infotech/schedules/migrations/0006_schedule_year_level.py diff --git a/infotech/schedules/migrations/0006_schedule_year_level.py b/infotech/schedules/migrations/0006_schedule_year_level.py new file mode 100644 index 0000000..e3f3414 --- /dev/null +++ b/infotech/schedules/migrations/0006_schedule_year_level.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2 on 2023-04-22 07:21 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('schedules', '0005_schedule_max_slots'), + ] + + operations = [ + migrations.AddField( + model_name='schedule', + name='year_level', + field=models.TextField(default='Placeholder Year Level'), + preserve_default=False, + ), + ] diff --git a/infotech/schedules/models.py b/infotech/schedules/models.py index 7c4e8f6..f3601f1 100644 --- a/infotech/schedules/models.py +++ b/infotech/schedules/models.py @@ -7,10 +7,13 @@ from django.utils.timezone import now class Schedule(models.Model): name = models.TextField() subject = models.ForeignKey('subjects.Subject', on_delete=models.CASCADE) + year_level = models.TextField() + students_assigned = models.ManyToManyField( 'students.Student', related_name='StudentSchedule_student_assigned', through='schedules.StudentSchedule') professor = models.OneToOneField( 'professors.Professor', related_name='Professor_full_name', on_delete=models.CASCADE) + daytimes = models.ForeignKey( 'daytimes.DayTime', related_name='DayTime_full_name', on_delete=models.CASCADE, null=True) @@ -19,6 +22,7 @@ class Schedule(models.Model): def save(self, *args, **kwargs): self.name = f"{self.subject} : {self.professor}" + self.year_level = self.subject.year_level super().save(*args, **kwargs) def __str__(self): diff --git a/infotech/schedules/serializers.py b/infotech/schedules/serializers.py index 90dc2fe..8faa096 100644 --- a/infotech/schedules/serializers.py +++ b/infotech/schedules/serializers.py @@ -24,14 +24,14 @@ class ScheduleSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Schedule - fields = ('id', 'name', 'max_slots', 'subject', 'daytimes', 'students_assigned', + fields = ('id', 'name', 'max_slots', 'subject', 'year_level', 'daytimes', 'students_assigned', 'professor', 'date_created') - read_only_fields = ('id', 'date_created', 'name') + read_only_fields = ('id', 'date_created', 'name', 'year_level') def validate(self, attrs): students = attrs.get('students') max_slots = attrs.get('max_slots') - if students.count() > max_slots: + if students and students.count() > max_slots: raise serializers.ValidationError( 'Too many students for this subject') return attrs diff --git a/infotech/schedules/urls.py b/infotech/schedules/urls.py index 26e31be..a11536a 100644 --- a/infotech/schedules/urls.py +++ b/infotech/schedules/urls.py @@ -9,4 +9,8 @@ router.register(r'schedules', views.ScheduleViewSet) # Additionally, we include login URLs for the browsable API. urlpatterns = [ path('', include(router.urls)), + path('first_year_schedules/', views.FirstYearScheduleViewSet.as_view()), + path('second_year_subjects/', views.SecondYearScheduleViewSet.as_view()), + path('third_year_schedules/', views.ThirdYearScheduleViewSet.as_view()), + path('fourth_year_schedules/', views.FourthYearScheduleViewSet.as_view()), ] diff --git a/infotech/schedules/views.py b/infotech/schedules/views.py index 92fb678..9402ddc 100644 --- a/infotech/schedules/views.py +++ b/infotech/schedules/views.py @@ -2,7 +2,7 @@ from django.shortcuts import render # Create your views here. from rest_framework.permissions import IsAuthenticated -from rest_framework import viewsets +from rest_framework import viewsets, generics from .serializers import ScheduleSerializer from .models import Schedule, StudentSchedule @@ -11,3 +11,27 @@ class ScheduleViewSet(viewsets.ModelViewSet): # permission_classes = [IsAuthenticated] serializer_class = ScheduleSerializer queryset = Schedule.objects.all() + + +class FirstYearScheduleViewSet(generics.ListAPIView): + # permission_classes = [IsAuthenticated] + serializer_class = ScheduleSerializer + queryset = Schedule.objects.filter(year_level='1st Year') + + +class SecondYearScheduleViewSet(generics.ListAPIView): + # permission_classes = [IsAuthenticated] + serializer_class = ScheduleSerializer + queryset = Schedule.objects.filter(year_level='2nd Year') + + +class ThirdYearScheduleViewSet(generics.ListAPIView): + # permission_classes = [IsAuthenticated] + serializer_class = ScheduleSerializer + queryset = Schedule.objects.filter(year_level='3rd Year') + + +class FourthYearScheduleViewSet(generics.ListAPIView): + # permission_classes = [IsAuthenticated] + serializer_class = ScheduleSerializer + queryset = Schedule.objects.filter(year_level='4th Year') diff --git a/infotech/subjects/urls.py b/infotech/subjects/urls.py index b8df367..81bf59e 100644 --- a/infotech/subjects/urls.py +++ b/infotech/subjects/urls.py @@ -8,8 +8,4 @@ router.register(r'subjects', views.SubjectViewSet) # Additionally, we include login URLs for the browsable API. urlpatterns = [ path('', include(router.urls)), - path('first_year_subjects/', views.FirstYearSubjectViewSet.as_view()), - path('second_year_subjects/', views.SecondYearSubjectViewSet.as_view()), - path('third_year_subjects/', views.ThirdYearSubjectViewSet.as_view()), - path('fourth_year_subjects/', views.FourthYearSubjectViewSet.as_view()), ]