dynamic routes for main page

This commit is contained in:
2025-05-23 11:48:45 +03:00
parent efccb591ff
commit 45e5e78df2
10 changed files with 234 additions and 141 deletions

View File

@@ -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)
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

View File

@@ -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