From 0eb4bc706a783d7528f55d79e19e027a5d293a45 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Wed, 22 Jan 2025 08:36:02 -0500 Subject: [PATCH] feat: enhance adventure handling with user ID support in serializers and attachment view; refactor saveEdit function and clean up Avatar component --- backend/server/adventures/serializers.py | 8 +- .../adventures/views/attachment_view.py | 5 +- .../src/lib/components/AdventureModal.svelte | 364 +++++++++--------- frontend/src/lib/components/Avatar.svelte | 1 - .../src/routes/adventures/+page.server.ts | 3 + .../src/routes/adventures/[id]/+page.svelte | 4 +- 6 files changed, 199 insertions(+), 186 deletions(-) diff --git a/backend/server/adventures/serializers.py b/backend/server/adventures/serializers.py index c213169..a7c0bd2 100644 --- a/backend/server/adventures/serializers.py +++ b/backend/server/adventures/serializers.py @@ -8,8 +8,8 @@ from main.utils import CustomModelSerializer class AdventureImageSerializer(CustomModelSerializer): class Meta: model = AdventureImage - fields = ['id', 'image', 'adventure', 'is_primary'] - read_only_fields = ['id'] + fields = ['id', 'image', 'adventure', 'is_primary', 'user_id'] + read_only_fields = ['id', 'user_id'] def to_representation(self, instance): representation = super().to_representation(instance) @@ -25,8 +25,8 @@ class AttachmentSerializer(CustomModelSerializer): extension = serializers.SerializerMethodField() class Meta: model = Attachment - fields = ['id', 'file', 'adventure', 'extension', 'name'] - read_only_fields = ['id'] + fields = ['id', 'file', 'adventure', 'extension', 'name', 'user_id'] + read_only_fields = ['id', 'user_id'] def get_extension(self, obj): return obj.file.name.split('.')[-1] diff --git a/backend/server/adventures/views/attachment_view.py b/backend/server/adventures/views/attachment_view.py index e83bdea..0292b16 100644 --- a/backend/server/adventures/views/attachment_view.py +++ b/backend/server/adventures/views/attachment_view.py @@ -34,4 +34,7 @@ class AttachmentViewSet(viewsets.ModelViewSet): else: return Response({"error": "User does not own this adventure"}, status=status.HTTP_403_FORBIDDEN) - return super().create(request, *args, **kwargs) \ No newline at end of file + return super().create(request, *args, **kwargs) + + def perform_create(self, serializer): + serializer.save(user_id=self.request.user) \ No newline at end of file diff --git a/frontend/src/lib/components/AdventureModal.svelte b/frontend/src/lib/components/AdventureModal.svelte index cb5a920..d6d0121 100644 --- a/frontend/src/lib/components/AdventureModal.svelte +++ b/frontend/src/lib/components/AdventureModal.svelte @@ -173,16 +173,27 @@ } } + let selectedFile: File | null = null; + + function handleFileChange(event: Event) { + const input = event.target as HTMLInputElement; + if (input.files && input.files.length) { + selectedFile = input.files[0]; + console.log('Selected file:', selectedFile); + } + } + async function uploadAttachment(event: Event) { event.preventDefault(); console.log('UPLOAD'); + console.log(selectedFile); - if (!fileInput || !fileInput.files || fileInput.files.length === 0) { + if (!selectedFile) { console.error('No files selected'); return; } - const file = fileInput.files[0]; + const file = selectedFile; console.log(file); const formData = new FormData(); @@ -962,69 +973,6 @@ it would also work to just use on:click on the MapLibre component itself. --> - -
- -
- {$t('adventures.attachments')} ({adventure.attachments?.length || 0}) -
-
-
- {#each adventure.attachments as attachment} - (attachmentToEdit = e.detail)} - /> - {/each} -
-
{ - e.preventDefault(); - uploadAttachment(e); - }} - > -
- - - - -
-
- {#if attachmentToEdit} -
{ - e.preventDefault(); - editAttachment(); - }} - > -
- - -
-
- {/if} -
-
@@ -1185,122 +1133,180 @@ it would also work to just use on:click on the MapLibre component itself. -->
{:else} -

{$t('adventures.upload_images_here')}

- -
- -
- - - -
-
- -
- -
- - -
-
- -
- -
- - -
- {#if wikiImageError} -

{$t('adventures.wiki_image_error')}

- {/if} -
- - {#if immichIntegration} - { - url = e.detail; - fetchImage(); - }} - /> - {/if} - -
- - {#if images.length > 0} -

{$t('adventures.my_images')}

-
- {#each images as image} -
- - {#if !image.is_primary} - - {:else} - - -
- -
- {/if} - {image.id} + +
+ +
+ {$t('adventures.images')} ({adventure.images?.length || 0}) +
+
+ +
+ + +
+ +
+ +
+ + +
+
+ +
+ +
+ + +
+ {#if wikiImageError} +

{$t('adventures.wiki_image_error')}

+ {/if} +
+ + {#if immichIntegration} + { + url = e.detail; + fetchImage(); + }} + /> + {/if} + +
+ + {#if images.length > 0} +

{$t('adventures.my_images')}

+
+ {#each images as image} +
+ + {#if !image.is_primary} + + {:else} + + +
+ +
+ {/if} + {image.id} +
+ {/each} +
+ {:else} +

{$t('adventures.no_images')}

+ {/if} +
+
+
diff --git a/frontend/src/lib/components/Avatar.svelte b/frontend/src/lib/components/Avatar.svelte index a94a8eb..01eb068 100644 --- a/frontend/src/lib/components/Avatar.svelte +++ b/frontend/src/lib/components/Avatar.svelte @@ -36,7 +36,6 @@

  • -
  • diff --git a/frontend/src/routes/adventures/+page.server.ts b/frontend/src/routes/adventures/+page.server.ts index 89887f3..9875399 100644 --- a/frontend/src/routes/adventures/+page.server.ts +++ b/frontend/src/routes/adventures/+page.server.ts @@ -91,6 +91,9 @@ export const actions: Actions = { body: formData }); let data = await res.json(); + + console.log(res); + console.log(data); return data; } }; diff --git a/frontend/src/routes/adventures/[id]/+page.svelte b/frontend/src/routes/adventures/[id]/+page.svelte index fb0528f..35cd73c 100644 --- a/frontend/src/routes/adventures/[id]/+page.svelte +++ b/frontend/src/routes/adventures/[id]/+page.svelte @@ -102,9 +102,11 @@ await getGpxFiles(); }); - function saveEdit(event: CustomEvent) { + async function saveEdit(event: CustomEvent) { adventure = event.detail; isEditModalOpen = false; + geojson = null; + await getGpxFiles(); }