1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-01 19:25:17 +02:00

refactor: Improve performance and fix conditional rendering issue in +page.svelte

This commit is contained in:
Sean Morley 2024-07-30 07:10:41 -04:00
parent f98ca62aad
commit 3a01178acb
2 changed files with 18 additions and 19 deletions

View file

@ -175,6 +175,10 @@ class CollectionViewSet(viewsets.ModelViewSet):
permission_classes = [IsOwnerOrReadOnly, IsPublicReadOnly]
pagination_class = StandardResultsSetPagination
def get_queryset(self):
print(self.request.user.id)
return Collection.objects.filter(user_id=self.request.user.id)
def apply_sorting(self, queryset):
order_by = self.request.query_params.get('order_by', 'name')
order_direction = self.request.query_params.get('order_direction', 'asc')
@ -255,9 +259,19 @@ class CollectionViewSet(viewsets.ModelViewSet):
return Response(serializer.data)
def get_queryset(self):
collections = Collection.objects.filter(
adventures = None
if self.action == 'retrieve':
# For individual collection retrieval, include public collections
adventures = Collection.objects.filter(
Q(is_public=True) | Q(user_id=self.request.user.id)
).prefetch_related(
)
else:
# For other actions, only include user's own collections
adventures = Collection.objects.filter(user_id=self.request.user.id)
adventures = adventures.prefetch_related(
Prefetch('adventure_set', queryset=Adventure.objects.filter(
Q(is_public=True) | Q(user_id=self.request.user.id)
))
@ -266,7 +280,7 @@ class CollectionViewSet(viewsets.ModelViewSet):
Q(is_public=True) | Q(user_id=self.request.user.id)
))
)
return self.apply_sorting(collections)
return self.apply_sorting(adventures)
def perform_create(self, serializer):
serializer.save(user_id=self.request.user)

View file

@ -19,7 +19,6 @@
let showVisited = true;
let showPlanned = true;
let showCollectionAdventures = false;
$: {
if (!showVisited) {
@ -34,12 +33,6 @@
const plannedMarkers = data.props.markers.filter((marker) => marker.type === 'planned');
markers = [...markers, ...plannedMarkers];
}
if (!showCollectionAdventures) {
markers = markers.filter((marker) => marker.collection === null);
} else {
const collectionMarkers = data.props.markers.filter((marker) => marker.collection !== null);
markers = [...markers, ...collectionMarkers];
}
}
let newMarker = [];
@ -124,14 +117,6 @@
<span class="label-text">Planned</span>
<input type="checkbox" bind:checked={showPlanned} class="checkbox checkbox-primary" />
</label>
<label class="label cursor-pointer">
<span class="label-text">Collection Adventures</span>
<input
type="checkbox"
bind:checked={showCollectionAdventures}
class="checkbox checkbox-primary"
/>
</label>
{#if newMarker.length > 0}
<button type="button" class="btn btn-primary mb-2" on:click={() => (createModalOpen = true)}