🏷️(backend) add body type on generate-document endpoint

We were converting from markdown to html, but the
frontend can provide the body in html format, so
wa can avoid the conversion.

Solution:
Add body type on generate-document endpoint
to allow to choose between markdown and html.
This commit is contained in:
Anthony LC
2024-04-16 10:30:10 +02:00
committed by Anthony LC
parent f9705c6ce9
commit 1df7c43dd3
4 changed files with 83 additions and 8 deletions

View File

@@ -153,4 +153,10 @@ class TemplateSerializer(BaseResourceSerializer):
class DocumentGenerationSerializer(serializers.Serializer):
"""Serializer to receive a request to generate a document on a template."""
body = serializers.CharField(label=_("Markdown Body"))
body = serializers.CharField(label=_("Body"))
body_type = serializers.ChoiceField(
choices=["html", "markdown"],
label=_("Body type"),
required=False,
default="html",
)

View File

@@ -372,9 +372,10 @@ class TemplateViewSet(
)
body = serializer.validated_data["body"]
body_type = serializer.validated_data["body_type"]
template = self.get_object()
pdf_content = template.generate_document(body)
pdf_content = template.generate_document(body, body_type)
response = FileResponse(BytesIO(pdf_content), content_type="application/pdf")
response["Content-Disposition"] = f"attachment; filename={template.title}.pdf"

View File

@@ -361,17 +361,21 @@ class Template(BaseModel):
"retrieve": can_get,
}
def generate_document(self, body):
def generate_document(self, body, body_type):
"""
Generate and return a PDF document for this template around the
markdown body passed as argument.
body passed as argument.
"""
document = frontmatter.loads(body)
metadata = document.metadata
markdown_body = document.content.strip()
body_html = (
markdown.markdown(textwrap.dedent(markdown_body)) if markdown_body else ""
)
strip_body = document.content.strip()
if body_type == "html":
body_html = strip_body
else:
body_html = (
markdown.markdown(textwrap.dedent(strip_body)) if strip_body else ""
)
document_html = HTML(
string=DjangoTemplate(self.code).render(

View File

@@ -114,3 +114,67 @@ def test_api_templates_generate_document_related(via, mock_user_get_teams):
assert response.status_code == 200
assert response.headers["content-type"] == "application/pdf"
def test_api_templates_generate_document_type_html():
"""Generate pdf document with the body type html."""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)
template = factories.TemplateFactory(is_public=True)
data = {"body": "<p>Test body</p>", "body_type": "html"}
response = client.post(
f"/api/v1.0/templates/{template.id!s}/generate-document/",
data,
format="json",
)
assert response.status_code == 200
assert response.headers["content-type"] == "application/pdf"
def test_api_templates_generate_document_type_markdown():
"""Generate pdf document with the body type markdown."""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)
template = factories.TemplateFactory(is_public=True)
data = {"body": "# Test markdown body", "body_type": "markdown"}
response = client.post(
f"/api/v1.0/templates/{template.id!s}/generate-document/",
data,
format="json",
)
assert response.status_code == 200
assert response.headers["content-type"] == "application/pdf"
def test_api_templates_generate_document_type_unknown():
"""Generate pdf document with the body type unknown."""
user = factories.UserFactory()
client = APIClient()
client.force_login(user)
template = factories.TemplateFactory(is_public=True)
data = {"body": "# Test markdown body", "body_type": "unknown"}
response = client.post(
f"/api/v1.0/templates/{template.id!s}/generate-document/",
data,
format="json",
)
assert response.status_code == 400
assert response.json() == {
"body_type": [
'"unknown" is not a valid choice.',
]
}