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

collections is public fix

This commit is contained in:
Sean Morley 2024-07-15 18:51:05 -04:00
parent 7e5e4edd4d
commit d91b8c7128
5 changed files with 83 additions and 14 deletions

View file

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

View file

@ -35,6 +35,7 @@
}
function add(event: CustomEvent<Adventure>) {
adventures = adventures.filter((a) => a.id !== event.detail.id);
dispatch('add', event.detail);
}
@ -48,12 +49,17 @@
<dialog id="my_modal_1" class="modal">
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<div class="modal-box" role="dialog" on:keydown={handleKeydown} tabindex="0">
<div class="modal-box w-11/12 max-w-5xl" role="dialog" on:keydown={handleKeydown} tabindex="0">
<h1 class="text-center font-bold text-4xl mb-6">My Adventures</h1>
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each adventures as adventure}
<AdventureCard type="link" {adventure} on:link={add} />
{/each}
{#if adventures.length === 0}
<p class="text-center text-lg">
No adventures found that can be linked to this collection.
</p>
{/if}
</div>
<button class="btn btn-primary" on:click={close}>Close</button>
</div>

View file

@ -246,17 +246,19 @@
Location</button
>
</div>
<div class="mb-2">
<label for="is_public">Public <Earth class="inline-block -mt-1 mb-1 w-6 h-6" /></label><br
/>
<input
type="checkbox"
class="toggle toggle-primary"
id="is_public"
name="is_public"
bind:checked={adventureToEdit.is_public}
/>
</div>
{#if adventureToEdit.collection === null}
<div class="mb-2">
<label for="is_public">Public <Earth class="inline-block -mt-1 mb-1 w-6 h-6" /></label
><br />
<input
type="checkbox"
class="toggle toggle-primary"
id="is_public"
name="is_public"
bind:checked={adventureToEdit.is_public}
/>
</div>
{/if}
{#if adventureToEdit.is_public}
<div class="bg-neutral p-4 rounded-md shadow-sm">

View file

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

View file

@ -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<Adventure>) {
adventureToEdit = event.detail;
isEditModalOpen = true;
}
function saveEdit(event: CustomEvent<Adventure>) {
adventures = adventures.map((adventure) => {
if (adventure.id === event.detail.id) {
return event.detail;
}
return adventure;
});
isEditModalOpen = false;
}
</script>
{#if isShowingCreateModal}
@ -64,6 +83,14 @@
/>
{/if}
{#if isEditModalOpen}
<EditAdventure
{adventureToEdit}
on:close={() => (isEditModalOpen = false)}
on:saveEdit={saveEdit}
/>
{/if}
{#if notFound}
<div
class="flex min-h-[100dvh] flex-col items-center justify-center bg-background px-4 py-12 sm:px-6 lg:px-8 -mt-20"
@ -127,7 +154,12 @@
<h1 class="text-center font-semibold text-2xl mt-4 mb-2">Linked Adventures</h1>
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
{#each adventures as adventure}
<AdventureCard on:delete={deleteAdventure} type={adventure.type} {adventure} />
<AdventureCard
on:edit={editAdventure}
on:delete={deleteAdventure}
type={adventure.type}
{adventure}
/>
{/each}
</div>