mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 21:09:37 +02:00
Archive Collections
This commit is contained in:
parent
493c25018c
commit
1858790308
9 changed files with 173 additions and 33 deletions
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 5.0.7 on 2024-08-07 16:20
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('adventures', '0020_checklist_checklistitem'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='collection',
|
||||||
|
name='is_archived',
|
||||||
|
field=models.BooleanField(default=False),
|
||||||
|
),
|
||||||
|
]
|
|
@ -72,6 +72,7 @@ class Collection(models.Model):
|
||||||
start_date = models.DateField(blank=True, null=True)
|
start_date = models.DateField(blank=True, null=True)
|
||||||
end_date = models.DateField(blank=True, null=True)
|
end_date = models.DateField(blank=True, null=True)
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
is_archived = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
|
||||||
# if connected adventures are private and collection is public, raise an error
|
# if connected adventures are private and collection is public, raise an error
|
||||||
|
|
|
@ -205,5 +205,5 @@ class CollectionSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Collection
|
model = Collection
|
||||||
# fields are all plus the adventures field
|
# fields are all plus the adventures field
|
||||||
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures', 'created_at', 'start_date', 'end_date', 'transportations', 'notes', 'updated_at', 'checklists']
|
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures', 'created_at', 'start_date', 'end_date', 'transportations', 'notes', 'updated_at', 'checklists', 'is_archived']
|
||||||
read_only_fields = ['id', 'created_at', 'updated_at', 'user_id']
|
read_only_fields = ['id', 'created_at', 'updated_at', 'user_id']
|
||||||
|
|
|
@ -209,9 +209,8 @@ class CollectionViewSet(viewsets.ModelViewSet):
|
||||||
permission_classes = [IsOwnerOrReadOnly, IsPublicReadOnly]
|
permission_classes = [IsOwnerOrReadOnly, IsPublicReadOnly]
|
||||||
pagination_class = StandardResultsSetPagination
|
pagination_class = StandardResultsSetPagination
|
||||||
|
|
||||||
def get_queryset(self):
|
# def get_queryset(self):
|
||||||
print(self.request.user.id)
|
# return Collection.objects.filter(Q(user_id=self.request.user.id) & Q(is_archived=False))
|
||||||
return Collection.objects.filter(user_id=self.request.user.id)
|
|
||||||
|
|
||||||
def apply_sorting(self, queryset):
|
def apply_sorting(self, queryset):
|
||||||
order_by = self.request.query_params.get('order_by', 'name')
|
order_by = self.request.query_params.get('order_by', 'name')
|
||||||
|
@ -263,6 +262,20 @@ class CollectionViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
@action(detail=False, methods=['get'])
|
||||||
|
def archived(self, request):
|
||||||
|
if not request.user.is_authenticated:
|
||||||
|
return Response({"error": "User is not authenticated"}, status=400)
|
||||||
|
|
||||||
|
queryset = Collection.objects.filter(
|
||||||
|
Q(user_id=request.user.id) & Q(is_archived=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
queryset = self.apply_sorting(queryset)
|
||||||
|
serializer = self.get_serializer(queryset, many=True)
|
||||||
|
|
||||||
|
return Response(serializer.data)
|
||||||
|
|
||||||
# this make the is_public field of the collection cascade to the adventures
|
# this make the is_public field of the collection cascade to the adventures
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def update(self, request, *args, **kwargs):
|
def update(self, request, *args, **kwargs):
|
||||||
|
@ -298,36 +311,21 @@ class CollectionViewSet(viewsets.ModelViewSet):
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
|
if self.action == 'destroy':
|
||||||
|
return Collection.objects.filter(user_id=self.request.user.id)
|
||||||
|
|
||||||
adventures = None
|
if self.action in ['update', 'partial_update']:
|
||||||
|
return Collection.objects.filter(user_id=self.request.user.id)
|
||||||
|
|
||||||
if self.action == 'retrieve':
|
if self.action == 'retrieve':
|
||||||
# For individual collection retrieval, include public collections
|
return Collection.objects.filter(
|
||||||
adventures = 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)
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
# For other actions, only include user's own collections
|
|
||||||
adventures = Collection.objects.filter(user_id=self.request.user.id)
|
|
||||||
|
|
||||||
# adventures = adventures.prefetch_related(
|
# For other actions (like list), only include user's non-archived collections
|
||||||
# Prefetch('adventure_set', queryset=Adventure.objects.filter(
|
return Collection.objects.filter(
|
||||||
# Q(is_public=True) | Q(user_id=self.request.user.id)
|
Q(user_id=self.request.user.id) & Q(is_archived=False)
|
||||||
# ))
|
)
|
||||||
# ).prefetch_related(
|
|
||||||
# Prefetch('transportation_set', queryset=Transportation.objects.filter(
|
|
||||||
# Q(is_public=True) | Q(user_id=self.request.user.id)
|
|
||||||
# ))
|
|
||||||
# ).prefetch_related(
|
|
||||||
# Prefetch('note_set', queryset=Note.objects.filter(
|
|
||||||
# Q(is_public=True) | Q(user_id=self.request.user.id)
|
|
||||||
# ))
|
|
||||||
# ).prefetch_related(
|
|
||||||
# Prefetch('checklist_set', queryset=Checklist.objects.filter(
|
|
||||||
# Q(is_public=True) | Q(user_id=self.request.user.id)
|
|
||||||
# ))
|
|
||||||
# )
|
|
||||||
return self.apply_sorting(adventures)
|
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
serializer.save(user_id=self.request.user)
|
serializer.save(user_id=self.request.user)
|
||||||
|
|
|
@ -5,12 +5,15 @@
|
||||||
import TrashCanOutline from '~icons/mdi/trash-can-outline';
|
import TrashCanOutline from '~icons/mdi/trash-can-outline';
|
||||||
|
|
||||||
import FileDocumentEdit from '~icons/mdi/file-document-edit';
|
import FileDocumentEdit from '~icons/mdi/file-document-edit';
|
||||||
|
import ArchiveArrowDown from '~icons/mdi/archive-arrow-down';
|
||||||
|
import ArchiveArrowUp from '~icons/mdi/archive-arrow-up';
|
||||||
|
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import type { Collection } from '$lib/types';
|
import type { Collection } from '$lib/types';
|
||||||
import { addToast } from '$lib/toasts';
|
import { addToast } from '$lib/toasts';
|
||||||
|
|
||||||
import Plus from '~icons/mdi/plus';
|
import Plus from '~icons/mdi/plus';
|
||||||
|
import { json } from '@sveltejs/kit';
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
|
@ -22,6 +25,24 @@
|
||||||
dispatch('edit', collection);
|
dispatch('edit', collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function archiveCollection(is_archived: boolean) {
|
||||||
|
console.log(JSON.stringify({ is_archived: is_archived }));
|
||||||
|
let res = await fetch(`/api/collections/${collection.id}/`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ is_archived: is_archived })
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
console.log(`Collection ${is_archived ? 'archived' : 'unarchived'}`);
|
||||||
|
addToast('info', `Adventure ${is_archived ? 'archived' : 'unarchived'} successfully!`);
|
||||||
|
dispatch('delete', collection.id);
|
||||||
|
} else {
|
||||||
|
console.log('Error archiving adventure');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export let collection: Collection;
|
export let collection: Collection;
|
||||||
|
|
||||||
async function deleteCollection() {
|
async function deleteCollection() {
|
||||||
|
@ -61,15 +82,22 @@
|
||||||
) + 1}{' '}
|
) + 1}{' '}
|
||||||
days
|
days
|
||||||
</p>{/if}
|
</p>{/if}
|
||||||
|
<div class="inline-flex gap-2 mb-2">
|
||||||
<div class="badge badge-neutral">{collection.is_public ? 'Public' : 'Private'}</div>
|
<div class="badge badge-neutral">{collection.is_public ? 'Public' : 'Private'}</div>
|
||||||
|
{#if collection.is_archived}
|
||||||
|
<div class="badge badge-warning">Archived</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
<div class="card-actions justify-end">
|
<div class="card-actions justify-end">
|
||||||
{#if type != 'link'}
|
{#if type != 'link'}
|
||||||
<button on:click={deleteCollection} class="btn btn-secondary"
|
<button on:click={deleteCollection} class="btn btn-secondary"
|
||||||
><TrashCanOutline class="w-5 h-5 mr-1" /></button
|
><TrashCanOutline class="w-5 h-5 mr-1" /></button
|
||||||
>
|
>
|
||||||
|
{#if !collection.is_archived}
|
||||||
<button class="btn btn-primary" on:click={editAdventure}>
|
<button class="btn btn-primary" on:click={editAdventure}>
|
||||||
<FileDocumentEdit class="w-6 h-6" />
|
<FileDocumentEdit class="w-6 h-6" />
|
||||||
</button>
|
</button>
|
||||||
|
{/if}
|
||||||
<button class="btn btn-primary" on:click={() => goto(`/collections/${collection.id}`)}
|
<button class="btn btn-primary" on:click={() => goto(`/collections/${collection.id}`)}
|
||||||
><Launch class="w-5 h-5 mr-1" /></button
|
><Launch class="w-5 h-5 mr-1" /></button
|
||||||
>
|
>
|
||||||
|
@ -79,6 +107,15 @@
|
||||||
<Plus class="w-5 h-5 mr-1" />
|
<Plus class="w-5 h-5 mr-1" />
|
||||||
</button>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
|
{#if collection.is_archived}
|
||||||
|
<button class="btn btn-primary" on:click={() => archiveCollection(false)}>
|
||||||
|
<ArchiveArrowUp class="w-5 h-5 mr-1" />
|
||||||
|
</button>
|
||||||
|
{:else}
|
||||||
|
<button class="btn btn-primary" on:click={() => archiveCollection(true)}>
|
||||||
|
<ArchiveArrowDown class="w-5 h-5 mr" />
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -70,6 +70,7 @@ export type Collection = {
|
||||||
transportations?: Transportation[];
|
transportations?: Transportation[];
|
||||||
notes?: Note[];
|
notes?: Note[];
|
||||||
checklists?: Checklist[];
|
checklists?: Checklist[];
|
||||||
|
is_archived?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type OpenStreetMapPlace = {
|
export type OpenStreetMapPlace = {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { enhance, deserialize } from '$app/forms';
|
import { enhance, deserialize } from '$app/forms';
|
||||||
|
import { goto } from '$app/navigation';
|
||||||
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
||||||
import CollectionCard from '$lib/components/CollectionCard.svelte';
|
import CollectionCard from '$lib/components/CollectionCard.svelte';
|
||||||
import EditAdventure from '$lib/components/EditAdventure.svelte';
|
import EditAdventure from '$lib/components/EditAdventure.svelte';
|
||||||
|
@ -247,6 +248,11 @@
|
||||||
<button type="submit" class="btn btn-success btn-primary mt-4">Filter</button>
|
<button type="submit" class="btn btn-success btn-primary mt-4">Filter</button>
|
||||||
</form>
|
</form>
|
||||||
<div class="divider"></div>
|
<div class="divider"></div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="btn btn-neutral btn-primary mt-4"
|
||||||
|
on:click={() => goto('/collections/archived')}>Archived Collections</button
|
||||||
|
>
|
||||||
</div>
|
</div>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
35
frontend/src/routes/collections/archived/+page.server.ts
Normal file
35
frontend/src/routes/collections/archived/+page.server.ts
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import { redirect } from '@sveltejs/kit';
|
||||||
|
import type { PageServerLoad } from './$types';
|
||||||
|
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
||||||
|
import type { Adventure } from '$lib/types';
|
||||||
|
const serverEndpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
|
||||||
|
|
||||||
|
export const load = (async (event) => {
|
||||||
|
if (!event.locals.user) {
|
||||||
|
return redirect(302, '/login');
|
||||||
|
} else {
|
||||||
|
let next = null;
|
||||||
|
let previous = null;
|
||||||
|
let count = 0;
|
||||||
|
let adventures: Adventure[] = [];
|
||||||
|
let initialFetch = await fetch(`${serverEndpoint}/api/collections/archived/`, {
|
||||||
|
headers: {
|
||||||
|
Cookie: `${event.cookies.get('auth')}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!initialFetch.ok) {
|
||||||
|
console.error('Failed to fetch visited adventures');
|
||||||
|
return redirect(302, '/login');
|
||||||
|
} else {
|
||||||
|
let res = await initialFetch.json();
|
||||||
|
let visited = res as Adventure[];
|
||||||
|
adventures = [...adventures, ...visited];
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
adventures
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}) satisfies PageServerLoad;
|
44
frontend/src/routes/collections/archived/+page.svelte
Normal file
44
frontend/src/routes/collections/archived/+page.svelte
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<script lang="ts">
|
||||||
|
import { enhance, deserialize } from '$app/forms';
|
||||||
|
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
||||||
|
import CollectionCard from '$lib/components/CollectionCard.svelte';
|
||||||
|
import EditAdventure from '$lib/components/EditAdventure.svelte';
|
||||||
|
import EditCollection from '$lib/components/EditCollection.svelte';
|
||||||
|
import NewAdventure from '$lib/components/NewAdventure.svelte';
|
||||||
|
import NewCollection from '$lib/components/NewCollection.svelte';
|
||||||
|
import NotFound from '$lib/components/NotFound.svelte';
|
||||||
|
import type { Adventure, Collection } from '$lib/types';
|
||||||
|
|
||||||
|
import Plus from '~icons/mdi/plus';
|
||||||
|
|
||||||
|
export let data: any;
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
|
let collections: Collection[] = data.props.adventures || [];
|
||||||
|
|
||||||
|
function deleteCollection(event: CustomEvent<number>) {
|
||||||
|
collections = collections.filter((collection) => collection.id !== event.detail);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="drawer lg:drawer-open">
|
||||||
|
<div class="drawer-content">
|
||||||
|
<!-- Page content -->
|
||||||
|
<h1 class="text-center font-bold text-4xl mb-6">Archived Collections</h1>
|
||||||
|
{#if collections.length === 0}
|
||||||
|
<NotFound error={undefined} />
|
||||||
|
{/if}
|
||||||
|
<div class="p-4">
|
||||||
|
<div class="flex flex-wrap gap-4 mr-4 justify-center content-center">
|
||||||
|
{#each collections as collection}
|
||||||
|
<CollectionCard type="" {collection} on:delete={deleteCollection} />
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<svelte:head>
|
||||||
|
<title>Collections</title>
|
||||||
|
<meta name="description" content="View your adventure collections." />
|
||||||
|
</svelte:head>
|
Loading…
Add table
Add a link
Reference in a new issue