mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Improved study_groups geofencing in serializer
This commit is contained in:
parent
a67ea5cd8a
commit
447b034d38
1 changed files with 19 additions and 0 deletions
|
@ -3,6 +3,7 @@ from .models import StudyGroup, StudyGroupMembership
|
|||
from accounts.models import CustomUser
|
||||
from subjects.models import Subject
|
||||
from drf_extra_fields.geo_fields import PointField
|
||||
from landmarks.models import Landmark
|
||||
|
||||
|
||||
class StudyGroupSerializer(serializers.ModelSerializer):
|
||||
|
@ -11,10 +12,28 @@ class StudyGroupSerializer(serializers.ModelSerializer):
|
|||
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)
|
||||
|
||||
def create(self, validated_data):
|
||||
user = self.context['request'].user
|
||||
study_group = StudyGroup.objects.create(
|
||||
users=[user], defaults=validated_data)
|
||||
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)
|
||||
|
||||
class Meta:
|
||||
model = StudyGroup
|
||||
fields = '__all__'
|
||||
read_only_fields = ['landmark']
|
||||
|
||||
|
||||
class StudyGroupMembershipSerializer(serializers.ModelSerializer):
|
||||
|
|
Loading…
Reference in a new issue