Improved student status serializer

This commit is contained in:
Keannu Bernasol 2023-08-07 15:16:57 +08:00
parent e79671b9b9
commit 3df8aed143
2 changed files with 11 additions and 0 deletions

View file

@ -26,9 +26,14 @@ class StudentStatusSerializer(serializers.ModelSerializer):
return student_status
def update(self, instance, validated_data):
active = validated_data.get('active', None)
subject = validated_data.get('subject', None)
# Do not update if instance if active is not specified
if active is None:
return instance
# If status is set as inactive or if no subject is specified in the request, clear the student status
if active is not None and active is False or not subject:
validated_data['location'] = Point(0, 0)

View file

@ -1,3 +1,4 @@
from django.shortcuts import get_object_or_404
from rest_framework import generics, viewsets
from rest_framework.permissions import IsAuthenticated
from .models import StudentStatus
@ -13,6 +14,11 @@ class StudentStatusAPIView(generics.RetrieveUpdateAPIView):
queryset = StudentStatus.objects.filter(user=user)
return queryset
def get_object(self):
queryset = self.get_queryset()
obj = get_object_or_404(queryset)
return obj
class ActiveStudentStatusListAPIView(generics.ListAPIView):
serializer_class = StudentStatusSerializer