2023-09-12 19:51:06 +08:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.decorators.cache import cache_page
|
2023-07-18 15:51:06 +08:00
|
|
|
from rest_framework import generics, viewsets
|
2023-09-03 17:46:36 +08:00
|
|
|
from .models import Subject, SubjectInstance
|
2023-09-04 18:18:08 +08:00
|
|
|
from .serializers import SubjectInstanceSerializer
|
2023-06-27 18:15:31 +08:00
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
2023-07-26 23:32:03 +08:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-06-27 18:15:31 +08:00
|
|
|
|
|
|
|
|
2023-07-26 23:32:03 +08:00
|
|
|
class SubjectListAllView(generics.ListAPIView):
|
2023-09-04 18:18:08 +08:00
|
|
|
serializer_class = SubjectInstanceSerializer
|
2023-09-03 17:46:36 +08:00
|
|
|
queryset = SubjectInstance.objects.all()
|
2023-06-27 18:15:31 +08:00
|
|
|
|
2023-09-12 19:51:06 +08:00
|
|
|
@method_decorator(cache_page(60*60))
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super().dispatch(*args, **kwargs)
|
|
|
|
|
2023-06-27 18:15:31 +08:00
|
|
|
|
2023-07-26 23:32:03 +08:00
|
|
|
class SubjectListView(generics.ListAPIView):
|
|
|
|
permission_classes = [IsAuthenticated]
|
2023-09-03 17:46:36 +08:00
|
|
|
queryset = SubjectInstance.objects.all()
|
2023-09-04 18:18:08 +08:00
|
|
|
serializer_class = SubjectInstanceSerializer
|
2023-07-26 23:32:03 +08:00
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
user = self.request.user
|
|
|
|
user_course = user.course
|
|
|
|
# If user is irregular, show all subjects in his/her course to choose from
|
|
|
|
if (user.irregular):
|
2023-09-03 17:46:36 +08:00
|
|
|
subjects = SubjectInstance.objects.filter(
|
2023-07-26 23:32:03 +08:00
|
|
|
course__name=user_course)
|
|
|
|
|
|
|
|
# Else, return subjects that match the user's student info (Year Level, Semster, Course)
|
|
|
|
else:
|
|
|
|
user_yearlevel = user.year_level
|
|
|
|
user_semester = user.semester
|
2023-09-03 17:46:36 +08:00
|
|
|
subjects = SubjectInstance.objects.filter(
|
2023-07-26 23:32:03 +08:00
|
|
|
course__name=user.course, year_level__name=user_yearlevel, semester__name=user_semester)
|
|
|
|
|
|
|
|
# Serialize the subjects
|
2023-09-04 18:18:08 +08:00
|
|
|
serializer = SubjectInstanceSerializer(subjects, many=True)
|
2023-07-26 23:32:03 +08:00
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
2023-07-25 16:29:11 +08:00
|
|
|
class SubjectByCourseView(generics.ListAPIView):
|
2023-09-03 17:46:36 +08:00
|
|
|
queryset = SubjectInstance.objects.all()
|
2023-09-04 18:18:08 +08:00
|
|
|
serializer_class = SubjectInstanceSerializer
|
2023-07-25 16:29:11 +08:00
|
|
|
|
|
|
|
def get(self, request, course_slug):
|
2023-07-26 12:14:19 +08:00
|
|
|
# Retrieve the subjects based on course slug
|
2023-09-03 17:46:36 +08:00
|
|
|
subjects = SubjectInstance.objects.filter(
|
2023-07-26 12:14:19 +08:00
|
|
|
course__shortname=course_slug)
|
2023-07-25 16:29:11 +08:00
|
|
|
|
|
|
|
# Serialize the subjects
|
2023-09-04 18:18:08 +08:00
|
|
|
serializer = SubjectInstanceSerializer(subjects, many=True)
|
2023-07-25 16:29:11 +08:00
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
2023-07-24 01:47:01 +08:00
|
|
|
class SubjectByCourseYearSemesterView(generics.ListAPIView):
|
2023-09-04 18:18:08 +08:00
|
|
|
queryset = SubjectInstance.objects.all()
|
|
|
|
serializer_class = SubjectInstanceSerializer
|
2023-06-27 21:35:19 +08:00
|
|
|
|
2023-07-17 21:23:27 +08:00
|
|
|
def get(self, request, course_slug, year_slug, semester_slug):
|
2023-06-27 21:35:19 +08:00
|
|
|
# Retrieve the subjects based on year level and semester slugs
|
2023-09-04 18:18:08 +08:00
|
|
|
subjects = SubjectInstance.objects.filter(
|
2023-07-26 12:14:19 +08:00
|
|
|
course__shortname=course_slug, year_level__shortname=year_slug, semester__shortname=semester_slug)
|
2023-06-27 21:35:19 +08:00
|
|
|
# Serialize the subjects
|
2023-09-04 18:18:08 +08:00
|
|
|
serializer = SubjectInstanceSerializer(subjects, many=True)
|
2023-06-27 21:35:19 +08:00
|
|
|
return Response(serializer.data)
|