Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1334 from akatsoulas/send-remo-mail-remobot
Browse files Browse the repository at this point in the history
Originate all emails from ReMobot in send_remo_mail.
  • Loading branch information
akatsoulas authored Mar 20, 2017
2 parents b464d93 + 9089aa2 commit 5da70fb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
25 changes: 16 additions & 9 deletions remo/base/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ def send_remo_mail(subject, recipients_list, sender=None,
if email_template:
message = render_to_string(email_template, data)

if not sender:
email = EmailMessage(subject=subject, body=message,
from_email=settings.FROM_EMAIL,
to=[to], headers=headers)
else:
email = EmailMessage(subject=subject, body=message,
from_email=sender, to=[to], cc=[sender],
headers=headers)
email.send()
email_data = {
'subject': subject,
'body': message,
'from_email': settings.FROM_EMAIL,
'to': [to],
}

if sender:
# If there is a sender, add a Reply-To header and send a copy to the sender
headers.update({'Reply-To': sender})
email_data.update({'cc': [sender]})

# Add the headers to the mail data
email_data.update({'headers': headers})
# Send the email
EmailMessage(**email_data).send()
10 changes: 5 additions & 5 deletions remo/base/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def test_base(self):
to=[to],
cc=[from_email],
subject=subject,
headers={},
from_email=from_email)
headers={'Reply-To': from_email},
from_email=settings.FROM_EMAIL)

def test_send_email_from_remobot(self):
recipient = UserFactory.create()
Expand Down Expand Up @@ -75,8 +75,8 @@ def test_send_to_valid_email_address(self):
to=['mail@example.com'],
cc=[from_email],
subject=subject,
headers={},
from_email=from_email)
headers={'Reply-To': from_email},
from_email=settings.FROM_EMAIL)

def test_send_to_invalid_email_address(self):
subject = 'This is the subject'
Expand All @@ -102,5 +102,5 @@ def test_headers(self):
to=['mail@example.com'],
cc=[from_email],
subject=subject,
from_email=from_email,
from_email=settings.FROM_EMAIL,
headers=headers)
4 changes: 2 additions & 2 deletions remo/profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,12 @@ def email_mentor_notification(sender, instance, raw, **kwargs):
subject=subject,
email_template=email_template,
data=ctx_data,
headers={'Reply-To': instance.user.email})
sender=instance.user.email)
send_remo_mail.delay(recipients_list=rep_recipient,
subject=subject,
email_template=email_template,
data=ctx_data,
headers={'Reply-To': instance.mentor.email})
sender=instance.mentor.email)
statsd.incr('profiles.change_mentor')


Expand Down
2 changes: 0 additions & 2 deletions remo/profiles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ def test_send_email_on_mentor_change(self, mocked_mail):
data2 = mocked_mail.call_args_list[1][1]
recipients = data1['recipients_list'] + data2['recipients_list']
eq_(set(recipients), set([new_mentor.id, old_mentor.id, user.id]))
eq_(data1['headers']['Reply-To'], user.email)
eq_(data2['headers']['Reply-To'], new_mentor.email)


class UserStatusNotification(RemoTestCase):
Expand Down
17 changes: 9 additions & 8 deletions remo/reports/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ def test_base(self):

eq_(mail_mock.call_count, 2)
expected_call_list = [
call(rep_subject, [rep.email], message=mockany,
headers={'Reply-To': mentor.email}),
call(mentor_subject, [mentor.email], message=mockany,
headers={'Reply-To': rep.email})]
call(subject=rep_subject, recipients_list=[rep.email], message=mockany,
sender=mentor.email),
call(subject=mentor_subject, recipients_list=[mentor.email], message=mockany,
sender=rep.email)]
eq_(mail_mock.call_args_list, expected_call_list)

def test_with_report_filled(self):
Expand Down Expand Up @@ -132,10 +132,11 @@ def test_with_no_report_filled_and_one_notification(self):

eq_(mail_mock.call_count, 2)
expected_call_list = [
call(rep_subject, [rep.email], message=mockany,
headers={'Reply-To': mentor.email}),
call(mentor_subject, [mentor.email], message=mockany,
headers={'Reply-To': rep.email})]
call(subject=rep_subject, recipients_list=[rep.email], message=mockany,
sender=mentor.email),
call(subject=mentor_subject, recipients_list=[mentor.email], message=mockany,
sender=rep.email)
]
eq_(mail_mock.call_args_list, expected_call_list)

def test_with_user_unavailable(self):
Expand Down
11 changes: 6 additions & 5 deletions remo/reports/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ def send_report_notification(reps, weeks):
rep_message = render_to_string(rep_mail_body, ctx_data)
mentor_message = render_to_string(mentor_mail_body, ctx_data)
if mentor:
send_remo_mail(rep_subject, [rep.email], message=rep_message,
headers={'Reply-To': mentor.email})
send_remo_mail(mentor_subject, [mentor.email],
message=mentor_message,
headers={'Reply-To': rep.email})
send_remo_mail(subject=rep_subject, recipients_list=[rep.email],
sender=mentor.email, message=rep_message)
send_remo_mail(subject=mentor_subject,
recipients_list=[mentor.email],
sender=rep.email,
message=mentor_message)
else:
send_remo_mail(rep_subject, [rep.email], message=rep_message)

0 comments on commit 5da70fb

Please sign in to comment.