From d5cfdb37dd6dcd46c20c251e14a1b193d44fa877 Mon Sep 17 00:00:00 2001 From: Matias Vallejos Date: Thu, 30 May 2024 11:03:12 -0300 Subject: [PATCH] Add archived feature to todo_api for lists --- tests/todo_api/test_list_api.py | 36 +++++++++++++++++++ todo_api/migrations/0002_tasklist_archived.py | 18 ++++++++++ todo_api/models.py | 1 + todo_api/serializers.py | 1 + todo_api/views.py | 10 ++++-- 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 todo_api/migrations/0002_tasklist_archived.py diff --git a/tests/todo_api/test_list_api.py b/tests/todo_api/test_list_api.py index 8576d9f..960f99e 100644 --- a/tests/todo_api/test_list_api.py +++ b/tests/todo_api/test_list_api.py @@ -35,6 +35,23 @@ def test_list_task_lists_limited_to_user(api_client_with_credentials, user2): assert len(res.data) == 0 +def test_list_task_lists_not_archived(api_client_with_credentials, user): + baker.make(TaskList, created_by=user, _quantity=5) + baker.make(TaskList, created_by=user, archived=True, _quantity=5) + res = api_client_with_credentials.get(LISTS_URL) + assert res.status_code == status.HTTP_200_OK + assert len(res.data) == 5 + + +def test_list_task_lists_archived(api_client_with_credentials, user): + baker.make(TaskList, created_by=user, _quantity=5) + baker.make(TaskList, created_by=user, archived=True, _quantity=5) + params = {"archived": "true"} + res = api_client_with_credentials.get(LISTS_URL, params) + assert res.status_code == status.HTTP_200_OK + assert len(res.data) == 5 + + def test_retrieve_list(api_client_with_credentials, user): list = baker.make(TaskList, created_by=user) res = api_client_with_credentials.get(tasks_detail_url(list.list_uuid)) @@ -43,6 +60,12 @@ def test_retrieve_list(api_client_with_credentials, user): assert res.data["name"] == list.name +def test_retrieve_list_archived(api_client_with_credentials, user): + list = baker.make(TaskList, created_by=user, archived=True) + res = api_client_with_credentials.get(tasks_detail_url(list.list_uuid)) + assert res.status_code == status.HTTP_404_NOT_FOUND + + def test_retrieve_list_not_found(api_client_with_credentials, user): baker.make(TaskList, created_by=user) res = api_client_with_credentials.get(tasks_detail_url("not-found")) @@ -125,3 +148,16 @@ def test_delete_list_limited_to_user(api_client_with_credentials, user2): res = api_client_with_credentials.delete(tasks_detail_url(list.list_uuid)) assert res.status_code == status.HTTP_404_NOT_FOUND assert TaskList.objects.count() == 1 + + +def test_archive_list(api_client_with_credentials, user): + list = baker.make(TaskList, created_by=user) + payload = {"archived": True} + res = api_client_with_credentials.patch( + tasks_detail_url(list.list_uuid), + payload, + ) + assert res.status_code == status.HTTP_200_OK + + res = api_client_with_credentials.get(tasks_detail_url(list.list_uuid)) + assert res.status_code == status.HTTP_404_NOT_FOUND diff --git a/todo_api/migrations/0002_tasklist_archived.py b/todo_api/migrations/0002_tasklist_archived.py new file mode 100644 index 0000000..6b170cf --- /dev/null +++ b/todo_api/migrations/0002_tasklist_archived.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0 on 2024-05-29 16:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo_api', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='tasklist', + name='archived', + field=models.BooleanField(default=False), + ), + ] diff --git a/todo_api/models.py b/todo_api/models.py index 02b1902..4da820a 100644 --- a/todo_api/models.py +++ b/todo_api/models.py @@ -35,6 +35,7 @@ class TaskList(models.Model): created_by = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=False, null=True ) + archived = models.BooleanField(default=False) def save(self, *args, **kwargs): super().save(*args, **kwargs) diff --git a/todo_api/serializers.py b/todo_api/serializers.py index ca85568..6e6590e 100644 --- a/todo_api/serializers.py +++ b/todo_api/serializers.py @@ -15,6 +15,7 @@ class Meta: "list_uuid", "name", "created_by", + "archived", ) read_only_fields = ("id", "list_uuid") diff --git a/todo_api/views.py b/todo_api/views.py index d9476e3..2b713be 100644 --- a/todo_api/views.py +++ b/todo_api/views.py @@ -38,7 +38,7 @@ def get_queryset(self): # Guard clause for empty task_list if not task_list: - return queryset.order_by("created_at").distinct() + return queryset.order_by("due_date").distinct() # Define filter mappings for task_list values filter_mapping = { @@ -62,7 +62,7 @@ def get_queryset(self): get_object_or_404(TaskList, list_uuid=task_list_uuid) queryset = queryset.filter(task_list__list_uuid=task_list_uuid) - return queryset.order_by("created_at").distinct() + return queryset.order_by("due_date").distinct() def list(self, request, *args, **kwargs): return super().list(request, *args, **kwargs) @@ -114,6 +114,12 @@ class TaskListViewSet(viewsets.ModelViewSet): def get_queryset(self): queryset = self.queryset.filter(created_by=self.request.user) + queryparams = self.request.query_params + archived = queryparams.get("archived", None) + if archived is not None: + queryset = queryset.filter(archived=True) + else: + queryset = queryset.filter(archived=False) return queryset.order_by("created_at").distinct() def perform_create(self, serializer):