From 0ecfb2223a2210442b131a9d7aadb2ae584c4af7 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sat, 30 Sep 2023 17:06:35 +0800 Subject: [PATCH] Also return distance in study group serializers --- stude/schema.yml | 68 +++++++++++++++---------------- stude/study_groups/serializers.py | 29 ++++++++++++- stude/study_groups/views.py | 4 +- 3 files changed, 64 insertions(+), 37 deletions(-) diff --git a/stude/schema.yml b/stude/schema.yml index fa8f089..d8357cb 100644 --- a/stude/schema.yml +++ b/stude/schema.yml @@ -626,9 +626,41 @@ paths: items: $ref: '#/components/schemas/Semester' 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: - operationId: api_v1_student_status_filter_near_current_location_create + operationId: api_v1_student_status_near_current_location_create tags: - api requestBody: @@ -652,38 +684,6 @@ paths: schema: $ref: '#/components/schemas/StudentStatusLocation' 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/: get: operationId: api_v1_student_status_self_retrieve diff --git a/stude/study_groups/serializers.py b/stude/study_groups/serializers.py index 759b49e..eb8ea76 100644 --- a/stude/study_groups/serializers.py +++ b/stude/study_groups/serializers.py @@ -26,7 +26,34 @@ class StudyGroupSerializer(serializers.ModelSerializer): class Meta: model = StudyGroup 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): diff --git a/stude/study_groups/views.py b/stude/study_groups/views.py index 1fa16e2..80335da 100644 --- a/stude/study_groups/views.py +++ b/stude/study_groups/views.py @@ -2,7 +2,7 @@ from django.shortcuts import render from rest_framework import generics, mixins from rest_framework.exceptions import PermissionDenied from rest_framework.permissions import IsAuthenticated -from .serializers import StudyGroupSerializer, StudyGroupCreateSerializer +from .serializers import StudyGroupSerializer, StudyGroupCreateSerializer, StudyGroupDistanceSerializer from .models import StudyGroup from subjects.models import Subject, SubjectInstance from student_status.models import StudentStatus @@ -63,7 +63,7 @@ class StudyGroupListView(generics.ListAPIView): class StudyGroupListNearView(generics.ListAPIView): permission_classes = [IsAuthenticated] - serializer_class = StudyGroupSerializer + serializer_class = StudyGroupDistanceSerializer queryset = StudyGroup.objects.all() def get_queryset(self):