cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
mail.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.clm.utils.mail
22 # @author Piotr Wójcik
23 # @date 3.12.2010
24 #
25 
26 
27 from smtplib import SMTPRecipientsRefused
28 
29 from django.conf import settings
30 from django.core.mail.message import EmailMessage
31 from django.template import loader, Context
32 from django.utils.translation import ugettext_lazy as _
33 
34 from clm.models.user import User
35 from clm.utils import log
36 from clm.utils.exception import CLMException
37 
38 
39 ##
40 #
41 # Decorator for catching exception with sending emails' error.
42 #
43 # @par Decorated function's declaration
44 # @code
45 # @email_error
46 # function (request, *args, **kw)
47 # @endcode
48 #
49 # @par Decorated function's call
50 # @code
51 # function (request, *arg, **kw)
52 # @endcode
53 #
54 def email_error(f):
55  def wrap(request, *args, **kwds):
56  try:
57  return f(request, *args, **kwds)
58  except SMTPRecipientsRefused, e:
59  error = "%s %s" % (f.__name__, str(e))
60  log.error(0, error)
61  raise
62  return wrap
63 
64 
65 @email_error
66 ##
67 #
68 # @parameter{to_address,string} addressee of the email
69 # @parameter{msg_text,string} contents of the email
70 # @parameter{subject,string} subject of the email
71 #
72 # Sends email via STMP server.
73 #
74 def send(to_address, msg_text, subject):
75  from_address = settings.FROM_EMAIL
76  log.debug(0, '%s%s%s%s%s%s%s' % ("send_email(from='", from_address, "', to='", to_address, "', subject='", subject, "')"))
77 
78  msg = EmailMessage(subject, msg_text, from_address, [to_address])
79  msg.send()
80 
81 
82 ##
83 #
84 # @parameter{activation_key,string} activation key to be sent
85 # @parameter{user,string} username of the user to activate
86 # @parameter{wi_data}
87 #
88 # Sends email with activation key to registred user.
89 #
90 def send_activation_email(activation_key, user, wi_data):
91  ctx_dict = {'activation_key': activation_key,
92  'site': wi_data['site_domain'],
93  'site_name': wi_data['site_name']}
94 
95  subject = render_from_template_to_string('registration/activation_email_subject.txt', ctx_dict)
96  subject = ''.join(subject.splitlines())
97  message = render_from_template_to_string('registration/activation_email.txt', ctx_dict)
98 
99  send(user.email, message, subject)
100 
101 
102 ##
103 #
104 # @parameter{user,string} username of the user to activate
105 # @parameter{wi_data,dict}, \n fields:
106 # @dictkey{site_domain,string}
107 # @dictkey{site_name,string}
108 #
109 # Sends confirmation email to user as admin confirms user's activation.
110 #
112  ctx_dict = {'site': wi_data['site_domain'],
113  'site_name': wi_data['site_name']}
114  subject = render_from_template_to_string('admin_clm/activation_email_subject.txt', ctx_dict)
115  subject = ''.join(subject.splitlines())
116  message = render_from_template_to_string('admin_clm/activation_email.txt', ctx_dict)
117 
118  send(user.email, message, subject)
119 
120 
121 ##
122 #
123 # @parameter{user}
124 # @parameter{wi_data,dict}, \n fields:
125 # @dictkey{site_name,string}
126 #
127 # Sends notification to admin about a newly registered user.
128 #
130  ctx_dict = {'site_name': wi_data['site_name']}
131  subject = render_from_template_to_string('registration/admin_notify_email_subject.txt', ctx_dict)
132  subject = ''.join(subject.splitlines())
133  message = render_from_template_to_string('registration/admin_notify_email.txt', user)
134 
135  for admin in User.objects.filter(is_superuser=True):
136  send(admin.email, message, subject)
137 
138 
139 ##
140 #
141 # @parameter{user,string} username of the user to reset password
142 # @parameter{token,string}
143 # @parameter{wi_data,dict}, \n fields:
144 # @dictkey{site_domain,string}
145 # @dictkey{site_name,string}
146 #
147 # Sends mail for password reset.
148 #
149 def send_reset_password_mail(user, token, wi_data):
150  ctx_dict = {'site_name': wi_data['site_name'],
151  'domain': wi_data['site_domain'],
152  'username': user.login,
153  'token': token}
154  message = render_from_template_to_string('account/password_reset_email.txt', ctx_dict)
155 
156  send(user.email, message, _("Password reset on %s") % wi_data['site_name'])
157 
158 
159 ##
160 #
161 # @parameter{user,string} username of the user to reset password
162 # @parameter{block,boolean} whether to block or unblock.
163 # @parameter{wi_data,dict}, \n fields:
164 # @dictkey{site_name,string}
165 #
166 def send_block_email(user, block, wi_data):
167  ctx_dict = {}
168  if block:
169  send(user.email,
170  render_from_template_to_string('account/block_email.txt', ctx_dict),
171  _("User account blocked on %s") % wi_data['site_name'])
172  else:
173  send(user.email,
174  render_from_template_to_string('account/unblock_email.txt', ctx_dict),
175  _("User account unblocked on %s") % wi_data['site_name'])
176 
177 
178 ##
179 #
180 # @parameter{template_filename,string} path to template of the email
181 # @parameter{ctx_dict,dict} params to be filled in the email
182 #
183 # Renders strings which can be sent as email contents (basing on template and
184 # data to be filled in).
185 #
186 # @raises{clm_template_create,CLMException}
187 #
188 def render_from_template_to_string(template_filename, ctx_dict={}):
189  try:
190  template = loader.get_template(template_filename)
191  except Exception, e:
192  log.error(0, "Cannot load template. Error: %s" % str(e))
193  raise CLMException('clm_template_create')
194 
195  return template.render(Context(ctx_dict))
196