Polished subject url endpoints for better filtering. Added serializers for study_group

This commit is contained in:
Keannu Bernasol 2023-06-27 21:35:19 +08:00
parent 701dc55e31
commit 8c337295e2
5 changed files with 57 additions and 4 deletions

View file

@ -0,0 +1,23 @@
from rest_framework import serializers
from .models import StudyGroup, StudyGroupMembership
from accounts.models import CustomUser
class StudyGroupSerializer(serializers.ModelSerializer):
users = serializers.SlugRelatedField(
queryset=CustomUser.objects.all(), many=True, slug_field='name', allow_null=True)
subject = serializers.CharField(source='subject.Subject', read_only=True)
class Meta:
model = StudyGroup
fields = '__all__'
class StudyGroupMembershipSerializer(serializers.ModelSerializer):
user = serializers.CharField(source='accounts.CustomUser', read_only=True)
subject = serializers.CharField(
source='study_groups.StudyGroup', read_only=True)
class Meta:
model = StudyGroupMembership
fields = '__all__'

View file

View file

@ -1,3 +1,15 @@
from django.shortcuts import render
from rest_framework import generics
from .serializers import StudyGroupSerializer
from .models import StudyGroup
# Create your views here.
class StudyGroupListView(generics.ListAPIView):
serializer_class = StudyGroupSerializer
queryset = StudyGroup.objects.all()
class StudyGroupMembershipViewSet(generics.ListAPIView):
serializer_class = StudyGroupSerializer
queryset = StudyGroup.objects.all()

View file

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

View file

@ -10,7 +10,9 @@ class SubjectListView(generics.ListAPIView):
queryset = Subject.objects.all()
class SubjectByYearSemesterView(generics.ListAPIView):
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(
@ -20,3 +22,17 @@ class SubjectByYearSemesterView(generics.ListAPIView):
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):
# Retrieve the subjects based on year level and semester slugs
subjects = Subject.objects.filter(
year_level__shortname=year_slug, semester__shortname=semester_slug)
# Serialize the subjects
serializer = SubjectSerializer(subjects, many=True)
return Response(serializer.data)