mirror of
https://github.com/sartography/cr-connect-workflow.git
synced 2025-02-20 11:48:16 +00:00
Assure that new lines entered in text-fields are correctly added to the final word document.
This commit is contained in:
parent
5f2331f081
commit
5c5c2a7312
@ -1,14 +1,13 @@
|
||||
from io import StringIO, BytesIO
|
||||
import copy
|
||||
from io import BytesIO
|
||||
|
||||
from jinja2 import UndefinedError
|
||||
import jinja2
|
||||
from docxtpl import DocxTemplate, Listing
|
||||
|
||||
from crc import session
|
||||
from crc.api.common import ApiError
|
||||
from crc.models.file import FileModel, FileDataModel, CONTENT_TYPES
|
||||
from crc.models.workflow import WorkflowSpecModel, WorkflowModel
|
||||
from docxtpl import DocxTemplate
|
||||
import jinja2
|
||||
|
||||
from crc.models.file import CONTENT_TYPES
|
||||
from crc.models.workflow import WorkflowModel
|
||||
from crc.scripts.script import Script
|
||||
from crc.services.file_service import FileService
|
||||
from crc.services.workflow_processor import WorkflowProcessor
|
||||
@ -66,11 +65,30 @@ Takes two arguments:
|
||||
|
||||
def make_template(self, binary_stream, context):
|
||||
doc = DocxTemplate(binary_stream)
|
||||
doc_context = copy.deepcopy(context)
|
||||
doc_context = self.rich_text_update(doc_context)
|
||||
jinja_env = jinja2.Environment(autoescape=True)
|
||||
doc.render(context, jinja_env)
|
||||
doc.render(doc_context, jinja_env)
|
||||
target_stream = BytesIO()
|
||||
doc.save(target_stream)
|
||||
target_stream.seek(0) # move to the beginning of the stream.
|
||||
return target_stream
|
||||
|
||||
|
||||
def rich_text_update(self, context):
|
||||
"""This is a bit of a hack. If we find that /n characters exist in the data, we want
|
||||
these to come out in the final document without requiring someone to predict it in the
|
||||
template. Ideally we would use the 'RichText' feature of the python-docx library, but
|
||||
that requires we both escape it here, and in the Docx template. There is a thing called
|
||||
a 'listing' in python-docx library that only requires we use it on the way in, and the
|
||||
template doesn't have to think about it. So running with that for now."""
|
||||
# loop through the content, identify anything that has a newline character in it, and
|
||||
# wrap that sucker in a 'listing' function.
|
||||
if isinstance(context, dict):
|
||||
for k, v in context.items():
|
||||
context[k] = self.rich_text_update(v)
|
||||
elif isinstance(context, list):
|
||||
for i in range(len(context)):
|
||||
context[i] = self.rich_text_update(context[i])
|
||||
elif isinstance(context, str) and '\n' in context:
|
||||
return Listing(context)
|
||||
return context
|
||||
|
36
tests/test_complete_template_script.py
Normal file
36
tests/test_complete_template_script.py
Normal file
@ -0,0 +1,36 @@
|
||||
import json
|
||||
import os
|
||||
import unittest
|
||||
import copy
|
||||
|
||||
from docxtpl import Listing
|
||||
|
||||
from crc import app
|
||||
from crc.scripts.complete_template import CompleteTemplate
|
||||
from tests.base_test import BaseTest
|
||||
|
||||
|
||||
class TestCompleteTemplate(unittest.TestCase):
|
||||
|
||||
def test_rich_text_update(self):
|
||||
script = CompleteTemplate()
|
||||
data = {"name": "Dan"}
|
||||
data_copy = copy.deepcopy(data)
|
||||
script.rich_text_update(data_copy)
|
||||
self.assertEquals(data, data_copy)
|
||||
|
||||
def test_rich_text_update_new_line(self):
|
||||
script = CompleteTemplate()
|
||||
data = {"name": "Dan\n Funk"}
|
||||
data_copy = copy.deepcopy(data)
|
||||
script.rich_text_update(data_copy)
|
||||
self.assertNotEqual(data, data_copy)
|
||||
self.assertIsInstance(data_copy["name"], Listing)
|
||||
|
||||
def test_rich_text_nested_new_line(self):
|
||||
script = CompleteTemplate()
|
||||
data = {"names": [{"name": "Dan\n Funk"}]}
|
||||
data_copy = copy.deepcopy(data)
|
||||
script.rich_text_update(data_copy)
|
||||
self.assertNotEqual(data, data_copy)
|
||||
self.assertIsInstance(data_copy["names"][0]["name"], Listing)
|
Loading…
x
Reference in New Issue
Block a user