2020-05-19 21:51:54 -04:00
|
|
|
import re
|
2020-05-18 11:55:10 -04:00
|
|
|
from io import BytesIO
|
2020-02-10 16:19:23 -05:00
|
|
|
|
2021-11-23 14:38:48 -05:00
|
|
|
from SpiffWorkflow.exceptions import WorkflowTaskExecException
|
2020-02-10 16:19:23 -05:00
|
|
|
|
|
|
|
from crc import session
|
|
|
|
from crc.api.common import ApiError
|
2021-10-20 14:04:45 -04:00
|
|
|
from crc.models.file import CONTENT_TYPES, FileModel
|
2020-05-18 11:55:10 -04:00
|
|
|
from crc.models.workflow import WorkflowModel
|
2020-03-03 13:50:22 -05:00
|
|
|
from crc.scripts.script import Script
|
2021-10-21 10:29:56 -04:00
|
|
|
from crc.services.jinja_service import JinjaService
|
2022-01-07 15:34:51 -05:00
|
|
|
from crc.services.spec_file_service import SpecFileService
|
2022-02-02 12:59:56 -05:00
|
|
|
from crc.services.user_file_service import UserFileService
|
2020-02-10 16:19:23 -05:00
|
|
|
from crc.services.workflow_processor import WorkflowProcessor
|
2022-02-08 11:30:13 -05:00
|
|
|
from crc.services.workflow_spec_service import WorkflowSpecService
|
2020-02-10 16:19:23 -05:00
|
|
|
|
|
|
|
|
2020-03-03 13:50:22 -05:00
|
|
|
class CompleteTemplate(Script):
|
|
|
|
|
|
|
|
def get_description(self):
|
2021-09-22 12:12:26 -04:00
|
|
|
return """Using the Jinja template engine, takes data available in the current task, and uses it to populate
|
2020-03-19 17:13:30 -04:00
|
|
|
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.
|
2022-02-02 12:59:56 -05:00
|
|
|
2. The 'code' of the IRB Document as set in the documents.xlsx file."
|
2020-03-19 17:13:30 -04:00
|
|
|
"""
|
2020-02-10 16:19:23 -05:00
|
|
|
|
2020-05-24 16:13:15 -04:00
|
|
|
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
|
2020-03-27 08:29:31 -04:00
|
|
|
"""For validation only, process the template, but do not store it in the database."""
|
2020-06-01 12:33:58 -04:00
|
|
|
workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
|
|
|
|
self.process_template(task, study_id, workflow, *args, **kwargs)
|
2020-03-27 08:29:31 -04:00
|
|
|
|
2020-05-24 16:13:15 -04:00
|
|
|
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
|
2022-02-08 11:30:13 -05:00
|
|
|
workflow_spec_service = WorkflowSpecService()
|
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 15:08:11 -04:00
|
|
|
workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
|
2020-05-20 00:10:32 -04:00
|
|
|
final_document_stream = self.process_template(task, study_id, workflow, *args, **kwargs)
|
2020-03-27 08:29:31 -04:00
|
|
|
file_name = args[0]
|
|
|
|
irb_doc_code = args[1]
|
2022-02-02 12:59:56 -05:00
|
|
|
UserFileService.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)
|
2020-03-27 08:29:31 -04:00
|
|
|
|
2020-05-20 00:10:32 -04:00
|
|
|
def process_template(self, task, study_id, workflow=None, *args, **kwargs):
|
2020-02-10 16:19:23 -05:00
|
|
|
"""Entry point, mostly worried about wiring it all up."""
|
2020-05-19 21:51:54 -04:00
|
|
|
if len(args) < 2 or len(args) > 3:
|
2020-02-10 16:19:23 -05:00
|
|
|
raise ApiError(code="missing_argument",
|
2020-03-19 17:13:30 -04: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 "
|
2022-02-02 12:59:56 -05:00
|
|
|
"set in the reference document.")
|
2020-03-16 10:37:06 -04:00
|
|
|
task_study_id = task.workflow.data[WorkflowProcessor.STUDY_ID_KEY]
|
2020-03-27 08:29:31 -04:00
|
|
|
file_name = args[0]
|
2020-03-16 10:37:06 -04: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 16:19:23 -05:00
|
|
|
|
2022-01-25 16:10:54 -05:00
|
|
|
file_data = None
|
2020-05-20 00:10:32 -04:00
|
|
|
if workflow is not None:
|
2022-02-08 11:30:13 -05:00
|
|
|
workflow_spec_service = WorkflowSpecService()
|
|
|
|
spec = workflow_spec_service.get_spec(workflow.workflow_spec_id)
|
|
|
|
file_data = SpecFileService().get_data(spec, file_name)
|
2020-03-27 08:29:31 -04:00
|
|
|
|
2020-05-19 21:51:54 -04:00
|
|
|
# Get images from file/files fields
|
|
|
|
if len(args) == 3:
|
2020-05-20 00:10:32 -04:00
|
|
|
image_file_data = self.get_image_file_data(args[2], task)
|
|
|
|
else:
|
|
|
|
image_file_data = None
|
2020-05-19 21:51:54 -04:00
|
|
|
|
2021-11-23 14:38:48 -05:00
|
|
|
try:
|
2022-01-25 16:10:54 -05:00
|
|
|
return JinjaService().make_template(BytesIO(file_data), task.data, image_file_data)
|
2021-11-23 14:38:48 -05:00
|
|
|
except ApiError as ae:
|
|
|
|
# In some cases we want to provide a very specific error, that does not get obscured when going
|
|
|
|
# through the python expression engine. We can do that by throwing a WorkflowTaskExecException,
|
|
|
|
# which the expression engine should just pass through.
|
|
|
|
raise WorkflowTaskExecException(task, ae.message, exception=ae, line_number=ae.line_number,
|
|
|
|
error_line=ae.error_line)
|
2020-05-19 21:51:54 -04:00
|
|
|
|
2020-05-20 00:10:32 -04:00
|
|
|
def get_image_file_data(self, fields_str, task):
|
|
|
|
image_file_data = []
|
|
|
|
images_field_str = re.sub(r'[\[\]]', '', fields_str)
|
|
|
|
images_field_keys = [v.strip() for v in images_field_str.strip().split(',')]
|
|
|
|
for field_key in images_field_keys:
|
|
|
|
if field_key in task.data:
|
|
|
|
v = task.data[field_key]
|
|
|
|
file_ids = v if isinstance(v, list) else [v]
|
|
|
|
|
|
|
|
for file_id in file_ids:
|
|
|
|
if isinstance(file_id, str) and file_id.isnumeric():
|
|
|
|
file_id = int(file_id)
|
|
|
|
|
|
|
|
if file_id is not None and isinstance(file_id, int):
|
|
|
|
if not task.workflow.data[WorkflowProcessor.VALIDATION_PROCESS_KEY]:
|
|
|
|
# Get the actual image data
|
|
|
|
image_file_model = session.query(FileModel).filter_by(id=file_id).first()
|
2022-02-02 12:59:56 -05:00
|
|
|
image_file_data_model = UserFileService.get_file_data(file_id, image_file_model)
|
2020-05-20 00:10:32 -04:00
|
|
|
if image_file_data_model is not None:
|
|
|
|
image_file_data.append(image_file_data_model)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise ApiError(
|
|
|
|
code="not_a_file_id",
|
|
|
|
message="The CompleteTemplate script requires 2-3 arguments. The third argument should "
|
|
|
|
"be a comma-delimited list of File IDs")
|
2020-05-20 00:12:48 -04:00
|
|
|
|
2020-05-20 00:10:32 -04:00
|
|
|
return image_file_data
|