Skip to content

Commit

Permalink
Merge pull request #350 from softwaresaved/347
Browse files Browse the repository at this point in the history
Add option to email staffs when claimant is emailed
  • Loading branch information
rgaiacs authored Jun 15, 2017
2 parents 4e4004a + 0717acc commit 8e06bcb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
9 changes: 9 additions & 0 deletions lowfat/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class GarlicForm(ModelForm):
initial=False,
label="Suppress email notification for this update to claimant?"
)
not_copy_email_field = BooleanField(
widget=CheckboxInput,
required=False,
initial=True,
label="Suppress copy of email to staff?"
)

def __init__(self, *args, **kwargs):
# Add staff option to not send email notification
Expand Down Expand Up @@ -352,6 +358,7 @@ def __init__(self, *args, **kwargs):
"notes_from_admin",
"email",
'not_send_email_field' if self.is_staff else None,
'not_copy_email_field' if self.is_staff else None,
)
)

Expand Down Expand Up @@ -537,6 +544,7 @@ def __init__(self, *args, **kwargs):
'notes_from_admin',
'email',
'not_send_email_field' if self.is_staff else None,
'not_copy_email_field' if self.is_staff else None,
ButtonHolder(
Submit('submit', 'Update')
)
Expand Down Expand Up @@ -645,6 +653,7 @@ def __init__(self, *args, **kwargs):
'tweet_url',
'email',
'not_send_email_field' if self.is_staff else None,
'not_copy_email_field' if self.is_staff else None,
ButtonHolder(
Submit('submit', 'Update')
)
Expand Down
47 changes: 24 additions & 23 deletions lowfat/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@

from django.contrib.flatpages.models import FlatPage
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.template import Context, Template

from .models import *
from .settings import DEFAULT_FROM_EMAIL, SITE_ID

def mail_admins(subject, message, fail_silently=False, connection=None, html_message=None):
"""Overwrite of Django mail_admins()"""
admins = ast.literal_eval(config.STAFFS_EMAIL) # XXX This is unsecure
STAFFS_EMAIL = ast.literal_eval(config.STAFFS_EMAIL) # XXX This is unsecure

send_mail(
def mail_staffs(subject, message, fail_silently=False, connection=None, html_message=None):
"""Overwrite of Django mail_staffs()"""
msg = EmailMultiAlternatives(
subject,
message,
DEFAULT_FROM_EMAIL,
admins,
fail_silently=fail_silently,
STAFFS_EMAIL,
connection=connection,
html_message=html_message
)
msg.attach_alternative(html_message, "text/html")
msg.send(fail_silently=fail_silently)

def new_notification(admin_url, email_url, user_email, context, mail):
if config.STAFF_EMAIL_NOTIFICATION:
Expand All @@ -43,7 +43,7 @@ def new_notification(admin_url, email_url, user_email, context, mail):
jinja_context = Context(context)
html = template.render(jinja_context)
plain_text = html2text(html)
mail_admins(
mail_staffs(
flatemail.title,
plain_text,
html_message=html,
Expand All @@ -63,14 +63,14 @@ def new_notification(admin_url, email_url, user_email, context, mail):
jinja_context = Context(context)
html = template.render(jinja_context)
plain_text = html2text(html)
send_mail(
msg = EmailMultiAlternatives(
flatemail.title,
plain_text,
DEFAULT_FROM_EMAIL,
user_email,
html_message=html,
fail_silently=False
user_email
)
msg.attach_alternative(html, "text/html")
msg.send(fail_silently=False)
mail.justification = plain_text
mail.save()

Expand Down Expand Up @@ -131,7 +131,7 @@ def new_blog_notification(blog):

new_notification(admin_url, email_url, user_email, context, mail)

def review_notification(email_url, user_email, context, mail):
def review_notification(email_url, user_email, context, mail, copy_to_staffs=False):
"""Compose the message and send the email."""
if config.CLAIMANT_EMAIL_NOTIFICATION and email_url is not None:
# Generate message
Expand All @@ -149,18 +149,19 @@ def review_notification(email_url, user_email, context, mail):
mail.justification = plain_text

# Email to claimant
send_mail(
msg = EmailMultiAlternatives(
flatemail.title,
plain_text,
mail.sender.email,
user_email,
html_message=html,
fail_silently=False
bcc=STAFFS_EMAIL if copy_to_staffs else None
)
msg.attach_alternative(html, "text/html")
msg.send(fail_silently=False)
# Every email is archived in the database
mail.save()

def fund_review_notification(message, sender, old, new):
def fund_review_notification(message, sender, old, new, copy_to_staffs):
user_email = [new.claimant.email]
context = {
"old": old,
Expand All @@ -182,9 +183,9 @@ def fund_review_notification(message, sender, old, new):
else:
email_url = None

review_notification(email_url, user_email, context, mail)
review_notification(email_url, user_email, context, mail, copy_to_staffs)

def expense_review_notification(message, sender, old, new):
def expense_review_notification(message, sender, old, new, copy_to_staffs):
user_email = [new.fund.claimant.email]

context = {
Expand All @@ -207,9 +208,9 @@ def expense_review_notification(message, sender, old, new):
else:
email_url = None

review_notification(email_url, user_email, context, mail)
review_notification(email_url, user_email, context, mail, copy_to_staffs)

def blog_review_notification(message, sender, old, new):
def blog_review_notification(message, sender, old, new, copy_to_staffs):
user_email = [new.author.email]
if new.coauthor.all():
user_email.extend([author.email for author in new.coauthor.all()])
Expand All @@ -233,4 +234,4 @@ def blog_review_notification(message, sender, old, new):
else:
email_url = None

review_notification(email_url, user_email, context, mail)
review_notification(email_url, user_email, context, mail, copy_to_staffs)
9 changes: 6 additions & 3 deletions lowfat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ def fund_review(request, fund_id):
formset.cleaned_data['email'],
request.user,
old_fund,
fund
fund,
not formset.cleaned_data['not_copy_email_field']
)
return HttpResponseRedirect(
reverse('fund_detail', args=[fund.id,])
Expand Down Expand Up @@ -515,7 +516,8 @@ def expense_review(request, expense_id):
formset.cleaned_data['email'],
request.user,
old_expense,
expense
expense,
not formset.cleaned_data['not_copy_email_field']
)
return HttpResponseRedirect(
reverse('expense_detail', args=[expense.id,])
Expand Down Expand Up @@ -651,7 +653,8 @@ def blog_review(request, blog_id):
formset.cleaned_data['email'],
request.user,
old_blog,
blog
blog,
not formset.cleaned_data['not_copy_email_field']
)
return HttpResponseRedirect(
reverse('blog_detail', args=[blog.id,])
Expand Down

0 comments on commit 8e06bcb

Please sign in to comment.