diff --git a/backend/server/adventures/serializers.py b/backend/server/adventures/serializers.py index aa98a35..4c55847 100644 --- a/backend/server/adventures/serializers.py +++ b/backend/server/adventures/serializers.py @@ -5,6 +5,7 @@ from rest_framework import serializers from main.utils import CustomModelSerializer from users.serializers import CustomUserDetailsSerializer from worldtravel.serializers import CountrySerializer, RegionSerializer, CitySerializer +from geopy.distance import geodesic class AdventureImageSerializer(CustomModelSerializer): @@ -194,15 +195,31 @@ class AdventureSerializer(CustomModelSerializer): return instance class TransportationSerializer(CustomModelSerializer): + distance = serializers.SerializerMethodField() class Meta: model = Transportation fields = [ 'id', 'user_id', 'type', 'name', 'description', 'rating', 'link', 'date', 'flight_number', 'from_location', 'to_location', - 'is_public', 'collection', 'created_at', 'updated_at', 'end_date', 'origin_latitude', 'origin_longitude', 'destination_latitude', 'destination_longitude', 'start_timezone', 'end_timezone' + 'is_public', 'collection', 'created_at', 'updated_at', 'end_date', + 'origin_latitude', 'origin_longitude', 'destination_latitude', 'destination_longitude', + 'start_timezone', 'end_timezone', 'distance' # ✅ Add distance here ] - read_only_fields = ['id', 'created_at', 'updated_at', 'user_id'] + read_only_fields = ['id', 'created_at', 'updated_at', 'user_id', 'distance'] + + def get_distance(self, obj): + if ( + obj.origin_latitude and obj.origin_longitude and + obj.destination_latitude and obj.destination_longitude + ): + try: + origin = (float(obj.origin_latitude), float(obj.origin_longitude)) + destination = (float(obj.destination_latitude), float(obj.destination_longitude)) + return round(geodesic(origin, destination).km, 2) + except ValueError: + return None + return None class LodgingSerializer(CustomModelSerializer): diff --git a/frontend/src/lib/components/TransportationCard.svelte b/frontend/src/lib/components/TransportationCard.svelte index 586c099..16dd996 100644 --- a/frontend/src/lib/components/TransportationCard.svelte +++ b/frontend/src/lib/components/TransportationCard.svelte @@ -23,6 +23,8 @@ export let user: User | null = null; export let collection: Collection | null = null; + const toMiles = (km: any) => (Number(km) * 0.621371).toFixed(1); + let isWarningModalOpen: boolean = false; function editTransportation() { @@ -109,14 +111,14 @@
-
- +
+
-

{transportation.name}

+

{transportation.name}

{$t(`transportation.modes.${transportation.type}`)} - {' '}{getTransportationIcon(transportation.type)} + {getTransportationIcon(transportation.type)}
{#if transportation.type === 'plane' && transportation.flight_number}
{transportation.flight_number}
@@ -127,50 +129,61 @@
- -
+ +
{#if transportation.from_location} -
- {$t('adventures.from')}: -

{transportation.from_location}

+
+ {$t('adventures.from')}: + {transportation.from_location}
{/if} - {#if transportation.date} -
- {$t('adventures.start')}: -

- {formatDateInTimezone(transportation.date, transportation.start_timezone)} - {#if transportation.start_timezone} - ({transportation.start_timezone}) - {/if} -

+ + {#if transportation.to_location} +
+ {$t('adventures.to')}: + {transportation.to_location} +
+ {/if} + + {#if transportation.distance && !isNaN(+transportation.distance)} +
+ {$t('adventures.distance')}: + + {(+transportation.distance).toFixed(1)} km / {toMiles(transportation.distance)} mi +
{/if}
- -
- {#if transportation.to_location} -
- {$t('adventures.to')}: -

{transportation.to_location}

+ +
+ {#if transportation.date} +
+ {$t('adventures.start')}: + + {formatDateInTimezone(transportation.date, transportation.start_timezone)} + {#if transportation.start_timezone} + ({transportation.start_timezone}) + {/if} +
{/if} + {#if transportation.end_date} -
- {$t('adventures.end')}: -

+

+ {$t('adventures.end')}: + {formatDateInTimezone(transportation.end_date, transportation.end_timezone)} {#if transportation.end_timezone} ({transportation.end_timezone}) {/if} -

+
{/if}
- {#if transportation.user_id == user?.uuid || (collection && user && collection.shared_with?.includes(user.uuid))} + {#if transportation.user_id === user?.uuid || (collection && user && collection.shared_with?.includes(user.uuid))}