]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Fix: fix notes serializing in API document response (#9336)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Sun, 9 Mar 2025 23:04:05 +0000 (16:04 -0700)
committerGitHub <noreply@github.com>
Sun, 9 Mar 2025 23:04:05 +0000 (23:04 +0000)
src/documents/serialisers.py
src/documents/tests/test_api_documents.py
src/documents/views.py

index a67673efc076bd63c54fb57aeac8830b269d1c3e..c8a4a12c34aa2b3c6342a16f85d91258d78c0977 100644 (file)
@@ -43,6 +43,7 @@ from documents.models import CustomFieldInstance
 from documents.models import Document
 from documents.models import DocumentType
 from documents.models import MatchingModel
+from documents.models import Note
 from documents.models import PaperlessTask
 from documents.models import SavedView
 from documents.models import SavedViewFilterRule
@@ -861,6 +862,22 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
         ]
 
 
+class BasicUserSerializer(serializers.ModelSerializer):
+    # Different than paperless.serializers.UserSerializer
+    class Meta:
+        model = User
+        fields = ["id", "username", "first_name", "last_name"]
+
+
+class NotesSerializer(serializers.ModelSerializer):
+    user = BasicUserSerializer()
+
+    class Meta:
+        model = Note
+        fields = ["id", "note", "created", "user"]
+        ordering = ["-created"]
+
+
 class DocumentSerializer(
     OwnedObjectSerializer,
     NestedUpdateMixin,
@@ -876,6 +893,8 @@ class DocumentSerializer(
     created_date = serializers.DateField(required=False)
     page_count = SerializerMethodField()
 
+    notes = NotesSerializer(many=True, required=False)
+
     custom_fields = CustomFieldInstanceSerializer(
         many=True,
         allow_null=False,
index 7258b33d35693c9dd201d3cb065c44131fb29434..a0a380e41fe2fc9b94b64903529045d746897728 100644 (file)
@@ -2170,8 +2170,10 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
         GIVEN:
             - A document with a single note
         WHEN:
+            - API request for document
             - API request for document notes is made
         THEN:
+            - Note is included in the document response
             - The associated note is returned
         """
         doc = Document.objects.create(
@@ -2185,6 +2187,18 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
             user=self.user,
         )
 
+        response = self.client.get(
+            f"/api/documents/{doc.pk}/",
+            format="json",
+        )
+
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+        resp_data = response.json()
+        self.assertEqual(len(resp_data["notes"]), 1)
+        self.assertEqual(resp_data["notes"][0]["note"], note.note)
+        self.assertEqual(resp_data["notes"][0]["user"]["username"], self.user.username)
+
         response = self.client.get(
             f"/api/documents/{doc.pk}/notes/",
             format="json",
index 1d4cb52dd13edd872e4b0b615f477bf965632003..7298391f28416bc75099dfdfa9e845e9ea859fb4 100644 (file)
@@ -803,33 +803,6 @@ class DocumentViewSet(
         except (FileNotFoundError, Document.DoesNotExist):
             raise Http404
 
-    def getNotes(self, doc):
-        return [
-            {
-                "id": c.pk,
-                "note": c.note,
-                "created": c.created,
-                "user": {
-                    "id": c.user.id,
-                    "username": c.user.username,
-                    "first_name": c.user.first_name,
-                    "last_name": c.user.last_name,
-                },
-            }
-            for c in Note.objects.select_related("user")
-            .only(
-                "pk",
-                "note",
-                "created",
-                "user__id",
-                "user__username",
-                "user__first_name",
-                "user__last_name",
-            )
-            .filter(document=doc)
-            .order_by("-created")
-        ]
-
     @action(
         methods=["get", "post", "delete"],
         detail=True,
@@ -854,9 +827,11 @@ class DocumentViewSet(
         except Document.DoesNotExist:
             raise Http404
 
+        serializer = self.get_serializer(doc)
+
         if request.method == "GET":
             try:
-                notes = self.getNotes(doc)
+                notes = serializer.to_representation(doc).get("notes")
                 return Response(notes)
             except Exception as e:
                 logger.warning(f"An error occurred retrieving notes: {e!s}")
@@ -897,7 +872,7 @@ class DocumentViewSet(
 
                 index.add_or_update_document(doc)
 
-                notes = self.getNotes(doc)
+                notes = serializer.to_representation(doc).get("notes")
 
                 return Response(notes)
             except Exception as e:
@@ -934,7 +909,9 @@ class DocumentViewSet(
 
             index.add_or_update_document(doc)
 
-            return Response(self.getNotes(doc))
+            notes = serializer.to_representation(doc).get("notes")
+
+            return Response(notes)
 
         return Response(
             {