diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index a67673efc..c8a4a12c3 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -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, diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py index 7258b33d3..a0a380e41 100644 --- a/src/documents/tests/test_api_documents.py +++ b/src/documents/tests/test_api_documents.py @@ -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", diff --git a/src/documents/views.py b/src/documents/views.py index 1d4cb52dd..7298391f2 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -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( {