From 3df8aed1432f1898b0dd676b91e1f8ecb39819e2 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Mon, 7 Aug 2023 15:16:57 +0800 Subject: [PATCH] Improved student status serializer --- stude/student_status/serializers.py | 5 +++++ stude/student_status/views.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/stude/student_status/serializers.py b/stude/student_status/serializers.py index b4111f2..579b1f8 100644 --- a/stude/student_status/serializers.py +++ b/stude/student_status/serializers.py @@ -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) diff --git a/stude/student_status/views.py b/stude/student_status/views.py index 46fd771..c300f9b 100644 --- a/stude/student_status/views.py +++ b/stude/student_status/views.py @@ -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