mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-20 21:39:37 +02:00
collections is public fix
This commit is contained in:
parent
7e5e4edd4d
commit
d91b8c7128
5 changed files with 83 additions and 14 deletions
|
@ -1,4 +1,5 @@
|
||||||
import requests
|
import requests
|
||||||
|
from django.db import transaction
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
from django.db.models.functions import Lower
|
from django.db.models.functions import Lower
|
||||||
|
@ -150,6 +151,34 @@ class CollectionViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
return queryset.order_by(ordering)
|
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):
|
def get_queryset(self):
|
||||||
collections = Collection.objects.filter(
|
collections = Collection.objects.filter(
|
||||||
Q(is_public=True) | Q(user_id=self.request.user.id)
|
Q(is_public=True) | Q(user_id=self.request.user.id)
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function add(event: CustomEvent<Adventure>) {
|
function add(event: CustomEvent<Adventure>) {
|
||||||
|
adventures = adventures.filter((a) => a.id !== event.detail.id);
|
||||||
dispatch('add', event.detail);
|
dispatch('add', event.detail);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,12 +49,17 @@
|
||||||
<dialog id="my_modal_1" class="modal">
|
<dialog id="my_modal_1" class="modal">
|
||||||
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
|
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
|
||||||
<!-- svelte-ignore a11y-no-noninteractive-tabindex -->
|
<!-- 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>
|
<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">
|
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
|
||||||
{#each adventures as adventure}
|
{#each adventures as adventure}
|
||||||
<AdventureCard type="link" {adventure} on:link={add} />
|
<AdventureCard type="link" {adventure} on:link={add} />
|
||||||
{/each}
|
{/each}
|
||||||
|
{#if adventures.length === 0}
|
||||||
|
<p class="text-center text-lg">
|
||||||
|
No adventures found that can be linked to this collection.
|
||||||
|
</p>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
<button class="btn btn-primary" on:click={close}>Close</button>
|
<button class="btn btn-primary" on:click={close}>Close</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -246,17 +246,19 @@
|
||||||
Location</button
|
Location</button
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
<div class="mb-2">
|
{#if adventureToEdit.collection === null}
|
||||||
<label for="is_public">Public <Earth class="inline-block -mt-1 mb-1 w-6 h-6" /></label><br
|
<div class="mb-2">
|
||||||
/>
|
<label for="is_public">Public <Earth class="inline-block -mt-1 mb-1 w-6 h-6" /></label
|
||||||
<input
|
><br />
|
||||||
type="checkbox"
|
<input
|
||||||
class="toggle toggle-primary"
|
type="checkbox"
|
||||||
id="is_public"
|
class="toggle toggle-primary"
|
||||||
name="is_public"
|
id="is_public"
|
||||||
bind:checked={adventureToEdit.is_public}
|
name="is_public"
|
||||||
/>
|
bind:checked={adventureToEdit.is_public}
|
||||||
</div>
|
/>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
{#if adventureToEdit.is_public}
|
{#if adventureToEdit.is_public}
|
||||||
<div class="bg-neutral p-4 rounded-md shadow-sm">
|
<div class="bg-neutral p-4 rounded-md shadow-sm">
|
||||||
|
|
|
@ -21,7 +21,7 @@ export type Adventure = {
|
||||||
link?: string | null;
|
link?: string | null;
|
||||||
image?: string | null;
|
image?: string | null;
|
||||||
date?: string | null; // Assuming date is a string in 'YYYY-MM-DD' format
|
date?: string | null; // Assuming date is a string in 'YYYY-MM-DD' format
|
||||||
trip_id?: number | null;
|
collection?: number | null;
|
||||||
latitude: number | null;
|
latitude: number | null;
|
||||||
longitude: number | null;
|
longitude: number | null;
|
||||||
is_public: boolean;
|
is_public: boolean;
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
import Plus from '~icons/mdi/plus';
|
import Plus from '~icons/mdi/plus';
|
||||||
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
||||||
import AdventureLink from '$lib/components/AdventureLink.svelte';
|
import AdventureLink from '$lib/components/AdventureLink.svelte';
|
||||||
|
import EditAdventure from '$lib/components/EditAdventure.svelte';
|
||||||
|
|
||||||
export let data: PageData;
|
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>
|
</script>
|
||||||
|
|
||||||
{#if isShowingCreateModal}
|
{#if isShowingCreateModal}
|
||||||
|
@ -64,6 +83,14 @@
|
||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if isEditModalOpen}
|
||||||
|
<EditAdventure
|
||||||
|
{adventureToEdit}
|
||||||
|
on:close={() => (isEditModalOpen = false)}
|
||||||
|
on:saveEdit={saveEdit}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
|
||||||
{#if notFound}
|
{#if notFound}
|
||||||
<div
|
<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"
|
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>
|
<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">
|
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
|
||||||
{#each adventures as adventure}
|
{#each adventures as adventure}
|
||||||
<AdventureCard on:delete={deleteAdventure} type={adventure.type} {adventure} />
|
<AdventureCard
|
||||||
|
on:edit={editAdventure}
|
||||||
|
on:delete={deleteAdventure}
|
||||||
|
type={adventure.type}
|
||||||
|
{adventure}
|
||||||
|
/>
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue