2023-08-15 00:45:00 +08:00
|
|
|
from rest_framework import serializers, exceptions
|
2023-06-26 23:50:55 +08:00
|
|
|
from .models import StudentStatus
|
2023-07-09 19:00:47 +08:00
|
|
|
from subjects.models import Subject
|
|
|
|
from django.contrib.gis.geos import Point
|
2023-07-14 23:55:54 +08:00
|
|
|
from drf_extra_fields.geo_fields import PointField
|
|
|
|
from landmarks.models import Landmark
|
2023-06-26 22:06:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
class StudentStatusSerializer(serializers.ModelSerializer):
|
2023-07-09 19:00:47 +08:00
|
|
|
subject = serializers.SlugRelatedField(
|
|
|
|
queryset=Subject.objects.all(), slug_field='name', required=True)
|
2023-07-01 16:09:20 +08:00
|
|
|
user = serializers.CharField(source='user.full_name', read_only=True)
|
2023-08-06 14:35:39 +08:00
|
|
|
location = PointField(required=True)
|
2023-07-14 23:55:54 +08:00
|
|
|
landmark = serializers.SlugRelatedField(
|
|
|
|
queryset=Landmark.objects.all(), many=False, slug_field='name', required=False, allow_null=True)
|
2023-08-07 15:36:37 +08:00
|
|
|
active = serializers.BooleanField(required=True)
|
2023-06-27 13:40:31 +08:00
|
|
|
|
2023-06-26 22:06:05 +08:00
|
|
|
class Meta:
|
|
|
|
model = StudentStatus
|
2023-06-26 23:50:55 +08:00
|
|
|
fields = '__all__'
|
2023-07-14 23:55:54 +08:00
|
|
|
read_only_fields = ['user', 'landmark']
|
2023-06-26 23:50:55 +08:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
user = self.context['request'].user
|
2023-06-27 13:40:31 +08:00
|
|
|
student_status = StudentStatus.objects.create(
|
2023-06-26 23:50:55 +08:00
|
|
|
user=user, defaults=validated_data)
|
|
|
|
return student_status
|
2023-07-01 16:09:20 +08:00
|
|
|
|
2023-06-27 13:40:31 +08:00
|
|
|
def update(self, instance, validated_data):
|
2023-08-07 15:16:57 +08:00
|
|
|
|
2023-06-27 13:40:31 +08:00
|
|
|
active = validated_data.get('active', None)
|
2023-08-07 15:37:47 +08:00
|
|
|
# subject = validated_data.get('subject', None)
|
2023-06-27 13:40:31 +08:00
|
|
|
|
2023-08-07 15:37:47 +08:00
|
|
|
# If status is set as false in the request, clear the student status
|
|
|
|
if active is False:
|
2023-07-09 19:00:47 +08:00
|
|
|
validated_data['location'] = Point(0, 0)
|
2023-06-27 13:40:31 +08:00
|
|
|
validated_data['subject'] = None
|
2023-07-14 23:55:54 +08:00
|
|
|
validated_data['landmark'] = None
|
|
|
|
else:
|
2023-08-15 00:45:00 +08:00
|
|
|
if 'subject' not in validated_data:
|
|
|
|
raise serializers.ValidationError(
|
|
|
|
{'subject': 'This field may not be empty if active is true'})
|
|
|
|
# To-do: Add geofencing to ensure locations are always within USTP
|
2023-07-14 23:55:54 +08:00
|
|
|
# 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
|
2023-06-27 13:40:31 +08:00
|
|
|
|
2023-07-01 16:09:20 +08:00
|
|
|
return super().update(instance, validated_data)
|