backend/passfinder/events/api/serializers.py

156 lines
4.4 KiB
Python
Raw Normal View History

2023-05-27 01:03:34 +03:00
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema_field
2023-05-20 16:47:08 +03:00
from rest_framework import serializers
2023-05-23 18:14:06 +03:00
from rest_framework.generics import get_object_or_404
2023-05-20 16:47:08 +03:00
2023-05-26 01:15:02 +03:00
from passfinder.events.models import (
Hotel,
HotelPhone,
City,
Event,
BasePoint,
Region,
Restaurant,
2023-05-27 01:03:34 +03:00
UserRoute,
UserRouteDate,
2023-05-26 01:15:02 +03:00
)
2023-05-20 16:47:08 +03:00
class HotelPhoneSerializer(serializers.ModelSerializer):
class Meta:
model = HotelPhone
2023-05-26 01:15:02 +03:00
exclude = ("hotel",)
2023-05-20 16:47:08 +03:00
class HotelSerializer(serializers.ModelSerializer):
phones = HotelPhoneSerializer(many=True)
source = serializers.CharField(source="parser_source")
class Meta:
model = Hotel
exclude = ("oid", "parser_source")
class MuseumSerializer(serializers.ModelSerializer):
phones = HotelPhoneSerializer(many=True)
class Meta:
model = Hotel
2023-05-26 01:15:02 +03:00
exclude = ("oid",)
2023-05-21 13:37:21 +03:00
class EventSerializer(serializers.ModelSerializer):
class Meta:
model = Event
2023-05-26 01:45:14 +03:00
fields = ("type", "title", "description", "city", "oid")
2023-05-21 20:46:07 +03:00
class PointSerializer(serializers.ModelSerializer):
class Meta:
model = BasePoint
fields = ["oid", "title", "description", "location", "icon"]
class RouteSerializer(serializers.Serializer):
name = serializers.CharField()
date = serializers.DateField(allow_null=True)
description = serializers.CharField()
points = serializers.ListSerializer(child=PointSerializer())
2023-05-23 00:33:04 +03:00
2023-05-23 18:14:06 +03:00
class RouteInputSerializer(serializers.Serializer):
date_from = serializers.DateField(required=False, allow_null=True)
date_to = serializers.DateField(required=False, allow_null=True)
2023-05-25 17:12:39 +03:00
city = serializers.CharField(
min_length=24, max_length=24, required=False, allow_blank=True, allow_null=True
2023-05-23 18:14:06 +03:00
)
class CitySerializer(serializers.ModelSerializer):
class Meta:
model = City
fields = ["oid", "title"]
2023-05-23 00:33:04 +03:00
class RegionSerializer(serializers.ModelSerializer):
cities = CitySerializer(many=True)
2023-05-23 00:33:04 +03:00
class Meta:
model = Region
fields = ["oid", "title", "description_short", "cities"]
class InputRoutePointSerializer(serializers.Serializer):
type = serializers.ChoiceField(choices=["point", "transition"])
2023-05-27 01:03:34 +03:00
duration = serializers.IntegerField(min_value=0, required=True)
# point
point = serializers.CharField(
min_length=24, max_length=24, required=False, allow_blank=True, allow_null=True
)
2023-05-27 01:03:34 +03:00
point_type = serializers.CharField(
required=False, allow_blank=True, allow_null=True
)
# transition
distance = serializers.FloatField(min_value=0, required=False, allow_null=True)
def validate(self, data):
if data["type"] == "point":
if "point" not in data or not data["point"]:
raise serializers.ValidationError("Point id is required")
get_object_or_404(BasePoint, oid=data["point"])
2023-05-27 01:03:34 +03:00
if "distance" not in data or not data["point_type"]:
raise serializers.ValidationError("Point type is required")
else:
if "distance" not in data or not data["distance"]:
raise serializers.ValidationError("Distance is required")
return data
2023-05-27 01:03:34 +03:00
class InputRouteDateSerializer(serializers.Serializer):
date = serializers.DateField()
points = serializers.ListSerializer(child=InputRoutePointSerializer())
2023-05-25 17:12:39 +03:00
2023-05-27 01:03:34 +03:00
class InputRouteSerializer(serializers.Serializer):
dates = serializers.ListSerializer(child=InputRouteDateSerializer())
class ListUserRouteSerializer(serializers.ModelSerializer):
class Meta:
model = UserRoute
fields = ["id", "created"]
class UserRouteDateSerializer(serializers.ModelSerializer):
points = serializers.SerializerMethodField(method_name="get_points")
@extend_schema_field(InputRoutePointSerializer)
def get_points(self, obj):
return [x.get_json() for x in obj.points.all()]
class Meta:
model = UserRouteDate
fields = ["date", "points"]
class UserRouteSerializer(serializers.ModelSerializer):
class Meta:
model = UserRoute
fields = ["created", "dates"]
class RestaurantSerializer(serializers.ModelSerializer):
2023-05-25 17:12:39 +03:00
class Meta:
model = Restaurant
2023-05-26 01:15:02 +03:00
exclude = ("phones",)
2023-05-25 17:12:39 +03:00
class ObjectRouteSerializer(serializers.Serializer):
lat = serializers.FloatField()
lon = serializers.FloatField()
title = serializers.CharField()
description = serializers.CharField()