from rest_framework import serializers from sitemanagement.models import FAQ, News from routes.models import Country 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): username = serializers.SerializerMethodField() userImg = serializers.SerializerMethodField() start_point = serializers.SerializerMethodField() country_from = serializers.SerializerMethodField() country_from_icon = serializers.SerializerMethodField() country_from_code = serializers.SerializerMethodField() end_point = serializers.SerializerMethodField() country_to = serializers.SerializerMethodField() country_to_icon = serializers.SerializerMethodField() country_to_code = serializers.SerializerMethodField() cargo_type = serializers.SerializerMethodField() user_request = serializers.SerializerMethodField() user_comment = serializers.CharField(source='comment') moving_type = serializers.SerializerMethodField() estimated_date = serializers.SerializerMethodField() day_out = serializers.DateTimeField(source='departure_DT') day_in = serializers.DateTimeField(source='arrival_DT') 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_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_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_from_code(self, obj): country = self.get_from_country_name(obj) return country[:3].upper() if country else None 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_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: print(f"Country not found: {country}") return None def get_country_to_code(self, obj): country = self.get_to_country_name(obj) return country[:3].upper() if country else None 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) def get_estimated_date(self, obj): return obj.arrival_DT