mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Also return distance in study group serializers
This commit is contained in:
parent
2e4a52eded
commit
0ecfb2223a
3 changed files with 64 additions and 37 deletions
|
@ -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
|
||||||
|
|
|
@ -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):
|
||||||
|
|
|
@ -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):
|
||||||
|
|
Loading…
Reference in a new issue