Initial commit

This commit is contained in:
2024-08-27 20:33:44 +02:00
commit 1f1832267d
14794 changed files with 1599592 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
from typing import List
from django.utils.html import escape
from wagtail.documents import get_document_model
from wagtail.rich_text import LinkHandler
# Front-end conversion
class DocumentLinkHandler(LinkHandler):
identifier = "document"
@staticmethod
def get_model():
return get_document_model()
@classmethod
def expand_db_attributes(cls, attrs: dict) -> str:
return cls.expand_db_attributes_many([attrs])[0]
@classmethod
def expand_db_attributes_many(cls, attrs_list: List[dict]) -> List[str]:
return [
'<a href="%s">' % escape(doc.url) if doc else "<a>"
for doc in cls.get_many(attrs_list)
]
@classmethod
def extract_references(cls, attrs):
# Yields tuples of (content_type_id, object_id, model_path, content_path)
yield cls.get_model(), attrs["id"], "", ""

View File

@@ -0,0 +1,57 @@
"""
Draftail / contentstate conversion
"""
from draftjs_exporter.dom import DOM
from wagtail.admin.rich_text.converters.html_to_contentstate import LinkElementHandler
from wagtail.documents import get_document_model
def document_link_entity(props):
"""
Helper to construct elements of the form
<a id="1" linktype="document">document link</a>
when converting from contentstate data
"""
return DOM.create_element(
"a",
{
"linktype": "document",
"id": props.get("id"),
},
props["children"],
)
class DocumentLinkElementHandler(LinkElementHandler):
"""
Rule for populating the attributes of a document link when converting from database representation
to contentstate
"""
def get_attribute_data(self, attrs):
Document = get_document_model()
try:
id = int(attrs["id"])
except (KeyError, ValueError):
return {}
try:
doc = Document.objects.get(id=id)
except Document.DoesNotExist:
return {"id": id}
return {
"id": doc.id,
"url": doc.url,
"filename": doc.filename,
}
ContentstateDocumentLinkConversionRule = {
"from_database_format": {
'a[linktype="document"]': DocumentLinkElementHandler("DOCUMENT"),
},
"to_database_format": {"entity_decorators": {"DOCUMENT": document_link_entity}},
}

View File

@@ -0,0 +1,34 @@
"""
editor-html conversion for contenteditable editors
"""
from django.utils.html import escape
from wagtail.admin.rich_text.converters import editor_html
from wagtail.documents import get_document_model
class DocumentLinkHandler:
@staticmethod
def get_db_attributes(tag):
return {"id": tag["data-id"]}
@staticmethod
def expand_db_attributes(attrs):
Document = get_document_model()
try:
doc = Document.objects.get(id=attrs["id"])
return '<a data-linktype="document" data-id="%d" href="%s">' % (
doc.id,
escape(doc.url),
)
except Document.DoesNotExist:
# Preserve the ID attribute for troubleshooting purposes, even though it
# points to a missing document
return '<a data-linktype="document" data-id="%s">' % attrs["id"]
except KeyError:
return '<a data-linktype="document">'
EditorHTMLDocumentLinkConversionRule = [
editor_html.LinkTypeRule("document", DocumentLinkHandler),
]