Skip to content

Commit

Permalink
Add archived feature to todo_api for lists
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasvallejosdev committed May 30, 2024
1 parent 524e059 commit d5cfdb3
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
36 changes: 36 additions & 0 deletions tests/todo_api/test_list_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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"))
Expand Down Expand Up @@ -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
18 changes: 18 additions & 0 deletions todo_api/migrations/0002_tasklist_archived.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
1 change: 1 addition & 0 deletions todo_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions todo_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Meta:
"list_uuid",
"name",
"created_by",
"archived",
)
read_only_fields = ("id", "list_uuid")

Expand Down
10 changes: 8 additions & 2 deletions todo_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit d5cfdb3

Please sign in to comment.