Files
tripwithbonus/backend/api/main/serializers.py

120 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from rest_framework import serializers
from sitemanagement.models import FAQ, News
from routes.models import Country, Route
from api.account.client.serializers import RouteSerializer
class FAQMainSerializer(serializers.ModelSerializer):
class Meta:
model = FAQ
fields = "__all__"
class NewsMainSerializer(serializers.ModelSerializer):
class Meta:
model = News
fields= "__all__"
class TelegramSerializer(serializers.Serializer):
"""Отправляем сообщение в телеграм канал компании с полями:
- Источник
- Имя пользователя
- Номер телефона
- Сообщение"""
SOURCE_CHOICES = [
("main", "Main"),
("admin", "Admin"),
("userAccount", "Account"),
("contactUs", "Contact Us"),
("support", "Support")
]
source = serializers.ChoiceField(choices=SOURCE_CHOICES)
name = serializers.CharField(max_length=255)
phone_number = serializers.CharField(max_length=20)
message = serializers.CharField(max_length=1000)
def create(self, validated_data):
return type('TelegramMessage', (), validated_data)
class HomePageRouteSerializer(RouteSerializer):
id = serializers.IntegerField()
username = serializers.SerializerMethodField()
owner_type = serializers.CharField()
from_city_name = serializers.SerializerMethodField('get_start_point')
from_country_name = serializers.SerializerMethodField('get_country_from')
to_city_name = serializers.SerializerMethodField('get_end_point')
to_country_name = serializers.SerializerMethodField('get_country_to')
formatted_cargo_type = serializers.SerializerMethodField('get_cargo_type')
formatted_transport = serializers.SerializerMethodField('get_moving_type')
type_transport = serializers.CharField()
userImg = serializers.SerializerMethodField()
comment = serializers.CharField()
formatted_departure = serializers.DateTimeField(source='departure_DT')
formatted_arrival = serializers.DateTimeField(source='arrival_DT')
country_from_icon = serializers.SerializerMethodField()
country_to_icon = serializers.SerializerMethodField()
class Meta:
model = Route
fields = (
'id', 'username', 'owner_type', 'from_city_name', 'from_country_name',
'to_city_name', 'to_country_name', 'formatted_cargo_type',
'formatted_transport', 'type_transport', 'userImg', 'comment',
'formatted_departure', 'formatted_arrival', 'country_from_icon',
'country_to_icon'
)
def get_username(self, obj):
return obj.owner.first_name if obj.owner else None
def get_userImg(self, obj):
try:
if obj.owner and hasattr(obj.owner, 'userprofile') and obj.owner.userprofile.image:
return obj.owner.userprofile.image.url
return None
except Exception as e:
print(f"Error in get_userImg: {e}")
return None
def get_country_from_icon(self, obj):
country = self.get_from_country_name(obj)
if not country:
return None
try:
country_obj = Country.objects.get(international_name=country)
return country_obj.flag_img_url
except Country.DoesNotExist:
return None
def get_country_to_icon(self, obj):
country = self.get_to_country_name(obj)
if not country:
return None
try:
country_obj = Country.objects.get(international_name=country)
return country_obj.flag_img_url
except Country.DoesNotExist:
return None
def get_start_point(self, obj):
return self.get_from_city_name(obj)
def get_country_from(self, obj):
return self.get_from_country_name(obj)
def get_end_point(self, obj):
return self.get_to_city_name(obj)
def get_country_to(self, obj):
return self.get_to_country_name(obj)
def get_cargo_type(self, obj):
return self.get_formatted_cargo_type(obj)
def get_user_request(self, obj):
return 'Нужен перевозчик' if obj.owner_type == 'customer' else 'Могу перевезти'
def get_moving_type(self, obj):
return self.get_formatted_transport(obj)