diff --git a/crc/models/approval.py b/crc/models/approval.py index be83ba30..df433fac 100644 --- a/crc/models/approval.py +++ b/crc/models/approval.py @@ -57,23 +57,11 @@ class Approval(object): @classmethod def from_model(cls, model: ApprovalModel): - # TODO: Reduce the code by iterating over model's dict keys - instance = cls() - instance.id = model.id - instance.study_id = model.study_id - instance.workflow_id = model.workflow_id - instance.version = model.version - instance.approver_uid = model.approver_uid - instance.status = model.status - instance.message = model.message - instance.date_created = model.date_created - instance.date_approved = model.date_approved - instance.version = model.version - instance.title = '' + args = dict((k, v) for k, v in model.__dict__.items() if not k.startswith('_')) + instance = cls(**args) instance.related_approvals = [] + instance.title = model.study.title if model.study else '' - if model.study: - instance.title = model.study.title try: instance.approver = LdapService.user_info(model.approver_uid) instance.primary_investigator = LdapService.user_info(model.study.primary_investigator_id) diff --git a/crc/services/email_service.py b/crc/services/email_service.py index 51886e3b..f800d900 100644 --- a/crc/services/email_service.py +++ b/crc/services/email_service.py @@ -13,7 +13,7 @@ class EmailService(object): """Provides common tools for working with an Email""" @staticmethod - def add_email(subject, sender, recipients, content, content_html, study_id): + def add_email(subject, sender, recipients, content, content_html, study_id=None): """We will receive all data related to an email and store it""" # Find corresponding study - if any diff --git a/tests/emails/test_email_service.py b/tests/emails/test_email_service.py index e2bcd139..174dca13 100644 --- a/tests/emails/test_email_service.py +++ b/tests/emails/test_email_service.py @@ -31,4 +31,15 @@ class TestEmailService(BaseTest): self.assertEqual(email_model.content_html, content_html) self.assertEqual(email_model.study, study) - # TODO: Create email model without study + subject = 'Email Subject - Empty study' + EmailService.add_email(subject=subject, sender=sender, recipients=recipients, + content=content, content_html=content_html) + + email_model = EmailModel.query.order_by(EmailModel.id.desc()).first() + + self.assertEqual(email_model.subject, subject) + self.assertEqual(email_model.sender, sender) + self.assertEqual(email_model.recipients, str(recipients)) + self.assertEqual(email_model.content, content) + self.assertEqual(email_model.content_html, content_html) + self.assertEqual(email_model.study, None)