Skip to content

Commit

Permalink
Merge pull request #79 from City-of-Helsinki/feature-bug-fixes-and-im…
Browse files Browse the repository at this point in the history
…provements

Feature bug fixes and improvements
  • Loading branch information
AntiAki-M authored Jun 20, 2023
2 parents bf2f613 + f053c23 commit 9c25c6e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
10 changes: 7 additions & 3 deletions documents/api/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)

Expand Down
3 changes: 1 addition & 2 deletions documents/api/querysets.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions documents/serializers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions documents/serializers/status_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion documents/tests/test_api_patch_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9c25c6e

Please sign in to comment.