from-to search route

This commit is contained in:
2025-05-26 17:36:30 +03:00
parent c761c60818
commit 46945e32a8
8 changed files with 207 additions and 25 deletions

View File

@@ -1,8 +1,81 @@
from api.main.serializers import HomePageRouteSerializer
from routes.models import Route
from rest_framework import serializers
from routes.models import Route, Country
from api.main.serializers import RouteSerializer
class SearchRouteSerializer(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 SearchRouteSerializer(HomePageRouteSerializer):
class Meta(HomePageRouteSerializer.Meta):
class Meta:
model = Route
fields = HomePageRouteSerializer.Meta.fields
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_moving_type(self, obj):
return self.get_formatted_transport(obj)

View File

@@ -12,6 +12,9 @@ class SearchRouteListView(generics.ListAPIView):
def get_queryset(self):
owner_type = self.kwargs.get('owner_type')
from_city = self.request.query_params.get('from')
to_city = self.request.query_params.get('to')
valid_types = [choice[0] for choice in owner_type_choices]
current_time = timezone.now()
@@ -23,6 +26,12 @@ class SearchRouteListView(generics.ListAPIView):
owner_type=owner_type,
status="actual")
# фильтруем по городам если они указаны
if from_city:
queryset = queryset.filter(from_city__name__iexact=from_city)
if to_city:
queryset = queryset.filter(to_city__name__iexact=to_city)
# фильтруем по времени в зависимости от типа
if owner_type == 'mover':
queryset = queryset.filter(departure_DT__gt=current_time)