From 9d8456518b964d5c7fe681f217bb98ea9a396b90 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jordan=20Aug=C3=A9?= Date: Fri, 7 Mar 2014 11:57:35 +0100 Subject: [PATCH] Emails: use templates for all emails - To be tested ! --- portal/accountview.py | 1 - portal/actions.py | 85 ++++++++++--------- portal/contactview.py | 37 ++++++-- portal/django_passresetview.py | 1 - portal/forms.py | 47 +++++----- portal/joinview.py | 70 ++++++++++----- portal/manageuserview.py | 1 - portal/models.py | 1 - portal/registrationview.py | 1 - portal/templates/authority_request_email.html | 27 ++++++ portal/templates/authority_request_email.txt | 27 ++++++ .../authority_request_email_subject.txt | 1 + portal/templates/contact_support_email.txt | 12 +++ .../contact_support_email_subject.txt | 1 + 14 files changed, 219 insertions(+), 93 deletions(-) create mode 100644 portal/templates/authority_request_email.html create mode 100644 portal/templates/authority_request_email.txt create mode 100644 portal/templates/authority_request_email_subject.txt create mode 100644 portal/templates/contact_support_email.txt create mode 100644 portal/templates/contact_support_email_subject.txt diff --git a/portal/accountview.py b/portal/accountview.py index 5d3c9f83..94adaf41 100644 --- a/portal/accountview.py +++ b/portal/accountview.py @@ -10,7 +10,6 @@ from ui.topmenu import topmenu_items_live, the_user from django.http import HttpResponse, HttpResponseRedirect from django.contrib import messages from django.contrib.auth.decorators import login_required -from django.core.mail import send_mail from theme import ThemeView diff --git a/portal/actions.py b/portal/actions.py index e39d4805..7d7a3374 100644 --- a/portal/actions.py +++ b/portal/actions.py @@ -6,7 +6,7 @@ import json from django.contrib.auth.models import User from django.template.loader import render_to_string -from django.core.mail import send_mail,EmailMultiAlternatives +from django.core.mail import EmailMultiAlternatives from theme import ThemeView @@ -399,25 +399,27 @@ def create_pending_slice(wsgi_request, request, email): ) s.save() - # Send an email: the recipients are the PI of the authority - recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn']) - - theme.template_name = 'slice_request_email.txt' - text_content = render_to_string(theme.template, request) - - theme.template_name = 'slice_request_email.html' - html_content = render_to_string(theme.template, request) - - theme.template_name = 'slice_request_email_subject.txt' - subject = render_to_string(theme.template, request) - subject = subject.replace('\n', '') - - sender = email - msg = EmailMultiAlternatives(subject, text_content, sender, [recipients]) - print msg - msg.attach_alternative(html_content, "text/html") - msg.send() - #send_mail(subject, msg, email, recipients) + try: + # Send an email: the recipients are the PI of the authority + recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn']) + + theme.template_name = 'slice_request_email.txt' + text_content = render_to_string(theme.template, request) + + theme.template_name = 'slice_request_email.html' + html_content = render_to_string(theme.template, request) + + theme.template_name = 'slice_request_email_subject.txt' + subject = render_to_string(theme.template, request) + subject = subject.replace('\n', '') + + sender = email + msg = EmailMultiAlternatives(subject, text_content, sender, [recipients]) + print msg + msg.attach_alternative(html_content, "text/html") + msg.send() + except Exception, e: + print "Failed to send email, please check the mail templates and the SMTP configuration of your server" #------------------------------------------------------------------------------- # REQUESTS - Users @@ -572,24 +574,27 @@ def create_pending_user(wsgi_request, request, user_detail): except Exception, e: print "Failed creating manifold account on platform %s for user: %s" % ('myslice', request['email']) - # Send an email: the recipients are the PI of the authority - # If No PI is defined for this Authority, send to a default email (different for each theme) - recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn']) + try: + # Send an email: the recipients are the PI of the authority + # If No PI is defined for this Authority, send to a default email (different for each theme) + recipients = authority_get_pi_emails(wsgi_request, request['authority_hrn']) + + theme.template_name = 'user_request_email.html' + html_content = render_to_string(theme.template, request) - theme.template_name = 'user_request_email.html' - html_content = render_to_string(theme.template, request) - - theme.template_name = 'user_request_email.txt' - text_content = render_to_string(theme.template, request) - - theme.template_name = 'user_request_email_subject.txt' - subject = render_to_string(theme.template, request) - subject = subject.replace('\n', '') - - theme.template_name = 'email_default_sender.txt' - sender = render_to_string(theme.template, request) - sender = sender.replace('\n', '') - - msg = EmailMultiAlternatives(subject, text_content, sender, [recipients]) - msg.attach_alternative(html_content, "text/html") - msg.send() + theme.template_name = 'user_request_email.txt' + text_content = render_to_string(theme.template, request) + + theme.template_name = 'user_request_email_subject.txt' + subject = render_to_string(theme.template, request) + subject = subject.replace('\n', '') + + theme.template_name = 'email_default_sender.txt' + sender = render_to_string(theme.template, request) + sender = sender.replace('\n', '') + + msg = EmailMultiAlternatives(subject, text_content, sender, [recipients]) + msg.attach_alternative(html_content, "text/html") + msg.send() + except Exception, e: + print "Failed to send email, please check the mail templates and the SMTP configuration of your server" diff --git a/portal/contactview.py b/portal/contactview.py index 63b4bb0b..13450dfd 100644 --- a/portal/contactview.py +++ b/portal/contactview.py @@ -1,7 +1,6 @@ from django.shortcuts import render from django.template.loader import render_to_string from django.views.generic import View -from django.core.mail import send_mail from unfold.loginrequired import FreeAccessView from ui.topmenu import topmenu_items, the_user @@ -26,13 +25,37 @@ class ContactView (FreeAccessView, ThemeView): email = form.cleaned_data['email'] # email of the sender cc_myself = form.cleaned_data['cc_myself'] - #recipients = authority_get_pi_emails(authority_hrn) - recipients = ['support@myslice.info' ] - if cc_myself: - recipients.append(email) + try: + # Send an email: the support recipients + theme.template_name = 'email_support.txt' + recipients = render_to_string(theme.template, form.cleaned_data) + recipients = subject.replace('\n', '') + if cc_myself: + recipients.append(email) + + theme.template_name = 'contact_support_email.html' + html_content = render_to_string(theme.template, form.cleaned_data) + + theme.template_name = 'contact_support_email.txt' + text_content = render_to_string(theme.template, form.cleaned_data) + + theme.template_name = 'contact_support_email_subject.txt' + subject = render_to_string(theme.template, form.cleaned_data) + subject = subject.replace('\n', '') + + if not email: + theme.template_name = 'email_default_sender.txt' + sender = render_to_string(theme.template, form.cleaned_data) + sender = sender.replace('\n', '') + else: + sender = email + + msg = EmailMultiAlternatives(subject, text_content, sender, [recipients]) + msg.attach_alternative(html_content, "text/html") + msg.send() + except Exception, e: + print "Failed to send email, please check the mail templates and the SMTP configuration of your server" - msg = render_to_string('contact-support-email.txt', form.cleaned_data) - send_mail("Onelab user %s submitted a query "%email, msg, email, recipients) if request.user.is_authenticated() : username = request.user.email else : diff --git a/portal/django_passresetview.py b/portal/django_passresetview.py index d931ccf1..7d7f52e9 100644 --- a/portal/django_passresetview.py +++ b/portal/django_passresetview.py @@ -72,7 +72,6 @@ import json from random import choice -from django.core.mail import send_mail from django.contrib import messages from django.views.generic import View from django.shortcuts import render diff --git a/portal/forms.py b/portal/forms.py index 61ffb860..cf3e172a 100644 --- a/portal/forms.py +++ b/portal/forms.py @@ -170,28 +170,31 @@ class PasswordResetForm(forms.Form): Generates a one-use only link for resetting password and sends to the user. """ - from django.core.mail import send_mail - for user in self.users_cache: - if not domain_override: - current_site = get_current_site(request) - site_name = current_site.name - domain = current_site.domain - else: - site_name = domain = domain_override - c = { - 'email': user.email, - 'domain': domain, - 'site_name': site_name, - 'uid': int_to_base36(user.pk), - 'user': user, - 'token': token_generator.make_token(user), - 'protocol': use_https and 'https' or 'http', - } - subject = loader.render_to_string(subject_template_name, c) - # Email subject *must not* contain newlines - subject = ''.join(subject.splitlines()) - email = loader.render_to_string(email_template_name, c) - send_mail(subject, email, from_email, [user.email]) + from django.core.mail import send_mail,EmailMultiAlternatives + try: + for user in self.users_cache: + if not domain_override: + current_site = get_current_site(request) + site_name = current_site.name + domain = current_site.domain + else: + site_name = domain = domain_override + c = { + 'email': user.email, + 'domain': domain, + 'site_name': site_name, + 'uid': int_to_base36(user.pk), + 'user': user, + 'token': token_generator.make_token(user), + 'protocol': use_https and 'https' or 'http', + } + subject = loader.render_to_string(subject_template_name, c) + # Email subject *must not* contain newlines + subject = ''.join(subject.splitlines()) + email = loader.render_to_string(email_template_name, c) + send_mail(subject, email, from_email, [user.email]) + except Exception, e: + print "Failed to send email, please check the mail templates and the SMTP configuration of your server" class SetPasswordForm(forms.Form): diff --git a/portal/joinview.py b/portal/joinview.py index caea6d28..abe73f39 100644 --- a/portal/joinview.py +++ b/portal/joinview.py @@ -2,7 +2,7 @@ import os.path, re import json from random import randint -from django.core.mail import send_mail +from django.core.mail import EmailMultiAlternatives from django.contrib.auth.models import User from django.views.generic import View from django.template.loader import render_to_string @@ -161,25 +161,57 @@ class JoinView (FreeAccessView, ThemeView): user_id = user_detail['user_id']+1 # the user_id for the newly created user in local:user account_params = {'platform_id': 5, 'user_id': user_id, 'auth_type': auth_type, 'config': account_config} manifold_add_account(request,account_params) - + # Send email - ctx = { - 'first_name' : reg_fname, - 'last_name' : reg_lname, - 'authority_hrn' : reg_auth, - 'email' : reg_email, - 'user_hrn' : user_hrn, - 'public_key' : public_key, - } - recipients = authority_get_pi_emails(request,reg_auth) - - # We don't need to send this email to user. - # it's for the PI only - #if ctx['cc_myself']: - # recipients.append(ctx['email']) - - msg = render_to_string('user_request_email.txt', ctx) - send_mail("Onelab New Authority request for %s submitted"%reg_email, msg, 'support@myslice.info', recipients) + try: + ctx = { + 'site_name' : reg_site_name, + 'authority_hrn' : reg_root_authority_hrn + '.' + reg_site_authority, + 'site_abbreviated_name' : reg_site_abbreviated_name, + 'site_url' : reg_site_url, + 'site_latitude' : reg_site_latitude, + 'site_longitude' : reg_site_longitude, + 'address_line1' : reg_address_line1, + 'address_line2' : reg_address_line2, + 'address_line3' : reg_address_line3, + 'address_city' : reg_address_city, + 'address_postalcode' : reg_address_postalcode, + 'address_state' : reg_address_state, + 'address_country' : reg_address_country, + 'first_name' : reg_fname, + 'last_name' : reg_lname, + 'authority_hrn' : reg_auth, + 'email' : reg_email, + 'user_hrn' : user_hrn, + 'public_key' : public_key, + } + recipients = authority_get_pi_emails(request,reg_auth) + + # We don't need to send this email to user. + # it's for the PI only + #if ctx['cc_myself']: + # recipients.append(ctx['email']) + theme.template_name = 'authority_request_email.html' + html_content = render_to_string(theme.template, ctx) + + theme.template_name = 'authority_request_email.txt' + text_content = render_to_string(theme.template, ctx) + + theme.template_name = 'authority_request_email_subject.txt' + subject = render_to_string(theme.template, ctx) + subject = subject.replace('\n', '') + + theme.template_name = 'email_default_sender.txt' + sender = render_to_string(theme.template, ctx) + sender = sender.replace('\n', '') + + msg = EmailMultiAlternatives(subject, text_content, sender, [recipients]) + msg.attach_alternative(html_content, "text/html") + msg.send() + + except Exception, e: + print "Failed to send email, please check the mail templates and the SMTP configuration of your server" + return render(request, 'user_register_complete.html') template_env = { diff --git a/portal/manageuserview.py b/portal/manageuserview.py index 4f5c72ff..5b8d2dba 100644 --- a/portal/manageuserview.py +++ b/portal/manageuserview.py @@ -10,7 +10,6 @@ from ui.topmenu import topmenu_items_live, the_user from django.http import HttpResponse, HttpResponseRedirect from django.contrib import messages from django.contrib.auth.decorators import login_required -from django.core.mail import send_mail from theme import ThemeView # import json, os, re, itertools diff --git a/portal/models.py b/portal/models.py index 0ea02823..3fa0383e 100644 --- a/portal/models.py +++ b/portal/models.py @@ -26,7 +26,6 @@ import random import re from django.conf import settings -from django.core.mail import send_mail from django.db import models from django.db import transaction from django.utils.translation import ugettext_lazy as _ diff --git a/portal/registrationview.py b/portal/registrationview.py index 44bcdd6e..9f5f39f4 100644 --- a/portal/registrationview.py +++ b/portal/registrationview.py @@ -2,7 +2,6 @@ import os.path, re import json from random import randint -from django.core.mail import send_mail from django.views.generic import View from django.template.loader import render_to_string from django.shortcuts import render diff --git a/portal/templates/authority_request_email.html b/portal/templates/authority_request_email.html new file mode 100644 index 00000000..51b7c401 --- /dev/null +++ b/portal/templates/authority_request_email.html @@ -0,0 +1,27 @@ + +
+

NEW AUTHORITY REQUEST

+
+

{{site_name}}

+Authority hrn : {{authority_hrn}} +Abreviated name : {{site_abbreviated_name}} +URL : {{site_url}} +latitude : {{site_latitude}} +longitude : {{site_longitude}} +Address : {{address_line1}}
+ {{address_line2}}
+ {{address_line3}}
+City : {{address_city}} +Postcode : {{address_postalcode}} +State : {{address_state}} +Country : {{address_country}} + +

+ +

PI for this Authority

+First Name : {{first_name}} +Last Name : {{last_name}} +Authority hrn: {{authority_hrn}} +Public key : {{public_key}} +Email : {{email}} +User Hrn : {{user_hrn}} diff --git a/portal/templates/authority_request_email.txt b/portal/templates/authority_request_email.txt new file mode 100644 index 00000000..25eb21b5 --- /dev/null +++ b/portal/templates/authority_request_email.txt @@ -0,0 +1,27 @@ +NEW AUTHORITY REQUEST + +Site : {{site_name}} + +Authority hrn : {{authority_hrn}} +Abreviated name : {{site_abbreviated_name}} +URL : {{site_url}} + +latitude : {{site_latitude}} +longitude : {{site_longitude}} + +Address : {{address_line1}} + {{address_line2}} + {{address_line3}} +City : {{address_city}} +Postcode : {{address_postalcode}} +State : {{address_state}} +Country : {{address_country}} + + +PI for this Authority +First Name : {{first_name}} +Last Name : {{last_name}} +Authority hrn: {{authority_hrn}} +Public key : {{public_key}} +Email : {{email}} +User Hrn : {{user_hrn}} diff --git a/portal/templates/authority_request_email_subject.txt b/portal/templates/authority_request_email_subject.txt new file mode 100644 index 00000000..226af29f --- /dev/null +++ b/portal/templates/authority_request_email_subject.txt @@ -0,0 +1 @@ +OneLab new Authority request submitted diff --git a/portal/templates/contact_support_email.txt b/portal/templates/contact_support_email.txt new file mode 100644 index 00000000..9eb576d6 --- /dev/null +++ b/portal/templates/contact_support_email.txt @@ -0,0 +1,12 @@ +User details: + +First name : {{first_name}} +Last name : {{last_name}} +email : {{email}} +Authority : {{authority}} + +Query Details: + +Subject : {{subject}} +Description : {{description}} + diff --git a/portal/templates/contact_support_email_subject.txt b/portal/templates/contact_support_email_subject.txt new file mode 100644 index 00000000..dd21e446 --- /dev/null +++ b/portal/templates/contact_support_email_subject.txt @@ -0,0 +1 @@ +OneLab support: user ticket -- 2.43.0