From d91b8c712822713d6556d044baf45fd9ca6b7079 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Mon, 15 Jul 2024 18:51:05 -0400 Subject: [PATCH] collections is public fix --- backend/server/adventures/views.py | 29 ++++++++++++++++ .../src/lib/components/AdventureLink.svelte | 8 ++++- .../src/lib/components/EditAdventure.svelte | 24 +++++++------ frontend/src/lib/types.ts | 2 +- .../src/routes/collections/[id]/+page.svelte | 34 ++++++++++++++++++- 5 files changed, 83 insertions(+), 14 deletions(-) diff --git a/backend/server/adventures/views.py b/backend/server/adventures/views.py index 1474b6b..cb4729b 100644 --- a/backend/server/adventures/views.py +++ b/backend/server/adventures/views.py @@ -1,4 +1,5 @@ import requests +from django.db import transaction from rest_framework.decorators import action from rest_framework import viewsets from django.db.models.functions import Lower @@ -149,6 +150,34 @@ class CollectionViewSet(viewsets.ModelViewSet): print(f"Ordering by: {ordering}") # For debugging return queryset.order_by(ordering) + + # this make the is_public field of the collection cascade to the adventures + @transaction.atomic + def update(self, request, *args, **kwargs): + partial = kwargs.pop('partial', False) + instance = self.get_object() + serializer = self.get_serializer(instance, data=request.data, partial=partial) + serializer.is_valid(raise_exception=True) + + # Check if the 'is_public' field is present in the update data + if 'is_public' in serializer.validated_data: + new_public_status = serializer.validated_data['is_public'] + + # Update associated adventures to match the collection's is_public status + Adventure.objects.filter(collection=instance).update(is_public=new_public_status) + + # Log the action (optional) + action = "public" if new_public_status else "private" + print(f"Collection {instance.id} and its adventures were set to {action}") + + self.perform_update(serializer) + + if getattr(instance, '_prefetched_objects_cache', None): + # If 'prefetch_related' has been applied to a queryset, we need to + # forcibly invalidate the prefetch cache on the instance. + instance._prefetched_objects_cache = {} + + return Response(serializer.data) def get_queryset(self): collections = Collection.objects.filter( diff --git a/frontend/src/lib/components/AdventureLink.svelte b/frontend/src/lib/components/AdventureLink.svelte index 1783f0d..53e57b2 100644 --- a/frontend/src/lib/components/AdventureLink.svelte +++ b/frontend/src/lib/components/AdventureLink.svelte @@ -35,6 +35,7 @@ } function add(event: CustomEvent) { + adventures = adventures.filter((a) => a.id !== event.detail.id); dispatch('add', event.detail); } @@ -48,12 +49,17 @@ - -
-
- -
+ {#if adventureToEdit.collection === null} +
+
+ +
+ {/if} {#if adventureToEdit.is_public}
diff --git a/frontend/src/lib/types.ts b/frontend/src/lib/types.ts index 9bc23c0..1d4296e 100644 --- a/frontend/src/lib/types.ts +++ b/frontend/src/lib/types.ts @@ -21,7 +21,7 @@ export type Adventure = { link?: string | null; image?: string | null; date?: string | null; // Assuming date is a string in 'YYYY-MM-DD' format - trip_id?: number | null; + collection?: number | null; latitude: number | null; longitude: number | null; is_public: boolean; diff --git a/frontend/src/routes/collections/[id]/+page.svelte b/frontend/src/routes/collections/[id]/+page.svelte index aa25ece..4dc9a4e 100644 --- a/frontend/src/routes/collections/[id]/+page.svelte +++ b/frontend/src/routes/collections/[id]/+page.svelte @@ -8,6 +8,7 @@ import Plus from '~icons/mdi/plus'; import AdventureCard from '$lib/components/AdventureCard.svelte'; import AdventureLink from '$lib/components/AdventureLink.svelte'; + import EditAdventure from '$lib/components/EditAdventure.svelte'; export let data: PageData; @@ -53,6 +54,24 @@ } } } + + let adventureToEdit: Adventure; + let isEditModalOpen: boolean = false; + + function editAdventure(event: CustomEvent) { + adventureToEdit = event.detail; + isEditModalOpen = true; + } + + function saveEdit(event: CustomEvent) { + adventures = adventures.map((adventure) => { + if (adventure.id === event.detail.id) { + return event.detail; + } + return adventure; + }); + isEditModalOpen = false; + } {#if isShowingCreateModal} @@ -64,6 +83,14 @@ /> {/if} +{#if isEditModalOpen} + (isEditModalOpen = false)} + on:saveEdit={saveEdit} + /> +{/if} + {#if notFound}
Linked Adventures
{#each adventures as adventure} - + {/each}