Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is the implementation of "Add email template text to Contact Participants page" #1363

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,16 @@ def send_announcement_email(cls, user: User, study, children):
return announcement_message

def send_as_email(self):
lab_email = self.related_study.lab.contact_email

context = {
"base_url": settings.BASE_URL,
"custom_message": mark_safe(self.body),
"lab_name": self.related_study.lab.name,
"lab_email": lab_email,
"study_name": self.related_study.name,
}

lab_email = self.related_study.lab.contact_email

recipient_email_list = list(self.recipients.values_list("username", flat=True))

for to_email in recipient_email_list:
Expand Down
1 change: 1 addition & 0 deletions studies/templates/emails/custom_email.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "emails/base.html" %}
{% block content %}
{% include "emails/custom_email_header.html" %}
{% autoescape off %}
{{ custom_message }}
{% endautoescape %}
Expand Down
2 changes: 2 additions & 0 deletions studies/templates/emails/custom_email.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% extends "emails/base.txt" %}

{% block content %}
{% include "emails/custom_email_header.txt" %}
{{ custom_message|striptags }}
{% endblock content %}
1 change: 1 addition & 0 deletions studies/templates/emails/custom_email_header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{% include "emails/custom_email_header.txt" %}</p>
1 change: 1 addition & 0 deletions studies/templates/emails/custom_email_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is an email from {{ lab_name }} for "{{ study_name }}". You may reply to this email to message directly with the researchers at {{ lab_name }} or email them at {{ lab_email }} if you have other questions.
9 changes: 9 additions & 0 deletions studies/templates/studies/study_participant_contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@
</div>
</div>
</div>
<div class="card bg-light mb-3">
<div class="card-header">Email Header</div>
<div class="card-body">
<p>All particiapant emails will see the following header when they're contacted from this form:</p>
<i>
{% include "emails/custom_email_header.html" with lab_name=study.lab.name study_name=study.name lab_email=study.lab.contact_email %}
</i>
</div>
</div>
<form method="post">
{% csrf_token %}
{% bootstrap_form form %}
Expand Down
15 changes: 10 additions & 5 deletions studies/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,13 @@ def test_potential_message_targets_external(self):

class TestSendMail(TestCase):
def setUp(self):
self.context = {"token": G(User).generate_token(), "username": "username"}
self.context = {
"token": G(User).generate_token(),
"username": "username",
"lab_email": "lab_email",
"study_name": "study_name",
"lab_name": "lab_name",
}

def test_send_email_with_image(self):
email = send_mail(
Expand All @@ -613,9 +619,8 @@ def test_send_email_with_image(self):
self.assertEqual(email.subject, "Test email")
self.assertEqual(email.to, ["[email protected]"])
self.assertTrue(
email.body.startswith(
"\nline 1[IMAGE]line 2\n\n\nUpdate your email preferences here"
),
"\nline 1[IMAGE]line 2\n\n\nUpdate your email preferences here"
in email.body,
"Email plain text does not have expected substitution of [IMAGE] for image tag",
)
self.assertEqual(
Expand All @@ -627,7 +632,7 @@ def test_send_email_with_image(self):
self.assertEqual(
email.alternatives[0],
(
f'\n \n <p>line 1<br></p><p><img style="width: 24px;" src="cid:image-00001" data-filename="small.jpg"></p><p>line 2<br></p>\n \n\n<br />\n<a href="https://localhost:8000/account/email/">Update your email preferences</a>\n<a href="https://localhost:8000/account/{self.context["username"]}/{self.context["token"]}/">Unsubscribe from all emails</a>\n',
f'\n <p>This is an email from {self.context["lab_name"]} for "{self.context["study_name"]}". You may reply to this email to message directly with the researchers at {self.context["lab_name"]} or email them at {self.context["lab_email"]} if you have other questions.\n</p>\n\n \n <p>line 1<br></p><p><img style="width: 24px;" src="cid:image-00001" data-filename="small.jpg"></p><p>line 2<br></p>\n \n\n<br />\n<a href="https://localhost:8000/account/email/">Update your email preferences</a>\n<a href="https://localhost:8000/account/{self.context["username"]}/{self.context["token"]}/">Unsubscribe from all emails</a>\n',
"text/html",
),
)
Expand Down