Fix: fix notes serializing in API document response (#9336)

This commit is contained in:
shamoon 2025-03-09 16:04:05 -07:00 committed by GitHub
parent 955ff32dcd
commit 4d15544a3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 30 deletions

View 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,

View 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",

View 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(
{