Also return distance in study group serializers

This commit is contained in:
Keannu Bernasol 2023-09-30 17:06:35 +08:00
parent 2e4a52eded
commit 0ecfb2223a
3 changed files with 64 additions and 37 deletions

View file

@ -626,9 +626,41 @@ paths:
items: items:
$ref: '#/components/schemas/Semester' $ref: '#/components/schemas/Semester'
description: '' description: ''
/api/v1/student_status/filter/near_current_location/: /api/v1/student_status/list/:
get:
operationId: api_v1_student_status_list_list
tags:
- api
security:
- jwtAuth: []
responses:
'200':
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/StudentStatus'
description: ''
/api/v1/student_status/near/:
get:
operationId: api_v1_student_status_near_list
tags:
- api
security:
- jwtAuth: []
responses:
'200':
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/StudentStatusLocation'
description: ''
/api/v1/student_status/near_current_location//:
post: post:
operationId: api_v1_student_status_filter_near_current_location_create operationId: api_v1_student_status_near_current_location_create
tags: tags:
- api - api
requestBody: requestBody:
@ -652,38 +684,6 @@ paths:
schema: schema:
$ref: '#/components/schemas/StudentStatusLocation' $ref: '#/components/schemas/StudentStatusLocation'
description: '' description: ''
/api/v1/student_status/filter/near_student_status:
get:
operationId: api_v1_student_status_filter_near_student_status_list
tags:
- api
security:
- jwtAuth: []
responses:
'200':
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/StudentStatusLocation'
description: ''
/api/v1/student_status/list/:
get:
operationId: api_v1_student_status_list_list
tags:
- api
security:
- jwtAuth: []
responses:
'200':
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/StudentStatus'
description: ''
/api/v1/student_status/self/: /api/v1/student_status/self/:
get: get:
operationId: api_v1_student_status_self_retrieve operationId: api_v1_student_status_self_retrieve

View file

@ -26,7 +26,34 @@ class StudyGroupSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = StudyGroup model = StudyGroup
fields = '__all__' fields = '__all__'
read_only_fields = ['landmark', 'radius', 'students'] read_only_fields = ['landmark', 'radius', 'students', 'distance']
class StudyGroupDistanceSerializer(serializers.ModelSerializer):
name = serializers.CharField()
students = serializers.StringRelatedField(many=True)
subject = serializers.SlugRelatedField(
many=False, slug_field='name', queryset=Subject.objects.all(), required=True, allow_null=False)
location = PointField()
landmark = serializers.SlugRelatedField(
queryset=Landmark.objects.all(), many=False, slug_field='name', required=False, allow_null=True)
radius = serializers.FloatField()
distance = serializers.SerializerMethodField(default=0)
class Meta:
model = StudyGroup
fields = '__all__'
read_only_fields = ['landmark', 'radius', 'students', 'distance']
def get_distance(self, obj):
if hasattr(obj, 'distance'):
return obj.distance.km
return 0
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['distance'] = self.get_distance(instance)
return representation
class FullNameSlugRelatedField(serializers.SlugRelatedField): class FullNameSlugRelatedField(serializers.SlugRelatedField):

View file

@ -2,7 +2,7 @@ from django.shortcuts import render
from rest_framework import generics, mixins from rest_framework import generics, mixins
from rest_framework.exceptions import PermissionDenied from rest_framework.exceptions import PermissionDenied
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from .serializers import StudyGroupSerializer, StudyGroupCreateSerializer from .serializers import StudyGroupSerializer, StudyGroupCreateSerializer, StudyGroupDistanceSerializer
from .models import StudyGroup from .models import StudyGroup
from subjects.models import Subject, SubjectInstance from subjects.models import Subject, SubjectInstance
from student_status.models import StudentStatus from student_status.models import StudentStatus
@ -63,7 +63,7 @@ class StudyGroupListView(generics.ListAPIView):
class StudyGroupListNearView(generics.ListAPIView): class StudyGroupListNearView(generics.ListAPIView):
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
serializer_class = StudyGroupSerializer serializer_class = StudyGroupDistanceSerializer
queryset = StudyGroup.objects.all() queryset = StudyGroup.objects.all()
def get_queryset(self): def get_queryset(self):