mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-19 20:25:21 +00:00
Enhancement: render document notes as markdown (#14626)
--------- Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
This commit is contained in:
1 parent
bfd5628dcc
commit
b63ea77543
13 files changed
+270
-30
No files matched your search
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 5.2.17 on 2026-11-03 05:57
|
||||
|
||||
from django.db import migrations
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("documents", "0029_alter_workflowrun_type_alter_workflowtrigger_type"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="note",
|
||||
name="format",
|
||||
field=models.CharField(
|
||||
choices=[("plain", "Plain"), ("markdown", "Markdown")],
|
||||
default="plain",
|
||||
max_length=16,
|
||||
verbose_name="format",
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -1006,12 +1006,23 @@ class PaperlessTask(ModelWithOwner):
|
||||
|
||||
|
||||
class Note(SoftDeleteModel):
|
||||
class Format(models.TextChoices):
|
||||
PLAIN = ("plain", _("Plain"))
|
||||
MARKDOWN = ("markdown", _("Markdown"))
|
||||
|
||||
note = models.TextField(
|
||||
_("content"),
|
||||
blank=True,
|
||||
help_text=_("Note for the document"),
|
||||
)
|
||||
|
||||
format = models.CharField(
|
||||
max_length=16,
|
||||
choices=Format,
|
||||
default=Format.PLAIN,
|
||||
verbose_name=_("format"),
|
||||
)
|
||||
|
||||
created = models.DateTimeField(
|
||||
_("created"),
|
||||
default=timezone.now,
|
||||
|
||||
@@ -971,10 +971,19 @@ class NotesSerializer(serializers.ModelSerializer[Note]):
|
||||
|
||||
class Meta:
|
||||
model = Note
|
||||
fields = ["id", "note", "created", "user"]
|
||||
fields = ["id", "note", "format", "created", "user"]
|
||||
ordering = ["-created"]
|
||||
|
||||
|
||||
class NoteCreateSerializer(serializers.Serializer[dict[str, Any]]):
|
||||
note = serializers.CharField()
|
||||
format = serializers.ChoiceField(
|
||||
choices=Note.Format.choices,
|
||||
default=Note.Format.PLAIN,
|
||||
required=False,
|
||||
)
|
||||
|
||||
|
||||
def _get_viewable_duplicates(
|
||||
document: Document,
|
||||
user: User | None,
|
||||
|
||||
@@ -3689,6 +3689,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase):
|
||||
{
|
||||
"id": note.id,
|
||||
"note": note.note,
|
||||
"format": Note.Format.PLAIN,
|
||||
"user": {
|
||||
"id": note.user.id,
|
||||
"username": note.user.username,
|
||||
@@ -3737,11 +3738,64 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase):
|
||||
resp_data = resp_data[0]
|
||||
|
||||
self.assertEqual(resp_data["note"], "this is a posted note")
|
||||
self.assertEqual(resp_data["format"], Note.Format.PLAIN)
|
||||
|
||||
doc = Document.objects.get(pk=doc.pk)
|
||||
# modified was updated to today
|
||||
self.assertEqual(doc.modified.day, timezone.now().day)
|
||||
|
||||
def test_create_markdown_note(self) -> None:
|
||||
doc = DocumentFactory(
|
||||
title="test",
|
||||
content="this is a document which will have a markdown note",
|
||||
)
|
||||
|
||||
response = self.client.post(
|
||||
f"/api/documents/{doc.pk}/notes/",
|
||||
data={"note": "**markdown**", "format": Note.Format.MARKDOWN},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
note = Note.objects.get(document=doc)
|
||||
self.assertEqual(note.format, Note.Format.MARKDOWN)
|
||||
|
||||
response = self.client.get(f"/api/documents/{doc.pk}/notes/", format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.json()[0]["format"], Note.Format.MARKDOWN)
|
||||
|
||||
def test_create_note_rejects_unsupported_format(self) -> None:
|
||||
doc = DocumentFactory(
|
||||
title="test",
|
||||
content="this is a document which will have a note",
|
||||
)
|
||||
|
||||
response = self.client.post(
|
||||
f"/api/documents/{doc.pk}/notes/",
|
||||
data={"note": "unsupported", "format": "html"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn("format", response.json())
|
||||
self.assertFalse(Note.objects.filter(document=doc).exists())
|
||||
|
||||
def test_create_note_requires_note(self) -> None:
|
||||
doc = DocumentFactory(
|
||||
title="test",
|
||||
content="this is a document which will have a note",
|
||||
)
|
||||
|
||||
response = self.client.post(
|
||||
f"/api/documents/{doc.pk}/notes/",
|
||||
data={"format": Note.Format.MARKDOWN},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertIn("note", response.json())
|
||||
self.assertFalse(Note.objects.filter(document=doc).exists())
|
||||
|
||||
def test_create_note_only_saves_document_modified_field(self) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
|
||||
+16
-16
@@ -207,6 +207,7 @@ from documents.serialisers import EditPdfDocumentsSerializer
|
||||
from documents.serialisers import EmailSerializer
|
||||
from documents.serialisers import MergeDocumentsAsVersionsSerializer
|
||||
from documents.serialisers import MergeDocumentsSerializer
|
||||
from documents.serialisers import NoteCreateSerializer
|
||||
from documents.serialisers import NotesSerializer
|
||||
from documents.serialisers import PostDocumentSerializer
|
||||
from documents.serialisers import RemovePasswordDocumentsSerializer
|
||||
@@ -833,12 +834,7 @@ class EmailDocumentDetailSchema(EmailSerializer):
|
||||
notes=extend_schema(
|
||||
description="View, add, or delete notes for the document",
|
||||
methods=["GET", "POST", "DELETE"],
|
||||
request=inline_serializer(
|
||||
name="NoteCreateRequest",
|
||||
fields={
|
||||
"note": serializers.CharField(),
|
||||
},
|
||||
),
|
||||
request=NoteCreateSerializer,
|
||||
parameters=[
|
||||
OpenApiParameter(
|
||||
name="id",
|
||||
@@ -1896,19 +1892,23 @@ class DocumentViewSet(
|
||||
{"error": "Error retrieving notes, check logs for more detail."},
|
||||
)
|
||||
elif request.method == "POST":
|
||||
try:
|
||||
if currentUser is not None and not has_perms_owner_aware(
|
||||
currentUser,
|
||||
"change_document",
|
||||
doc,
|
||||
):
|
||||
return HttpResponseForbidden(
|
||||
"Insufficient permissions to create notes",
|
||||
)
|
||||
if currentUser is not None and not has_perms_owner_aware(
|
||||
currentUser,
|
||||
"change_document",
|
||||
doc,
|
||||
):
|
||||
return HttpResponseForbidden(
|
||||
"Insufficient permissions to create notes",
|
||||
)
|
||||
|
||||
note_serializer = NoteCreateSerializer(data=request.data)
|
||||
note_serializer.is_valid(raise_exception=True)
|
||||
|
||||
try:
|
||||
c = Note.objects.create(
|
||||
document=doc,
|
||||
note=request.data["note"],
|
||||
note=note_serializer.validated_data["note"],
|
||||
format=note_serializer.validated_data["format"],
|
||||
user=currentUser,
|
||||
)
|
||||
# If audit log is enabled make an entry in the log
|
||||
|
||||
Reference in new issue
Block a user