mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Polished subject url endpoints for better filtering. Added serializers for study_group
This commit is contained in:
parent
701dc55e31
commit
8c337295e2
5 changed files with 57 additions and 4 deletions
23
stude/study_groups/serializers.py
Normal file
23
stude/study_groups/serializers.py
Normal 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__'
|
0
stude/study_groups/urls.py
Normal file
0
stude/study_groups/urls.py
Normal file
|
@ -1,3 +1,15 @@
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
from rest_framework import generics
|
||||||
|
from .serializers import StudyGroupSerializer
|
||||||
|
from .models import StudyGroup
|
||||||
# Create your views here.
|
# 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()
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
from .views import SubjectListView
|
from .views import SubjectListView
|
||||||
from .views import SubjectByYearSemesterView
|
from .views import SubjectByYearSemesterView, SubjectByYearView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', SubjectListView.as_view()),
|
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()),
|
SubjectByYearSemesterView.as_view()),
|
||||||
]
|
]
|
||||||
|
|
|
@ -10,7 +10,9 @@ class SubjectListView(generics.ListAPIView):
|
||||||
queryset = Subject.objects.all()
|
queryset = Subject.objects.all()
|
||||||
|
|
||||||
|
|
||||||
class SubjectByYearSemesterView(generics.ListAPIView):
|
class SubjectByYearView(generics.ListAPIView):
|
||||||
|
queryset = Subject.objects.all()
|
||||||
|
|
||||||
def get(self, request, year_slug):
|
def get(self, request, year_slug):
|
||||||
# Retrieve the subjects based on year level and semester slugs
|
# Retrieve the subjects based on year level and semester slugs
|
||||||
subjects = Subject.objects.filter(
|
subjects = Subject.objects.filter(
|
||||||
|
@ -20,3 +22,17 @@ class SubjectByYearSemesterView(generics.ListAPIView):
|
||||||
serializer = SubjectSerializer(subjects, many=True)
|
serializer = SubjectSerializer(subjects, many=True)
|
||||||
|
|
||||||
return Response(serializer.data)
|
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)
|
||||||
|
|
Loading…
Reference in a new issue