Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions document_merge_service/api/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,25 @@ def validate_template_syntax(self, available_placeholders=None, sample_data=None

try:
doc = DocxTemplate(self.template)
root = _MagicPlaceholder()
env = get_jinja_env()
ph = {
name: root[name] for name in doc.get_undeclared_template_variables(env)
}

xml = doc.get_xml()
xml = doc.patch_xml(xml)
image_match = re.match(r".*{{\s?(\S*)\s?\|\s?image\(.*", xml)
images = image_match.groups() if image_match else []
for image in images:
cleaned_image = image.strip('"').strip("'")
ph[root[cleaned_image]] = django_file("black.png").file

ph["_tpl"] = doc

doc.render(ph, env)

if sample_data:
sample_data["_tpl"] = doc
doc.render(sample_data, env)
if not sample_data:
# make our own sample data using magic placeholders
root = _MagicPlaceholder()
sample_data = {
name: root[name]
for name in doc.get_undeclared_template_variables(env)
}
xml = doc.get_xml()
xml = doc.patch_xml(xml)
image_match = re.match(r".*{{\s?(\S*)\s?\|\s?image\(.*", xml)
images = image_match.groups() if image_match else []
for image in images:
cleaned_image = image.strip('"').strip("'")
sample_data[root[cleaned_image]] = django_file("black.png").file

sample_data["_tpl"] = doc
doc.render(sample_data, env)

self.validate_available_placeholders(
used_placeholders=root.reports,
Expand Down