1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-21 13:59:36 +02:00

transportation

This commit is contained in:
Sean Morley 2024-07-27 19:22:01 -04:00
parent ae9a7f5479
commit 7c9afd8931
5 changed files with 206 additions and 67 deletions

View file

@ -23,14 +23,6 @@ class AdventureSerializer(serializers.ModelSerializer):
return [activity.lower() for activity in value] return [activity.lower() for activity in value]
return value return value
class CollectionSerializer(serializers.ModelSerializer):
adventures = AdventureSerializer(many=True, read_only=True, source='adventure_set')
class Meta:
model = Collection
# fields are all plus the adventures field
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures', 'created_at', 'start_date', 'end_date']
class TransportationSerializer(serializers.ModelSerializer): class TransportationSerializer(serializers.ModelSerializer):
class Meta: class Meta:
@ -66,3 +58,14 @@ class TransportationSerializer(serializers.ModelSerializer):
return super().create(validated_data) return super().create(validated_data)
class CollectionSerializer(serializers.ModelSerializer):
adventures = AdventureSerializer(many=True, read_only=True, source='adventure_set')
transportations = TransportationSerializer(many=True, read_only=True, source='transportation_set')
class Meta:
model = Collection
# fields are all plus the adventures field
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures', 'created_at', 'start_date', 'end_date', 'transportations']

View file

@ -261,6 +261,10 @@ class CollectionViewSet(viewsets.ModelViewSet):
Prefetch('adventure_set', queryset=Adventure.objects.filter( Prefetch('adventure_set', queryset=Adventure.objects.filter(
Q(is_public=True) | Q(user_id=self.request.user.id) Q(is_public=True) | Q(user_id=self.request.user.id)
)) ))
).prefetch_related(
Prefetch('transportation_set', queryset=Transportation.objects.filter(
Q(is_public=True) | Q(user_id=self.request.user.id)
))
) )
return self.apply_sorting(collections) return self.apply_sorting(collections)
@ -296,6 +300,14 @@ class CollectionViewSet(viewsets.ModelViewSet):
serializer = self.get_serializer(queryset, many=True) serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data) return Response(serializer.data)
# make a view to return all of the associated transportations with a collection
@action(detail=True, methods=['get'])
def transportations(self, request, pk=None):
collection = self.get_object()
transportations = Transportation.objects.filter(collection=collection)
serializer = TransportationSerializer(transportations, many=True)
return Response(serializer.data)
class StatsViewSet(viewsets.ViewSet): class StatsViewSet(viewsets.ViewSet):
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
@ -395,6 +407,16 @@ class TransportationViewSet(viewsets.ModelViewSet):
return Response({"detail": "Listing all adventures is not allowed."}, return Response({"detail": "Listing all adventures is not allowed."},
status=status.HTTP_403_FORBIDDEN) status=status.HTTP_403_FORBIDDEN)
@action(detail=False, methods=['get'])
def all(self, request):
if not request.user.is_authenticated:
return Response({"error": "User is not authenticated"}, status=400)
queryset = Transportation.objects.filter(
Q(user_id=request.user.id)
)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
def get_queryset(self): def get_queryset(self):

View file

