*** WIP ***

committing changes to work on another ticket.
Still need to figure out the complete_template piece
This commit is contained in:
mike cullerton 2021-10-20 09:21:45 -04:00
parent 4efdef3d77
commit e707783cea
2 changed files with 62 additions and 20 deletions

View File

@ -111,24 +111,24 @@ Takes two arguments:
def make_template(self, binary_stream, task_data, image_file_data=None):
# TODO: Move this into the jinja_service?
doc = DocxTemplate(binary_stream)
doc_context = copy.deepcopy(task_data)
doc_context = self.rich_text_update(doc_context)
doc_context = self.append_images(doc, doc_context, image_file_data)
jinja_env = jinja2.Environment(autoescape=True)
try:
doc.render(doc_context, jinja_env)
except Exception as e:
print (e)
# doc = DocxTemplate(binary_stream)
# doc_context = copy.deepcopy(task_data)
# doc_context = self.rich_text_update(doc_context)
# doc_context = self.append_images(doc, doc_context, image_file_data)
# jinja_env = jinja2.Environment(autoescape=True)
# try:
# doc = JinjaService.get_word_document_content(binary_stream, task_data, image_file_data)
# except jinja2.exceptions.TemplateError as te:
# # raise ApiError.from_task(code="bad_template",
# # message="There was a problem compiling your template.",
# # task=self.task)
# print(te)
# except TypeError as te:
# print(te)
# doc.render(doc_context, jinja_env)
# except Exception as e:
# print (e)
try:
doc = JinjaService.get_word_document_content(binary_stream, task_data, image_file_data)
except jinja2.exceptions.TemplateError as te:
# raise ApiError.from_task(code="bad_template",
# message="There was a problem compiling your template.",
# task=self.task)
print(te)
except TypeError as te:
print(te)
target_stream = BytesIO()
doc.save(target_stream)
target_stream.seek(0) # move to the beginning of the stream.

View File

@ -5,6 +5,7 @@ import os
import copy
from docxtpl import Listing
from io import BytesIO
from crc import app
from crc.scripts.complete_template import CompleteTemplate
@ -14,14 +15,14 @@ from crc.services.jinja_service import JinjaService
class TestCompleteTemplate(unittest.TestCase):
def test_rich_text_update(self):
script = JinjaService()
script = CompleteTemplate()
data = {"name": "Dan"}
data_copy = copy.deepcopy(data)
script.rich_text_update(data_copy)
self.assertEqual(data, data_copy)
def test_rich_text_update_new_line(self):
script = JinjaService()
script = CompleteTemplate()
data = {"name": "Dan\n Funk"}
data_copy = copy.deepcopy(data)
script.rich_text_update(data_copy)
@ -29,9 +30,50 @@ class TestCompleteTemplate(unittest.TestCase):
self.assertIsInstance(data_copy["name"], Listing)
def test_rich_text_nested_new_line(self):
script = JinjaService()
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)
class TestCallingScript(BaseTest):
def test_calling_script(self):
workflow = self.create_workflow('docx')
workflow_api = self.get_workflow_api(workflow)
task = workflow_api.next_task
filepath = os.path.join(app.root_path, '..', 'tests', 'data', 'docx', 'Letter.docx')
with open(filepath, 'rb') as f:
file_data = {'file': (f, 'Letter.docx')}
template_data = {'fullname',
'date',
'title',
'company',
'lastname'}
data = {'file': (BytesIO(b"abcdef"), 'Letter.docx')}
FileService.add_workflow_file(workflow_id=workflow_id,
task_spec_name=task.get_name(),
name=file_name,
content_type=CONTENT_TYPES['docx'],
binary_data=final_document_stream.read(),
irb_doc_code=irb_doc_code)
rv = self.app.post('/v1.0/file?study_id=%i&workflow_id=%s&task_spec_name=%s&form_field_key=%s' %
(workflow.study_id, workflow.id, task.name, 'Study_App_Doc'),
data=data,
follow_redirects=True,
content_type='multipart/form-data',
headers=self.logged_in_headers())
workflow_api = self.complete_form(workflow, task, )
print('test_calling_script')