Merge pull request #397 from sartography/email-data-script-487
Email data script #487
This commit is contained in:
commit
6428e4ab0a
|
@ -18,7 +18,7 @@ class EmailModel(db.Model):
|
|||
content_html = db.Column(db.String)
|
||||
study_id = db.Column(db.Integer, db.ForeignKey(StudyModel.id), nullable=True)
|
||||
timestamp = db.Column(db.DateTime(timezone=True), default=func.now())
|
||||
workflow_id = db.Column(db.String, nullable=True)
|
||||
workflow_spec_id = db.Column(db.String, nullable=True)
|
||||
study = db.relationship(StudyModel)
|
||||
|
||||
|
||||
|
|
|
@ -66,6 +66,8 @@ email(subject="My Subject", recipients="user@example.com", attachments=['Study_A
|
|||
message="Email script requires a subject and at least one email recipient as arguments")
|
||||
|
||||
if recipients:
|
||||
wf_model = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
|
||||
workflow_spec_id = wf_model.workflow_spec_id
|
||||
message = task.task_spec.documentation
|
||||
data = task.data
|
||||
try:
|
||||
|
@ -81,7 +83,7 @@ email(subject="My Subject", recipients="user@example.com", attachments=['Study_A
|
|||
study_id=study_id,
|
||||
reply_to=reply_to,
|
||||
attachment_files=files,
|
||||
workflow_id=workflow_id
|
||||
workflow_spec_id=workflow_spec_id
|
||||
)
|
||||
except Exception as e:
|
||||
exc_type, exc_value, exc_traceback = sys.exc_info()
|
||||
|
|
|
@ -11,7 +11,7 @@ class EmailData(Script):
|
|||
return """This is my description"""
|
||||
|
||||
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
|
||||
if 'email_id' in kwargs or 'workflow_id' in kwargs:
|
||||
if 'email_id' in kwargs or 'workflow_spec_id' in kwargs:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
@ -21,11 +21,14 @@ class EmailData(Script):
|
|||
email_data = None
|
||||
if 'email_id' in kwargs:
|
||||
email_models = session.query(EmailModel).filter(EmailModel.id == kwargs['email_id']).all()
|
||||
elif 'email_workflow_id' in kwargs:
|
||||
email_models = session.query(EmailModel).filter(EmailModel.workflow_id == str(kwargs['email_workflow_id'])).all()
|
||||
elif 'workflow_spec_id' in kwargs:
|
||||
email_models = session.query(EmailModel)\
|
||||
.filter(EmailModel.study_id == study_id)\
|
||||
.filter(EmailModel.workflow_spec_id == str(kwargs['workflow_spec_id']))\
|
||||
.all()
|
||||
else:
|
||||
raise ApiError.from_task(code='missing_email_id',
|
||||
message='You must include an email_id with the get_email_data script.',
|
||||
message='You must include an email_id or workflow_spec_id with the get_email_data script.',
|
||||
task=task)
|
||||
|
||||
if email_models:
|
||||
|
|
|
@ -17,7 +17,7 @@ class EmailService(object):
|
|||
|
||||
@staticmethod
|
||||
def add_email(subject, sender, recipients, content, content_html,
|
||||
cc=None, bcc=None, study_id=None, reply_to=None, attachment_files=None, workflow_id=None):
|
||||
cc=None, bcc=None, study_id=None, reply_to=None, attachment_files=None, workflow_spec_id=None):
|
||||
"""We will receive all data related to an email and store it"""
|
||||
|
||||
# Find corresponding study - if any
|
||||
|
@ -28,7 +28,7 @@ class EmailService(object):
|
|||
# Create EmailModel
|
||||
email_model = EmailModel(subject=subject, sender=sender, recipients=str(recipients),
|
||||
content=content, content_html=content_html, study=study,
|
||||
cc=cc, bcc=bcc, workflow_id=workflow_id)
|
||||
cc=cc, bcc=bcc, workflow_spec_id=workflow_spec_id)
|
||||
|
||||
# Send mail
|
||||
try:
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
"""email table changes
|
||||
|
||||
Revision ID: 6d8ceb1c18cb
|
||||
Revises: 25f846183f1c
|
||||
Create Date: 2021-10-12 12:54:08.354995
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '6d8ceb1c18cb'
|
||||
down_revision = '25f846183f1c'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.drop_column('email', 'workflow_id')
|
||||
op.add_column('email', sa.Column('workflow_spec_id', sa.String()))
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_column('email', 'workflow_spec_id')
|
||||
op.add_column('email', sa.Column('workflow_id', sa.String()))
|
|
@ -8,7 +8,7 @@
|
|||
<bpmn:userTask id="Activity_GetData" name="Get Data" camunda:formKey="DataForm">
|
||||
<bpmn:extensionElements>
|
||||
<camunda:formData>
|
||||
<camunda:formField id="workflow_id" label="Workflow ID" type="string" />
|
||||
<camunda:formField id="workflow_spec_id" label="Workflow Spec ID" type="string" />
|
||||
</camunda:formData>
|
||||
</bpmn:extensionElements>
|
||||
<bpmn:incoming>Flow_1erkgz2</bpmn:incoming>
|
||||
|
@ -18,7 +18,7 @@
|
|||
<bpmn:scriptTask id="Activity_GetEmailData" name="Get Email Data">
|
||||
<bpmn:incoming>Flow_1ira7x5</bpmn:incoming>
|
||||
<bpmn:outgoing>Flow_1bt23l3</bpmn:outgoing>
|
||||
<bpmn:script>email_data = get_email_data(email_workflow_id=workflow_id)</bpmn:script>
|
||||
<bpmn:script>email_data = get_email_data(workflow_spec_id=workflow_spec_id)</bpmn:script>
|
||||
</bpmn:scriptTask>
|
||||
<bpmn:sequenceFlow id="Flow_1bt23l3" sourceRef="Activity_GetEmailData" targetRef="Activity_DisplayEmailData" />
|
||||
<bpmn:manualTask id="Activity_DisplayEmailData" name="Display Email Data">
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
from tests.base_test import BaseTest
|
||||
from crc import mail, session
|
||||
from crc.models.study import StudyModel
|
||||
from crc.models.workflow import WorkflowModel
|
||||
from crc.services.email_service import EmailService
|
||||
|
||||
|
@ -9,7 +8,8 @@ class TestGetEmailData(BaseTest):
|
|||
|
||||
def test_get_email_data_by_email_id(self):
|
||||
self.load_example_data()
|
||||
study = session.query(StudyModel).first()
|
||||
workflow = self.create_workflow('get_email_data')
|
||||
study = workflow.study
|
||||
with mail.record_messages() as outbox:
|
||||
# Send an email we can use for get_email_data
|
||||
email_model = EmailService.add_email(subject='My Email Subject',
|
||||
|
@ -17,7 +17,6 @@ class TestGetEmailData(BaseTest):
|
|||
recipients=['joe@example.com'],
|
||||
content='asdf', content_html=None, study_id=study.id)
|
||||
|
||||
workflow = self.create_workflow('get_email_data')
|
||||
workflow_api = self.get_workflow_api(workflow)
|
||||
task = workflow_api.next_task
|
||||
|
||||
|
@ -34,10 +33,12 @@ class TestGetEmailData(BaseTest):
|
|||
self.assertEqual('sender@example.com', email_data[0]['sender'])
|
||||
self.assertEqual('[\'joe@example.com\']', email_data[0]['recipients'])
|
||||
|
||||
def test_get_email_data_by_workflow_id(self):
|
||||
def test_get_email_data_by_workflow_spec_id(self):
|
||||
self.load_example_data()
|
||||
study = session.query(StudyModel).first()
|
||||
workflow = self.create_workflow('get_email_data_by_workflow')
|
||||
study = workflow.study
|
||||
email_workflow = session.query(WorkflowModel).first()
|
||||
email_workflow_spec_id = email_workflow.workflow_spec_id
|
||||
|
||||
with mail.record_messages() as outbox:
|
||||
# Send an email we can use for get_email_data
|
||||
|
@ -47,21 +48,20 @@ class TestGetEmailData(BaseTest):
|
|||
content='asdf',
|
||||
content_html=None,
|
||||
study_id=study.id,
|
||||
workflow_id=email_workflow.id)
|
||||
workflow_spec_id=email_workflow_spec_id)
|
||||
email_model_two = EmailService.add_email(subject='My Other Email Subject',
|
||||
sender='sender2@example.com',
|
||||
recipients=['joanne@example.com'],
|
||||
content='xyzpdq',
|
||||
content_html=None,
|
||||
study_id=study.id,
|
||||
workflow_id=email_workflow.id)
|
||||
workflow_spec_id=email_workflow_spec_id)
|
||||
|
||||
workflow = self.create_workflow('get_email_data_by_workflow')
|
||||
workflow_api = self.get_workflow_api(workflow)
|
||||
task = workflow_api.next_task
|
||||
|
||||
# Run workflow with get_email_data
|
||||
workflow_api = self.complete_form(workflow, task, {'workflow_id': email_workflow.id})
|
||||
workflow_api = self.complete_form(workflow, task, {'workflow_spec_id': email_workflow_spec_id})
|
||||
task = workflow_api.next_task
|
||||
data = task.data
|
||||
|
||||
|
|
Loading…
Reference in New Issue