2023-06-27 21:35:19 +08:00
|
|
|
from rest_framework import serializers
|
2023-09-25 21:09:31 +08:00
|
|
|
from .models import StudyGroup
|
2023-06-27 21:35:19 +08:00
|
|
|
from accounts.models import CustomUser
|
2023-07-06 21:47:28 +08:00
|
|
|
from subjects.models import Subject
|
2023-07-14 23:55:54 +08:00
|
|
|
from drf_extra_fields.geo_fields import PointField
|
2023-07-14 23:56:24 +08:00
|
|
|
from landmarks.models import Landmark
|
2023-06-27 21:35:19 +08:00
|
|
|
|
|
|
|
|
2023-09-24 19:06:50 +08:00
|
|
|
class CustomUserKeyRelatedField(serializers.PrimaryKeyRelatedField):
|
|
|
|
|
|
|
|
def to_representation(self, value):
|
|
|
|
# returns the string representation of the custom user (aka the name)
|
|
|
|
return str(value)
|
|
|
|
|
|
|
|
|
2023-06-27 21:35:19 +08:00
|
|
|
class StudyGroupSerializer(serializers.ModelSerializer):
|
2023-09-24 21:03:32 +08:00
|
|
|
name = serializers.CharField()
|
2023-09-25 21:09:31 +08:00
|
|
|
students = serializers.StringRelatedField(many=True)
|
2023-07-06 21:47:28 +08:00
|
|
|
subject = serializers.SlugRelatedField(
|
|
|
|
many=False, slug_field='name', queryset=Subject.objects.all(), required=True, allow_null=False)
|
2023-07-14 23:55:54 +08:00
|
|
|
location = PointField()
|
2023-07-14 23:56:24 +08:00
|
|
|
landmark = serializers.SlugRelatedField(
|
|
|
|
queryset=Landmark.objects.all(), many=False, slug_field='name', required=False, allow_null=True)
|
2023-09-24 21:03:32 +08:00
|
|
|
radius = serializers.FloatField()
|
2023-07-14 23:56:24 +08:00
|
|
|
|
2023-09-26 20:28:59 +08:00
|
|
|
class Meta:
|
|
|
|
model = StudyGroup
|
|
|
|
fields = '__all__'
|
|
|
|
read_only_fields = ['landmark', 'radius', 'students']
|
|
|
|
|
|
|
|
|
|
|
|
class FullNameSlugRelatedField(serializers.SlugRelatedField):
|
|
|
|
def to_representation(self, instance):
|
|
|
|
return instance.full_name
|
|
|
|
|
|
|
|
|
|
|
|
class StudyGroupCreateSerializer(serializers.ModelSerializer):
|
|
|
|
name = serializers.CharField()
|
|
|
|
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)
|
|
|
|
|
2023-07-14 23:56:24 +08:00
|
|
|
def create(self, validated_data):
|
|
|
|
user = self.context['request'].user
|
|
|
|
study_group = StudyGroup.objects.create(
|
2023-09-26 20:28:59 +08:00
|
|
|
name=validated_data['name'], location=validated_data['location'], subject=validated_data['subject'])
|
|
|
|
for landmark in Landmark.objects.all():
|
|
|
|
if landmark.location.contains(validated_data['location']):
|
|
|
|
validated_data['landmark'] = landmark
|
|
|
|
break
|
2023-07-14 23:56:24 +08:00
|
|
|
validated_data['location'].read_only = True
|
|
|
|
return study_group
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
# Check each landmark to see if our location is within it
|
|
|
|
for landmark in Landmark.objects.all():
|
|
|
|
if landmark.location.contains(validated_data['location']):
|
|
|
|
validated_data['landmark'] = landmark
|
|
|
|
break
|
|
|
|
return super().update(instance, validated_data)
|
2023-06-27 21:35:19 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StudyGroup
|
|
|
|
fields = '__all__'
|
2023-09-26 20:28:59 +08:00
|
|
|
read_only_fields = ['landmark']
|