2020-02-10 21:19:23 +00:00
|
|
|
from io import StringIO, BytesIO
|
|
|
|
|
|
|
|
from jinja2 import UndefinedError
|
|
|
|
|
|
|
|
from crc import session
|
|
|
|
from crc.api.common import ApiError
|
2020-03-04 18:40:25 +00:00
|
|
|
from crc.models.file import FileModel, FileDataModel, CONTENT_TYPES
|
Refactor the document details scripts. Now there is one script, it returns data in a consistent format, and has all the details required. The script is located in StudyInfo, with the argument documents. Make note that it returns a dictionary of ALL the documents, with a field to mark which ones are required according to the protocol builder. Others may become required if a workflow determines such, in which case the workflow will enforce this, and the document will have a count > 0, and additional details in a list of files within the document. I modified the XLS file to use lower case variable names, because it disturbed me, and we have to reference them frequently. Removed devious "as_object" variable on get_required_docs, so it behaves like the other methods all the time, and returns a dictionary. All the core business logic for finding the documents list now resides in the StudyService.
Because this changes the endpoint for all existing document details, I've modified all the test and static bpmn files to use the new format.
Shorting up the SponsorsList.xls file makes for slightly faster tests. seems senseless to load 5000 everytime we reset the data.
Tried to test all of this carefully in the test_study_details_documents.py test.
2020-04-29 19:08:11 +00:00
|
|
|
from crc.models.workflow import WorkflowSpecModel, WorkflowModel
|
2020-02-10 21:19:23 +00:00
|
|
|
from docxtpl import DocxTemplate
|
|
|
|
import jinja2
|
|
|
|
|
2020-03-03 18:50:22 +00:00
|
|
|
from crc.scripts.script import Script
|
2020-02-28 16:54:11 +00:00
|
|
|
from crc.services.file_service import FileService
|
2020-02-10 21:19:23 +00:00
|
|
|
from crc.services.workflow_processor import WorkflowProcessor
|
|
|
|
|
|
|
|
|
2020-03-03 18:50:22 +00:00
|
|
|
class CompleteTemplate(Script):
|
|
|
|
|
|
|
|
def get_description(self):
|
2020-03-19 21:13:30 +00:00
|
|
|
return """
|
|
|
|
Using the Jinja template engine, takes data available in the current task, and uses it to populate
|
|
|
|
a word document that contains Jinja markup. Please see https://docxtpl.readthedocs.io/en/latest/
|
|
|
|
for more information on exact syntax.
|
|
|
|
Takes two arguments:
|
|
|
|
1. The name of a MS Word docx file to use as a template.
|
|
|
|
2. The 'code' of the IRB Document as set in the irb_documents.xlsx file."
|
|
|
|
"""
|
2020-02-10 21:19:23 +00:00
|
|
|
|
2020-03-27 12:29:31 +00:00
|
|
|
def do_task_validate_only(self, task, study_id, *args, **kwargs):
|
|
|
|
"""For validation only, process the template, but do not store it in the database."""
|
|
|
|
self.process_template(task, study_id, *args, **kwargs)
|
|
|
|
|
2020-03-03 18:50:22 +00:00
|
|
|
def do_task(self, task, study_id, *args, **kwargs):
|
2020-03-27 12:29:31 +00:00
|
|
|
workflow_id = task.workflow.data[WorkflowProcessor.WORKFLOW_ID_KEY]
|
|
|
|
final_document_stream = self.process_template(task, study_id, *args, **kwargs)
|
Refactor the document details scripts. Now there is one script, it returns data in a consistent format, and has all the details required. The script is located in StudyInfo, with the argument documents. Make note that it returns a dictionary of ALL the documents, with a field to mark which ones are required according to the protocol builder. Others may become required if a workflow determines such, in which case the workflow will enforce this, and the document will have a count > 0, and additional details in a list of files within the document. I modified the XLS file to use lower case variable names, because it disturbed me, and we have to reference them frequently. Removed devious "as_object" variable on get_required_docs, so it behaves like the other methods all the time, and returns a dictionary. All the core business logic for finding the documents list now resides in the StudyService.
Because this changes the endpoint for all existing document details, I've modified all the test and static bpmn files to use the new format.
Shorting up the SponsorsList.xls file makes for slightly faster tests. seems senseless to load 5000 everytime we reset the data.
Tried to test all of this carefully in the test_study_details_documents.py test.
2020-04-29 19:08:11 +00:00
|
|
|
workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
|
2020-03-27 12:29:31 +00:00
|
|
|
file_name = args[0]
|
|
|
|
irb_doc_code = args[1]
|
2020-04-19 19:14:10 +00:00
|
|
|
FileService.add_task_file(study_id=study_id,
|
|
|
|
workflow_id=workflow_id,
|
Refactor the document details scripts. Now there is one script, it returns data in a consistent format, and has all the details required. The script is located in StudyInfo, with the argument documents. Make note that it returns a dictionary of ALL the documents, with a field to mark which ones are required according to the protocol builder. Others may become required if a workflow determines such, in which case the workflow will enforce this, and the document will have a count > 0, and additional details in a list of files within the document. I modified the XLS file to use lower case variable names, because it disturbed me, and we have to reference them frequently. Removed devious "as_object" variable on get_required_docs, so it behaves like the other methods all the time, and returns a dictionary. All the core business logic for finding the documents list now resides in the StudyService.
Because this changes the endpoint for all existing document details, I've modified all the test and static bpmn files to use the new format.
Shorting up the SponsorsList.xls file makes for slightly faster tests. seems senseless to load 5000 everytime we reset the data.
Tried to test all of this carefully in the test_study_details_documents.py test.
2020-04-29 19:08:11 +00:00
|
|
|
workflow_spec_id=workflow.workflow_spec_id,
|
2020-04-19 19:14:10 +00:00
|
|
|
task_id=task.id,
|
2020-03-27 12:29:31 +00:00
|
|
|
name=file_name,
|
|
|
|
content_type=CONTENT_TYPES['docx'],
|
|
|
|
binary_data=final_document_stream.read(),
|
|
|
|
irb_doc_code=irb_doc_code)
|
|
|
|
|
|
|
|
def process_template(self, task, study_id, *args, **kwargs):
|
2020-02-10 21:19:23 +00:00
|
|
|
"""Entry point, mostly worried about wiring it all up."""
|
2020-03-19 21:13:30 +00:00
|
|
|
if len(args) != 2:
|
2020-02-10 21:19:23 +00:00
|
|
|
raise ApiError(code="missing_argument",
|
2020-03-19 21:13:30 +00:00
|
|
|
message="The CompleteTemplate script requires 2 arguments. The first argument is "
|
|
|
|
"the name of the docx template to use. The second "
|
|
|
|
"argument is a code for the document, as "
|
2020-05-07 17:57:24 +00:00
|
|
|
"set in the reference document %s. " % FileService.DOCUMENT_LIST)
|
2020-03-16 14:37:06 +00:00
|
|
|
task_study_id = task.workflow.data[WorkflowProcessor.STUDY_ID_KEY]
|
2020-03-27 12:29:31 +00:00
|
|
|
file_name = args[0]
|
2020-03-16 14:37:06 +00:00
|
|
|
|
|
|
|
if task_study_id != study_id:
|
|
|
|
raise ApiError(code="invalid_argument",
|
|
|
|
message="The given task does not match the given study.")
|
2020-02-10 21:19:23 +00:00
|
|
|
|
2020-04-15 15:13:32 +00:00
|
|
|
file_data_model = FileService.get_workflow_file_data(task.workflow, file_name)
|
2020-03-27 12:29:31 +00:00
|
|
|
return self.make_template(BytesIO(file_data_model.data), task.data)
|
|
|
|
|
2020-02-10 21:19:23 +00:00
|
|
|
|
2020-02-29 22:22:38 +00:00
|
|
|
def make_template(self, binary_stream, context):
|
|
|
|
doc = DocxTemplate(binary_stream)
|
2020-02-12 16:07:01 +00:00
|
|
|
jinja_env = jinja2.Environment(autoescape=True)
|
2020-02-10 21:19:23 +00:00
|
|
|
doc.render(context, jinja_env)
|
|
|
|
target_stream = BytesIO()
|
|
|
|
doc.save(target_stream)
|
|
|
|
target_stream.seek(0) # move to the beginning of the stream.
|
|
|
|
return target_stream
|
|
|
|
|
2020-04-15 15:13:32 +00:00
|
|
|
|