diff --git a/backend/api/main/serializers.py b/backend/api/main/serializers.py index 2e4db1a..5b8c8cd 100644 --- a/backend/api/main/serializers.py +++ b/backend/api/main/serializers.py @@ -1,9 +1,7 @@ from rest_framework import serializers -from routes.models import Route from sitemanagement.models import FAQ, News -from django.conf import settings -import pytz -from routes.constants.routeChoices import cargo_type_choices, type_transport_choices +from routes.models import Country +from api.account.client.serializers import RouteSerializer class FAQMainSerializer(serializers.ModelSerializer): class Meta: @@ -37,4 +35,88 @@ class TelegramSerializer(serializers.Serializer): def create(self, validated_data): return type('TelegramMessage', (), validated_data) - \ No newline at end of file + + +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 \ No newline at end of file diff --git a/backend/api/main/views.py b/backend/api/main/views.py index 7197550..f9d5d09 100644 --- a/backend/api/main/views.py +++ b/backend/api/main/views.py @@ -4,12 +4,10 @@ from rest_framework import status from rest_framework.views import APIView from rest_framework.response import Response from api.utils.decorators import handle_exceptions -from django.db.models import Q from routes.models import Route from routes.constants.routeChoices import owner_type_choices -from api.main.serializers import FAQMainSerializer, NewsMainSerializer, TelegramSerializer -from api.account.client.serializers import RouteSerializer +from api.main.serializers import FAQMainSerializer, NewsMainSerializer, TelegramSerializer, HomePageRouteSerializer from sitemanagement.models import FAQ, News class FAQView(APIView): @@ -39,16 +37,20 @@ class NewsView(APIView): class LatestRoutesView(APIView): @handle_exceptions def get(self, request): - """Получаем последние 5 маршрутов для каждого типа owner_type""" + """Получаем последние маршруты""" - latest_routes = {} + routes = [] owner_types = dict(owner_type_choices).keys() for owner_type in owner_types: - routes = Route.objects.filter(owner_type=owner_type).order_by('-id')[:5] - latest_routes[owner_type] = RouteSerializer(routes, many=True).data + routes.extend( + HomePageRouteSerializer( + Route.objects.filter(owner_type=owner_type).order_by('-id')[:5], + many=True + ).data + ) - return Response(latest_routes, status=status.HTTP_200_OK) + return Response(routes, status=status.HTTP_200_OK) class TelegramMessageView(APIView): @handle_exceptions diff --git a/frontend/app/(urls)/search/components/SearchCard.tsx b/frontend/app/(urls)/search/components/SearchCard.tsx index ac1306b..5410cd6 100644 --- a/frontend/app/(urls)/search/components/SearchCard.tsx +++ b/frontend/app/(urls)/search/components/SearchCard.tsx @@ -1,42 +1,43 @@ import React from 'react' import Image from 'next/image' import Button from '@/components/ui/Button' -import { SearchCardProps } from '@/app/types/index' +import { SearchCardProps } from '@/app/types' +import noPhoto from '../../../../public/images/noPhoto.png' const SearchCard = ({ id, username, + owner_type, + from_city_name, + from_country_name, + to_city_name, + to_country_name, + formatted_cargo_type, + formatted_transport, + type_transport, userImg, - start_point, - country_from, + comment, + formatted_departure, + formatted_arrival, country_from_icon, - country_from_code, - end_point, - country_to, country_to_icon, - country_to_code, - cargo_type, - user_request, - user_comment, - moving_type, - estimated_date, - day_out, - day_in, }: SearchCardProps) => { const getUserRequestStyles = () => { - if (user_request === 'Нужен перевозчик') { + if (owner_type === 'customer') { return 'text-[#065bff]' } return 'text-[#45c226]' } const setMovingTypeIcon = () => { - if (moving_type === 'Авиатранспорт') { + if (type_transport === 'air') { return '/images/airplane.png' } return '/images/car.png' } + const userRequest = owner_type === 'customer' ? 'Нужен перевозчик' : 'Могу перевезти' + return ( <> {/* десктоп */} @@ -46,21 +47,22 @@ const SearchCard = ({