2021-10-07 13:22:30 +00:00
|
|
|
from crc.scripts.script import Script
|
|
|
|
from crc.api.common import ApiError
|
|
|
|
from crc import session
|
|
|
|
from crc.models.email import EmailModel, EmailModelSchema
|
2021-10-26 18:29:38 +00:00
|
|
|
from crc.services.email_service import EmailService
|
|
|
|
|
|
|
|
import datetime
|
2021-10-07 13:22:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EmailData(Script):
|
|
|
|
|
|
|
|
def get_description(self):
|
|
|
|
return """This is my description"""
|
|
|
|
|
|
|
|
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
|
2021-10-12 17:44:36 +00:00
|
|
|
if 'email_id' in kwargs or 'workflow_spec_id' in kwargs:
|
2021-10-26 18:29:38 +00:00
|
|
|
subject = 'My Test Email'
|
|
|
|
recipients = 'user@example.com'
|
2021-11-02 14:00:41 +00:00
|
|
|
content = "Hello"
|
|
|
|
content_html = "<!DOCTYPE html><html><head></head><body><div><h2>Hello</h2></div></body></html>"
|
2021-10-26 18:29:38 +00:00
|
|
|
email_model = EmailModel(subject=subject,
|
|
|
|
recipients=recipients,
|
|
|
|
content=content,
|
|
|
|
content_html=content_html,
|
|
|
|
timestamp=datetime.datetime.utcnow())
|
|
|
|
return EmailModelSchema(many=True).dump([email_model])
|
|
|
|
|
2021-10-07 13:22:30 +00:00
|
|
|
else:
|
2021-10-26 18:29:38 +00:00
|
|
|
raise ApiError.from_task(code='missing_email_id',
|
|
|
|
message='You must include an email_id or workflow_spec_id with the get_email_data script.',
|
|
|
|
task=task)
|
2021-10-07 13:22:30 +00:00
|
|
|
|
2021-10-26 18:29:38 +00:00
|
|
|
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
|
2021-10-07 13:22:30 +00:00
|
|
|
email_models = None
|
2021-10-07 16:10:37 +00:00
|
|
|
email_data = None
|
2021-10-07 13:22:30 +00:00
|
|
|
if 'email_id' in kwargs:
|
|
|
|
email_models = session.query(EmailModel).filter(EmailModel.id == kwargs['email_id']).all()
|
2021-10-12 17:44:36 +00:00
|
|
|
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()
|
2021-10-07 13:22:30 +00:00
|
|
|
else:
|
|
|
|
raise ApiError.from_task(code='missing_email_id',
|
2021-10-12 17:44:36 +00:00
|
|
|
message='You must include an email_id or workflow_spec_id with the get_email_data script.',
|
2021-10-07 13:22:30 +00:00
|
|
|
task=task)
|
|
|
|
|
|
|
|
if email_models:
|
|
|
|
email_data = EmailModelSchema(many=True).dump(email_models)
|
|
|
|
return email_data
|