@ -0,0 +1,83 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
import Launch from '~icons/mdi/launch';
import TrashCanOutline from '~icons/mdi/trash-can-outline';
import FileDocumentEdit from '~icons/mdi/file-document-edit';
import { goto } from '$app/navigation';
import type { Collection } from '$lib/types';
import { addToast } from '$lib/toasts';
import Plus from '~icons/mdi/plus';
const dispatch = createEventDispatcher();
export let type: String | undefined | null;
// export let type: String;
function editAdventure() {
dispatch('edit', collection);
}
export let collection: Collection;
async function deleteCollection() {
let res = await fetch(`/collections/${collection.id}?/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
if (res.ok) {
console.log('Collection deleted');
addToast('info', 'Adventure deleted successfully!');
dispatch('delete', collection.id);
} else {
console.log('Error deleting adventure');
}
}
</script>
<div
class="card min-w-max lg:w-96 md:w-80 sm:w-60 xs:w-40 bg-primary-content shadow-xl overflow-hidden text-base-content"
>
<div class="card-body">
<h2 class="card-title overflow-ellipsis">{collection.name}</h2>
<p>{collection.adventures.length} Adventures</p>
{#if collection.start_date && collection.end_date}
<p>
Dates: {new Date(collection.start_date).toLocaleDateString('en-US', { timeZone: 'UTC' })} - {new Date(
collection.end_date
).toLocaleDateString('en-US', { timeZone: 'UTC' })}
</p>
<!-- display the duration in days -->
<p>
Duration: {Math.floor(
(new Date(collection.end_date).getTime() - new Date(collection.start_date).getTime()) /
(1000 * 60 * 60 * 24)
) + 1}{' '}
days
</p>{/if}
<div class="card-actions justify-end">
{#if type != 'link'}
<button on:click={deleteCollection} class="btn btn-secondary"
><TrashCanOutline class="w-5 h-5 mr-1" /></button
>
<button class="btn btn-primary" on:click={editAdventure}>
<FileDocumentEdit class="w-6 h-6" />
</button>
<button class="btn btn-primary" on:click={() => goto(`/collections/${collection.id}`)}
><Launch class="w-5 h-5 mr-1" /></button
>
{/if}
{#if type == 'link'}
<button class="btn btn-primary" on:click={() => dispatch('link', collection.id)}>
<Plus class="w-5 h-5 mr-1" />
</button>
{/if}
</div>
</div>
</div>

View file

@ -66,6 +66,7 @@ export type Collection = {
created_at?: string; created_at?: string;
start_date?: string; start_date?: string;
end_date?: string; end_date?: string;
transportations?: Transportation[];
}; };
export type OpenStreetMapPlace = { export type OpenStreetMapPlace = {
@ -84,3 +85,21 @@ export type OpenStreetMapPlace = {
display_name: string; display_name: string;
boundingbox: string[]; boundingbox: string[];
}; };
export type Transportation = {
id: number;
user_id: User;
type: string;
name: string;
description: string | null;
rating: number | null;
link: string | null;
date: string | null; // ISO 8601 date string
flight_number: string | null;
from_location: string | null;
to_location: string | null;
is_public: boolean;
collection: Collection | null;
created_at: string; // ISO 8601 date string
updated_at: string; // ISO 8601 date string
};

View file

@ -192,6 +192,7 @@
</div> </div>
{/if} {/if}
{#if collection} {#if collection}
{#if data.user}
<div class="fixed bottom-4 right-4 z-[999]"> <div class="fixed bottom-4 right-4 z-[999]">
<div class="flex flex-row items-center justify-center gap-4"> <div class="flex flex-row items-center justify-center gap-4">
<div class="dropdown dropdown-top dropdown-end"> <div class="dropdown dropdown-top dropdown-end">
@ -258,6 +259,7 @@
</div> </div>
</div> </div>
</div> </div>
{/if}
{#if collection.name} {#if collection.name}
<h1 class="text-center font-extrabold text-4xl mb-2">{collection.name}</h1> <h1 class="text-center font-extrabold text-4xl mb-2">{collection.name}</h1>
{/if} {/if}
@ -293,6 +295,16 @@
{/each} {/each}
</div> </div>
{#if collection.transportations && collection.transportations.length > 0}
<h1 class="text-center font-bold text-4xl mt-4 mb-2">Transportation</h1>
{#each collection.transportations as transportation}
<p>{transportation.id}</p>
<p>{transportation.name}</p>
<p>{transportation.type}</p>
<p>{transportation.date}</p>
{/each}
{/if}
{#if collection.start_date && collection.end_date} {#if collection.start_date && collection.end_date}
<h1 class="text-center font-bold text-4xl mt-4">Itinerary by Date</h1> <h1 class="text-center font-bold text-4xl mt-4">Itinerary by Date</h1>
{#if numberOfDays} {#if numberOfDays}