diff --git a/backend/api/main/serializers.py b/backend/api/main/serializers.py index 5b8c8cd..9d8b852 100644 --- a/backend/api/main/serializers.py +++ b/backend/api/main/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers from sitemanagement.models import FAQ, News -from routes.models import Country +from routes.models import Country, Route from api.account.client.serializers import RouteSerializer class FAQMainSerializer(serializers.ModelSerializer): @@ -38,23 +38,32 @@ class TelegramSerializer(serializers.Serializer): 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() - start_point = serializers.SerializerMethodField() - country_from = 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_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') + + 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 @@ -68,12 +77,6 @@ class HomePageRouteSerializer(RouteSerializer): 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: @@ -84,16 +87,6 @@ class HomePageRouteSerializer(RouteSerializer): 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: @@ -102,12 +95,21 @@ class HomePageRouteSerializer(RouteSerializer): 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_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) @@ -117,6 +119,3 @@ class HomePageRouteSerializer(RouteSerializer): def get_moving_type(self, obj): return self.get_formatted_transport(obj) - - def get_estimated_date(self, obj): - return obj.arrival_DT \ No newline at end of file