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-10-01 01:24:58 +08:00
|
|
|
from drf_extra_fields.fields import Base64ImageField
|
|
|
|
from djoser.serializers import UserSerializer as BaseUserSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class CustomUserAvatarSerializer(BaseUserSerializer):
|
|
|
|
avatar = Base64ImageField()
|
|
|
|
|
|
|
|
class Meta(BaseUserSerializer.Meta):
|
|
|
|
model = CustomUser
|
|
|
|
fields = ['avatar', 'username']
|
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-10-01 01:24:58 +08:00
|
|
|
class StudyGroupDetailSerializer(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)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StudyGroup
|
|
|
|
fields = '__all__'
|
|
|
|
read_only_fields = ['landmark', 'radius', 'students', 'distance']
|
|
|
|
|
|
|
|
|
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__'
|
2023-09-30 17:06:35 +08:00
|
|
|
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):
|
2023-12-19 18:29:48 +08:00
|
|
|
return 30
|
2023-09-30 17:06:35 +08:00
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
representation = super().to_representation(instance)
|
|
|
|
representation['distance'] = self.get_distance(instance)
|
|
|
|
return representation
|
2023-09-26 20:28:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2023-11-24 00:13:41 +08:00
|
|
|
study_group.landmark = landmark
|
|
|
|
study_group.save()
|
2023-09-26 20:28:59 +08:00
|
|
|
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
|
2023-09-28 21:03:34 +08:00
|
|
|
if ('location' in validated_data):
|
|
|
|
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
|
|
|
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']
|