Enhancement: only include correspondent 'last_correspondence' if requested (#6792)

This commit is contained in:
shamoon
2024-05-22 16:15:58 -07:00
committed by GitHub
parent c0c44b512c
commit 8abb0cd75d
9 changed files with 131 additions and 49 deletions

View File

@@ -291,7 +291,7 @@ class OwnedObjectSerializer(
class CorrespondentSerializer(MatchingModelSerializer, OwnedObjectSerializer):
last_correspondence = serializers.DateTimeField(read_only=True)
last_correspondence = serializers.DateTimeField(read_only=True, required=False)
class Meta:
model = Correspondent

View File

@@ -1,8 +1,10 @@
import datetime
import json
from unittest import mock
from django.contrib.auth.models import Permission
from django.contrib.auth.models import User
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APITestCase
@@ -89,6 +91,57 @@ class TestApiObjects(DirectoriesMixin, APITestCase):
results = response.data["results"]
self.assertEqual(len(results), 2)
def test_correspondent_last_correspondence(self):
"""
GIVEN:
- Correspondent with documents
WHEN:
- API is called
THEN:
- Last correspondence date is returned only if requested for list, and for detail
"""
Document.objects.create(
mime_type="application/pdf",
correspondent=self.c1,
created=timezone.make_aware(datetime.datetime(2022, 1, 1)),
checksum="123",
)
Document.objects.create(
mime_type="application/pdf",
correspondent=self.c1,
created=timezone.make_aware(datetime.datetime(2022, 1, 2)),
checksum="456",
)
# Only if requested for list
response = self.client.get(
"/api/correspondents/",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
results = response.data["results"]
self.assertNotIn("last_correspondence", results[0])
response = self.client.get(
"/api/correspondents/?last_correspondence=true",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
results = response.data["results"]
self.assertIn(
"2022-01-02",
results[0]["last_correspondence"],
)
# Included in detail by default
response = self.client.get(
f"/api/correspondents/{self.c1.id}/",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn(
"2022-01-02",
response.data["last_correspondence"],
)
class TestApiStoragePaths(DirectoriesMixin, APITestCase):
ENDPOINT = "/api/storage_paths/"

View File

@@ -253,14 +253,7 @@ class PermissionsAwareDocumentCountMixin(PassUserMixin):
class CorrespondentViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):
model = Correspondent
queryset = (
Correspondent.objects.prefetch_related("documents")
.annotate(
last_correspondence=Max("documents__created"),
)
.select_related("owner")
.order_by(Lower("name"))
)
queryset = Correspondent.objects.select_related("owner").order_by(Lower("name"))
serializer_class = CorrespondentSerializer
pagination_class = StandardPagination
@@ -279,6 +272,19 @@ class CorrespondentViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):
"last_correspondence",
)
def list(self, request, *args, **kwargs):
if request.query_params.get("last_correspondence", None):
self.queryset = self.queryset.annotate(
last_correspondence=Max("documents__created"),
)
return super().list(request, *args, **kwargs)
def retrieve(self, request, *args, **kwargs):
self.queryset = self.queryset.annotate(
last_correspondence=Max("documents__created"),
)
return super().retrieve(request, *args, **kwargs)
class TagViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin):
model = Tag