♻️(backend) api resources list ordering

Give the possibility to order the resources
list by creation date (documents / templates).
By default the list is ordered by
creation date descending.
This commit is contained in:
Anthony LC
2024-04-16 09:56:18 +02:00
committed by Anthony LC
parent db9c9ecfe3
commit f9705c6ce9
3 changed files with 117 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ from django.http import FileResponse
from rest_framework import (
decorators,
exceptions,
filters,
mixins,
pagination,
status,
@@ -138,6 +139,10 @@ class UserViewSet(
class ResourceViewsetMixin:
"""Mixin with methods common to all resource viewsets that are managed with accesses."""
filter_backends = [filters.OrderingFilter]
ordering_fields = ["created_at"]
ordering = ["-created_at"]
def get_queryset(self):
"""Custom queryset to get user related resources."""
queryset = super().get_queryset()

View File

@@ -164,3 +164,59 @@ def test_api_documents_list_authenticated_distinct():
content = response.json()
assert len(content["results"]) == 1
assert content["results"][0]["id"] == str(document.id)
def test_api_documents_order():
"""
Test that the endpoint GET documents is sorted in 'created_at' descending order by default.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)
document_ids = [
str(document.id)
for document in factories.DocumentFactory.create_batch(5, is_public=True)
]
response = client.get(
"/api/v1.0/documents/",
)
assert response.status_code == 200
response_data = response.json()
response_document_ids = [document["id"] for document in response_data["results"]]
document_ids.reverse()
assert (
response_document_ids == document_ids
), "created_at values are not sorted from newest to oldest"
def test_api_documents_order_param():
"""
Test that the 'created_at' field is sorted in ascending order
when the 'ordering' query parameter is set.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)
documents_ids = [
str(document.id)
for document in factories.DocumentFactory.create_batch(5, is_public=True)
]
response = APIClient().get(
"/api/v1.0/documents/?ordering=created_at",
)
assert response.status_code == 200
response_data = response.json()
response_document_ids = [document["id"] for document in response_data["results"]]
assert (
response_document_ids == documents_ids
), "created_at values are not sorted from oldest to newest"

View File

@@ -164,3 +164,59 @@ def test_api_templates_list_authenticated_distinct():
content = response.json()
assert len(content["results"]) == 1
assert content["results"][0]["id"] == str(template.id)
def test_api_templates_order():
"""
Test that the endpoint GET templates is sorted in 'created_at' descending order by default.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)
template_ids = [
str(template.id)
for template in factories.TemplateFactory.create_batch(5, is_public=True)
]
response = APIClient().get(
"/api/v1.0/templates/",
)
assert response.status_code == 200
response_data = response.json()
response_template_ids = [template["id"] for template in response_data["results"]]
template_ids.reverse()
assert (
response_template_ids == template_ids
), "created_at values are not sorted from newest to oldest"
def test_api_templates_order_param():
"""
Test that the 'created_at' field is sorted in ascending order
when the 'ordering' query parameter is set.
"""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)
templates_ids = [
str(template.id)
for template in factories.TemplateFactory.create_batch(5, is_public=True)
]
response = APIClient().get(
"/api/v1.0/templates/?ordering=created_at",
)
assert response.status_code == 200
response_data = response.json()
response_template_ids = [template["id"] for template in response_data["results"]]
assert (
response_template_ids == templates_ids
), "created_at values are not sorted from oldest to newest"