2020-02-29 22:22:38 +00:00
|
|
|
import io
|
|
|
|
import json
|
|
|
|
|
|
|
|
import connexion
|
2020-07-22 15:30:16 +00:00
|
|
|
from SpiffWorkflow.bpmn.PythonScriptEngine import PythonScriptEngine
|
2020-02-29 22:22:38 +00:00
|
|
|
from flask import send_file
|
|
|
|
from jinja2 import Template, UndefinedError
|
|
|
|
|
2020-03-03 20:30:42 +00:00
|
|
|
from crc.api.common import ApiError
|
2020-03-03 18:50:22 +00:00
|
|
|
from crc.scripts.complete_template import CompleteTemplate
|
2020-03-03 20:30:42 +00:00
|
|
|
from crc.scripts.script import Script
|
2021-03-11 17:38:47 +00:00
|
|
|
|
2021-03-11 16:19:02 +00:00
|
|
|
from crc.services.email_service import EmailService
|
|
|
|
from config.default import DEFAULT_SENDER
|
2020-06-08 18:15:56 +00:00
|
|
|
|
2020-02-29 22:22:38 +00:00
|
|
|
|
|
|
|
def render_markdown(data, template):
|
|
|
|
"""
|
2020-06-18 18:53:50 +00:00
|
|
|
Provides a quick way to very that a Jinja markdown template will work properly on a given json
|
2020-02-29 22:22:38 +00:00
|
|
|
data structure. Useful for folks that are building these markdown templates.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
template = Template(template)
|
|
|
|
data = json.loads(data)
|
|
|
|
return template.render(**data)
|
|
|
|
except UndefinedError as ue:
|
2020-06-03 19:03:22 +00:00
|
|
|
raise ApiError(code="undefined_field", message=ue.message)
|
2020-02-29 22:22:38 +00:00
|
|
|
except Exception as e:
|
2020-06-03 19:03:22 +00:00
|
|
|
raise ApiError(code="invalid_render", message=str(e))
|
2020-02-29 22:22:38 +00:00
|
|
|
|
|
|
|
|
2020-05-22 19:30:22 +00:00
|
|
|
def render_docx():
|
2020-02-29 22:22:38 +00:00
|
|
|
"""
|
|
|
|
Provides a quick way to verify that a Jinja docx template will work properly on a given json
|
|
|
|
data structure. Useful for folks that are building these templates.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
file = connexion.request.files['file']
|
2020-05-22 19:30:22 +00:00
|
|
|
data = connexion.request.form['data']
|
2020-02-29 22:22:38 +00:00
|
|
|
target_stream = CompleteTemplate().make_template(file, json.loads(data))
|
|
|
|
return send_file(
|
|
|
|
io.BytesIO(target_stream.read()),
|
|
|
|
as_attachment=True,
|
|
|
|
attachment_filename="output.docx",
|
|
|
|
mimetype="application/octet-stream",
|
|
|
|
cache_timeout=-1 # Don't cache these files on the browser.
|
|
|
|
)
|
|
|
|
except ValueError as e:
|
2020-06-03 19:03:22 +00:00
|
|
|
raise ApiError(code="undefined_field", message=str(e))
|
2020-02-29 22:22:38 +00:00
|
|
|
except Exception as e:
|
2020-06-03 19:03:22 +00:00
|
|
|
raise ApiError(code="invalid_render", message=str(e))
|
2020-03-03 20:30:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def list_scripts():
|
|
|
|
"""Provides a list of scripts that can be called by a script task."""
|
|
|
|
scripts = Script.get_all_subclasses()
|
|
|
|
script_meta = []
|
|
|
|
for script_class in scripts:
|
|
|
|
script_meta.append(
|
|
|
|
{
|
|
|
|
"name": script_class.__name__,
|
|
|
|
"description": script_class().get_description()
|
|
|
|
})
|
|
|
|
return script_meta
|
|
|
|
|
2020-07-21 17:57:33 +00:00
|
|
|
|
2021-03-11 16:19:02 +00:00
|
|
|
def send_email(subject, address, body, data=None):
|
2020-06-08 18:15:56 +00:00
|
|
|
"""Just sends a quick test email to assure the system is working."""
|
2021-03-11 16:19:02 +00:00
|
|
|
if address and body:
|
|
|
|
body = body.decode('UTF-8')
|
|
|
|
return send_test_email(subject, address, body, json.loads(data))
|
|
|
|
else:
|
|
|
|
raise ApiError(code='missing_parameter',
|
|
|
|
message='You must provide an email address and a message.')
|
2020-07-21 17:57:33 +00:00
|
|
|
|
|
|
|
|
2020-08-20 20:13:48 +00:00
|
|
|
def evaluate_python_expression(body):
|
2020-08-21 17:46:10 +00:00
|
|
|
"""Evaluate the given python expression, returning its result. This is useful if the
|
2020-07-21 17:57:33 +00:00
|
|
|
front end application needs to do real-time processing on task data. If for instance
|
|
|
|
there is a hide expression that is based on a previous value in the same form."""
|
|
|
|
try:
|
2021-03-08 19:00:03 +00:00
|
|
|
script_engine = CustomBpmnScriptEngine()
|
|
|
|
result = script_engine.eval(body['expression'], body['data'])
|
2020-08-20 20:13:48 +00:00
|
|
|
return {"result": result}
|
2020-07-21 17:57:33 +00:00
|
|
|
except Exception as e:
|
2021-03-08 19:00:03 +00:00
|
|
|
raise ApiError("expression_error", f"Failed to evaluate the expression '%s'. %s" %
|
|
|
|
(body['expression'], str(e)),
|
|
|
|
task_data = body["data"])
|
2021-03-11 16:19:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def send_test_email(subject, address, message, data=None):
|
|
|
|
rendered, wrapped = EmailService().get_rendered_content(message, data)
|
|
|
|
EmailService.add_email(
|
|
|
|
subject=subject,
|
|
|
|
sender=DEFAULT_SENDER,
|
|
|
|
recipients=[address],
|
|
|
|
content=rendered,
|
|
|
|
content_html=wrapped)
|