diff --git a/documents/api/docs.py b/documents/api/docs.py index d2c1034..55d3c3c 100644 --- a/documents/api/docs.py +++ b/documents/api/docs.py @@ -278,12 +278,16 @@ status_codes=[str(status.HTTP_200_OK), str(status.HTTP_201_CREATED)], value={ "id": "f6fe8acc-3b91-41b3-a176-9d2feab2d2bb", - "type": "APPLICATION_FOR_RESIDENTIAL_PARKING_PERMIT", - "human_readable_type": {"en": "Form", "fi": "Lomake"}, "created_at": "2022-03-07T16:08:39.580394+02:00", - "updated_at": "2022-03-07T17:59:39.580394+02:00", + "user_id": "g6fe8acc-3b91-41b3-a176-9d2feab2d2ba", "service": "Parking Permits", + "transaction_id": "TRANSACTION123", + "type": "APPLICATION_FOR_RESIDENTIAL_PARKING_PERMIT", + "human_readable_type": {"en": "Form", "fi": "Lomake"}, "status": "PROCESSING", + "deletable": True, + "attachments": ["attachment1.pdf", "attachment2.docs"], + "attachment_count": 2, }, ) diff --git a/documents/api/querysets.py b/documents/api/querysets.py index d7c9516..2e6871d 100644 --- a/documents/api/querysets.py +++ b/documents/api/querysets.py @@ -1,4 +1,4 @@ -from django.db.models import Count, Prefetch, QuerySet +from django.db.models import Prefetch, QuerySet from rest_framework.exceptions import PermissionDenied from services.enums import ServicePermissions @@ -28,7 +28,6 @@ def get_document_statistics_queryset(user: User, service: Service) -> QuerySet: queryset=Attachment.objects.only("document_id", "filename"), ) ) - .annotate(attachment_count=Count("attachments")) ).order_by("-created_at") if user.has_perm("users.view_document_statistics") or user.is_superuser: return qs diff --git a/documents/serializers/document.py b/documents/serializers/document.py index 4f75c46..5d092c6 100644 --- a/documents/serializers/document.py +++ b/documents/serializers/document.py @@ -46,8 +46,9 @@ class DocumentStatisticsSerializer(serializers.ModelSerializer): service = serializers.CharField( source="service.name", required=False, read_only=True ) - attachment_count = serializers.IntegerField() attachments = AttachmentNameSerializer(many=True) + # Attachment count included here just for clarity. Field is added to response body in to_representation + attachment_count = serializers.HiddenField(default=0) user_id = serializers.UUIDField(source="user.uuid", read_only=True) class Meta: @@ -62,10 +63,16 @@ class Meta: "human_readable_type", "status", "deletable", - "attachment_count", "attachments", + "attachment_count", ) + def to_representation(self, instance): + representation = super().to_representation(instance) + # Calculate attachment count here instead of aggregating it to queryset for performance reasons + representation["attachment_count"] = len(representation["attachments"]) + return representation + class GDPRDocumentSerializer(serializers.ModelSerializer): service = serializers.CharField( diff --git a/documents/serializers/status_history.py b/documents/serializers/status_history.py index cd63811..8b29b32 100644 --- a/documents/serializers/status_history.py +++ b/documents/serializers/status_history.py @@ -60,6 +60,9 @@ def create(self, validated_data): # Save document so updated_at field represents latest change's timestamp if document_changed: + if status_value: + document.status = status_value + document.status_display_values = status_display_values document.save() return status, document_changed diff --git a/documents/tests/test_api_patch_document.py b/documents/tests/test_api_patch_document.py index b08673d..ce08872 100644 --- a/documents/tests/test_api_patch_document.py +++ b/documents/tests/test_api_patch_document.py @@ -465,6 +465,9 @@ def test_user_create_status_and_activity(service, user): assert response.status_code == status.HTTP_201_CREATED assert StatusHistory.objects.count() == 1 document_id = response.json().get("id") + assert Document.objects.get(id=document_id).status == VALID_DOCUMENT_DATA.get( + "status" + ) status_activity_data = { "value": "newstatus", @@ -496,6 +499,7 @@ def test_user_create_status_and_activity(service, user): assert response.status_code == status.HTTP_201_CREATED assert StatusHistory.objects.count() == 2 assert Activity.objects.count() == 1 + assert Document.objects.get(id=document_id).status == "newstatus" def test_user_create_status(service, user): @@ -585,8 +589,8 @@ def test_create_new_activity(service_api_client): activity_data, format="json", ) - assert Activity.objects.count() == 1 assert response.status_code == status.HTTP_201_CREATED + assert Activity.objects.count() == 1 # OTHER STUFF