mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Use Django templating engine
This commit is contained in:
parent
538a4219bd
commit
f6a70b85f4
@ -229,21 +229,14 @@ class MailDocumentParser(DocumentParser):
|
||||
data["date"] = clean_html(mail.date.astimezone().strftime("%Y-%m-%d %H:%M"))
|
||||
data["content"] = clean_html(mail.text.strip())
|
||||
|
||||
html_file = os.path.join(os.path.dirname(__file__), "mail_template/index.html")
|
||||
placeholder_pattern = re.compile(r"{{(.+)}}")
|
||||
|
||||
html = StringIO()
|
||||
|
||||
with open(html_file) as html_template_handle:
|
||||
for line in html_template_handle.readlines():
|
||||
for placeholder in placeholder_pattern.findall(line):
|
||||
line = re.sub(
|
||||
"{{" + placeholder + "}}",
|
||||
data.get(placeholder.strip(), ""),
|
||||
line,
|
||||
)
|
||||
html.write(line)
|
||||
html.seek(0)
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
rendered = render_to_string("email_msg_template.html", context=data)
|
||||
|
||||
html.write(rendered)
|
||||
html.seek(0)
|
||||
|
||||
return html
|
||||
|
||||
@ -252,12 +245,12 @@ class MailDocumentParser(DocumentParser):
|
||||
url = self.gotenberg_server + "/forms/chromium/convert/html"
|
||||
self.log("info", "Converting mail to PDF")
|
||||
|
||||
css_file = os.path.join(os.path.dirname(__file__), "mail_template/output.css")
|
||||
css_file = os.path.join(os.path.dirname(__file__), "templates/output.css")
|
||||
|
||||
with open(css_file, "rb") as css_handle:
|
||||
|
||||
files = {
|
||||
"html": ("index.html", self.mail_to_html(mail)),
|
||||
"html": ("email_msg_template.html", self.mail_to_html(mail)),
|
||||
"css": ("output.css", css_handle),
|
||||
}
|
||||
headers = {}
|
||||
@ -302,7 +295,7 @@ class MailDocumentParser(DocumentParser):
|
||||
files.append((name_clean, BytesIO(a.payload)))
|
||||
html_clean = html_clean.replace(name_cid, name_clean)
|
||||
|
||||
files.append(("index.html", StringIO(html_clean)))
|
||||
files.append(("email_msg_template.html", StringIO(html_clean)))
|
||||
|
||||
return files
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
{% autoescape off %}
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
@ -43,3 +44,5 @@
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
{% endautoescape %}
|
@ -369,6 +369,7 @@ class TestParser(TestCase):
|
||||
os.path.join(self.SAMPLE_FILES, "html.eml.html"),
|
||||
) as html_expected_handle:
|
||||
html_expected = html_expected_handle.read()
|
||||
|
||||
self.assertHTMLEqual(html_expected, html_received)
|
||||
|
||||
@mock.patch("paperless_mail.parsers.requests.post")
|
||||
@ -436,7 +437,7 @@ class TestParser(TestCase):
|
||||
result = self.parser.transform_inline_html(html, attachments)
|
||||
|
||||
resulting_html = result[-1][1].read()
|
||||
self.assertTrue(result[-1][0] == "index.html")
|
||||
self.assertTrue(result[-1][0] == "email_msg_template.html")
|
||||
self.assertTrue(result[0][0] in resulting_html)
|
||||
self.assertFalse("<script" in resulting_html.lower())
|
||||
|
||||
@ -484,7 +485,7 @@ class TestParser(TestCase):
|
||||
mock_post.call_args.kwargs["files"]["cidpart1pNdUSz0sD3NqVtPgexamplede"][
|
||||
1
|
||||
].read()
|
||||
mock_post.call_args.kwargs["files"]["index.html"][1].read()
|
||||
mock_post.call_args.kwargs["files"]["email_msg_template.html"][1].read()
|
||||
|
||||
mock_response.raise_for_status.assert_called_once()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user