From 451c5ec00b201d146652cf44bd7a53884266ef6f Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sat, 2 Sep 2023 13:57:23 +0800 Subject: [PATCH] Made improvements to student status serializer --- stude/student_status/views.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/stude/student_status/views.py b/stude/student_status/views.py index a563f7f..a630a27 100644 --- a/stude/student_status/views.py +++ b/stude/student_status/views.py @@ -1,4 +1,5 @@ from django.shortcuts import get_object_or_404 +from django.contrib.gis.geos import Point from requests import Response from rest_framework import generics, viewsets, exceptions from rest_framework.permissions import IsAuthenticated @@ -36,13 +37,14 @@ class StudentStatusListByStudentStatusLocation(generics.ListAPIView): def get_queryset(self): user = self.request.user user_status = StudentStatus.objects.filter(user=user).first() - print('User Location: ', user_status.location) - if user_status.location is None: - raise exceptions.ValidationError("User location is not set") + user_location = fromstr( user_status.location, srid=4326) - return StudentStatus.objects.filter(user != user).filter(subject__in=user.subjects.all()).annotate(distance=Distance('location', user_location)).filter(distance__lte=50) + if user_status.active is False: + raise exceptions.ValidationError("Student Status is not active") + + return StudentStatus.objects.filter(active=True and user != user).filter(subject__in=user.subjects.all()).annotate(distance=Distance('location', user_location)).filter(distance__lte=50) class StudentStatusListByCurrentLocation(viewsets.ViewSet): @@ -53,11 +55,13 @@ class StudentStatusListByCurrentLocation(viewsets.ViewSet): def create(self, request): user = self.request.user location_str = request.data.get('location') + + # If location is not specified in request, throw error if not location_str: raise exceptions.ValidationError("Location is required") user_location = fromstr(location_str, srid=4326) - queryset = StudentStatus.objects.filter(user != user).filter(subject__in=user.subjects.all()).annotate( + queryset = StudentStatus.objects.filter(active=True and user != user).filter(subject__in=user.subjects.all()).annotate( distance=Distance('location', user_location)).filter(distance__lte=50) serializer = StudentStatusLocationSerializer(queryset, many=True) return Response(serializer.data)