mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-24 07:19:36 +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)
|
||||
end_date = models.DateField(blank=True, null=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
|
||||
|
|
|
@ -205,5 +205,5 @@ class CollectionSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = Collection
|
||||
# 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']
|
||||
|
|
|
@ -209,9 +209,8 @@ 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 get_queryset(self):
|
||||
# return Collection.objects.filter(Q(user_id=self.request.user.id) & Q(is_archived=False))
|
||||
|
||||
def apply_sorting(self, queryset):
|
||||
order_by = self.request.query_params.get('order_by', 'name')
|
||||
|
@ -263,6 +262,20 @@ class CollectionViewSet(viewsets.ModelViewSet):
|
|||
|
||||
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
|
||||
@transaction.atomic
|
||||
def update(self, request, *args, **kwargs):
|
||||
|
@ -298,36 +311,21 @@ class CollectionViewSet(viewsets.ModelViewSet):
|
|||
return Response(serializer.data)
|
||||
|
||||
def get_queryset(self):
|
||||
|
||||
adventures = None
|
||||
if self.action == 'destroy':
|
||||
return Collection.objects.filter(user_id=self.request.user.id)
|
||||
|
||||
if self.action in ['update', 'partial_update']:
|
||||
return Collection.objects.filter(user_id=self.request.user.id)
|
||||
|
||||
if self.action == 'retrieve':
|
||||
# For individual collection retrieval, include public collections
|
||||
adventures = Collection.objects.filter(
|
||||
return Collection.objects.filter(
|
||||
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(
|
||||
# Prefetch('adventure_set', queryset=Adventure.objects.filter(
|
||||
# Q(is_public=True) | Q(user_id=self.request.user.id)
|
||||
# ))
|
||||
# ).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)
|
||||
# For other actions (like list), only include user's non-archived collections
|
||||
return Collection.objects.filter(
|
||||
Q(user_id=self.request.user.id) & Q(is_archived=False)
|
||||
)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(user_id=self.request.user)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue