mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Final changes to subject and subject instance serializer for easier queries
This commit is contained in:
parent
6fe1042826
commit
63d16eae17
3 changed files with 19 additions and 14 deletions
|
@ -22,11 +22,14 @@ from drf_extra_fields.fields import Base64ImageField
|
||||||
|
|
||||||
|
|
||||||
class SubjectSlugRelatedField(serializers.SlugRelatedField):
|
class SubjectSlugRelatedField(serializers.SlugRelatedField):
|
||||||
|
def to_representation(self, value):
|
||||||
|
return value.subject.name
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
user_course = self.context['request'].user.course
|
user_course = self.context['request'].user.course
|
||||||
try:
|
try:
|
||||||
subject = SubjectInstance.objects.get(
|
subject = SubjectInstance.objects.get(
|
||||||
name=data, course=user_course)
|
subject__name=data, course=user_course)
|
||||||
return subject
|
return subject
|
||||||
except SubjectInstance.DoesNotExist:
|
except SubjectInstance.DoesNotExist:
|
||||||
self.fail('does_not_exist', slug_name=self.slug_field,
|
self.fail('does_not_exist', slug_name=self.slug_field,
|
||||||
|
@ -47,7 +50,7 @@ class CustomUserSerializer(BaseUserSerializer):
|
||||||
many=False, slug_field='name', queryset=Semester.objects.all(), required=False, allow_null=True)
|
many=False, slug_field='name', queryset=Semester.objects.all(), required=False, allow_null=True)
|
||||||
# Use custom slug field for filtering
|
# Use custom slug field for filtering
|
||||||
subjects = SubjectSlugRelatedField(
|
subjects = SubjectSlugRelatedField(
|
||||||
many=True, slug_field='name', queryset=SubjectInstance.objects.all(), required=False)
|
many=True, slug_field='subject', queryset=SubjectInstance.objects.all(), required=False)
|
||||||
avatar = Base64ImageField()
|
avatar = Base64ImageField()
|
||||||
|
|
||||||
class Meta(BaseUserSerializer.Meta):
|
class Meta(BaseUserSerializer.Meta):
|
||||||
|
|
|
@ -5,7 +5,8 @@ from year_levels.models import Year_Level
|
||||||
from semesters.models import Semester
|
from semesters.models import Semester
|
||||||
|
|
||||||
|
|
||||||
class SubjectSerializer(serializers.ModelSerializer):
|
class SubjectInstanceSerializer(serializers.ModelSerializer):
|
||||||
|
name = serializers.StringRelatedField(source='subject', read_only=True)
|
||||||
year_level = serializers.SlugRelatedField(
|
year_level = serializers.SlugRelatedField(
|
||||||
queryset=Subject.objects.all(), slug_field='name', allow_null=False)
|
queryset=Subject.objects.all(), slug_field='name', allow_null=False)
|
||||||
year_level = serializers.SlugRelatedField(
|
year_level = serializers.SlugRelatedField(
|
||||||
|
@ -17,4 +18,5 @@ class SubjectSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = SubjectInstance
|
model = SubjectInstance
|
||||||
fields = ('id', 'subject', 'code', 'course', 'year_level', 'semester')
|
fields = ('id', 'name', 'code',
|
||||||
|
'course', 'year_level', 'semester')
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
from rest_framework import generics, viewsets
|
from rest_framework import generics, viewsets
|
||||||
from .models import Subject, SubjectInstance
|
from .models import Subject, SubjectInstance
|
||||||
from .serializers import SubjectSerializer
|
from .serializers import SubjectInstanceSerializer
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
|
|
||||||
class SubjectListAllView(generics.ListAPIView):
|
class SubjectListAllView(generics.ListAPIView):
|
||||||
serializer_class = SubjectSerializer
|
serializer_class = SubjectInstanceSerializer
|
||||||
queryset = SubjectInstance.objects.all()
|
queryset = SubjectInstance.objects.all()
|
||||||
|
|
||||||
|
|
||||||
class SubjectListView(generics.ListAPIView):
|
class SubjectListView(generics.ListAPIView):
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
queryset = SubjectInstance.objects.all()
|
queryset = SubjectInstance.objects.all()
|
||||||
serializer_class = SubjectSerializer
|
serializer_class = SubjectInstanceSerializer
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
user = self.request.user
|
user = self.request.user
|
||||||
|
@ -32,13 +32,13 @@ class SubjectListView(generics.ListAPIView):
|
||||||
course__name=user.course, year_level__name=user_yearlevel, semester__name=user_semester)
|
course__name=user.course, year_level__name=user_yearlevel, semester__name=user_semester)
|
||||||
|
|
||||||
# Serialize the subjects
|
# Serialize the subjects
|
||||||
serializer = SubjectSerializer(subjects, many=True)
|
serializer = SubjectInstanceSerializer(subjects, many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
class SubjectByCourseView(generics.ListAPIView):
|
class SubjectByCourseView(generics.ListAPIView):
|
||||||
queryset = SubjectInstance.objects.all()
|
queryset = SubjectInstance.objects.all()
|
||||||
serializer_class = SubjectSerializer
|
serializer_class = SubjectInstanceSerializer
|
||||||
|
|
||||||
def get(self, request, course_slug):
|
def get(self, request, course_slug):
|
||||||
# Retrieve the subjects based on course slug
|
# Retrieve the subjects based on course slug
|
||||||
|
@ -46,18 +46,18 @@ class SubjectByCourseView(generics.ListAPIView):
|
||||||
course__shortname=course_slug)
|
course__shortname=course_slug)
|
||||||
|
|
||||||
# Serialize the subjects
|
# Serialize the subjects
|
||||||
serializer = SubjectSerializer(subjects, many=True)
|
serializer = SubjectInstanceSerializer(subjects, many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
|
||||||
class SubjectByCourseYearSemesterView(generics.ListAPIView):
|
class SubjectByCourseYearSemesterView(generics.ListAPIView):
|
||||||
queryset = Subject.objects.all()
|
queryset = SubjectInstance.objects.all()
|
||||||
serializer_class = SubjectSerializer
|
serializer_class = SubjectInstanceSerializer
|
||||||
|
|
||||||
def get(self, request, course_slug, year_slug, semester_slug):
|
def get(self, request, course_slug, year_slug, semester_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 = SubjectInstance.objects.filter(
|
||||||
course__shortname=course_slug, year_level__shortname=year_slug, semester__shortname=semester_slug)
|
course__shortname=course_slug, year_level__shortname=year_slug, semester__shortname=semester_slug)
|
||||||
# Serialize the subjects
|
# Serialize the subjects
|
||||||
serializer = SubjectSerializer(subjects, many=True)
|
serializer = SubjectInstanceSerializer(subjects, many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
Loading…
Reference in a new issue