root / snf-astakos-app / astakos / im / functions.py @ 49790d9d
History | View | Annotate | Download (8.6 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import logging |
35 |
import socket |
36 |
|
37 |
from django.utils.translation import ugettext as _ |
38 |
from django.template.loader import render_to_string |
39 |
from django.core.mail import send_mail |
40 |
from django.core.urlresolvers import reverse |
41 |
from django.core.exceptions import ValidationError |
42 |
from django.template import Context, loader |
43 |
|
44 |
from urllib import quote |
45 |
from urlparse import urljoin |
46 |
from smtplib import SMTPException |
47 |
|
48 |
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL |
49 |
from astakos.im.models import Invitation, AstakosUser |
50 |
|
51 |
logger = logging.getLogger(__name__) |
52 |
|
53 |
def send_verification(user, template_name='im/activation_email.txt'): |
54 |
"""
|
55 |
Send email to user to verify his/her email and activate his/her account.
|
56 |
|
57 |
Raises SendVerificationError
|
58 |
"""
|
59 |
url = '%s?auth=%s&next=%s' % (urljoin(BASEURL, reverse('astakos.im.views.activate')), |
60 |
quote(user.auth_token), |
61 |
quote(BASEURL)) |
62 |
message = render_to_string(template_name, { |
63 |
'user': user,
|
64 |
'url': url,
|
65 |
'baseurl': BASEURL,
|
66 |
'site_name': SITENAME,
|
67 |
'support': DEFAULT_CONTACT_EMAIL})
|
68 |
sender = DEFAULT_FROM_EMAIL |
69 |
try:
|
70 |
send_mail('%s alpha2 testing account activation is needed' % SITENAME, message, sender, [user.email])
|
71 |
except (SMTPException, socket.error) as e: |
72 |
logger.exception(e) |
73 |
raise SendVerificationError()
|
74 |
else:
|
75 |
logger.info('Sent activation %s', user)
|
76 |
|
77 |
def send_admin_notification(user, template_name='im/admin_notification.txt'): |
78 |
"""
|
79 |
Send email to DEFAULT_ADMIN_EMAIL to notify for a new user registration.
|
80 |
|
81 |
Raises SendNotificationError
|
82 |
"""
|
83 |
if not DEFAULT_ADMIN_EMAIL: |
84 |
return
|
85 |
message = render_to_string(template_name, { |
86 |
'user': user,
|
87 |
'baseurl': BASEURL,
|
88 |
'site_name': SITENAME,
|
89 |
'support': DEFAULT_CONTACT_EMAIL})
|
90 |
sender = DEFAULT_FROM_EMAIL |
91 |
try:
|
92 |
send_mail('%s alpha2 testing account notification' % SITENAME, message, sender, [DEFAULT_ADMIN_EMAIL])
|
93 |
except (SMTPException, socket.error) as e: |
94 |
logger.exception(e) |
95 |
raise SendNotificationError()
|
96 |
else:
|
97 |
logger.info('Sent admin notification for user %s', user)
|
98 |
|
99 |
def send_invitation(invitation, template_name='im/invitation.txt'): |
100 |
"""
|
101 |
Send invitation email.
|
102 |
|
103 |
Raises SendInvitationError
|
104 |
"""
|
105 |
subject = _('Invitation to %s alpha2 testing' % SITENAME)
|
106 |
url = '%s?code=%d' % (urljoin(BASEURL, reverse('astakos.im.views.index')), invitation.code) |
107 |
message = render_to_string('im/invitation.txt', {
|
108 |
'invitation': invitation,
|
109 |
'url': url,
|
110 |
'baseurl': BASEURL,
|
111 |
'site_name': SITENAME,
|
112 |
'support': DEFAULT_CONTACT_EMAIL})
|
113 |
sender = DEFAULT_FROM_EMAIL |
114 |
try:
|
115 |
send_mail(subject, message, sender, [invitation.username]) |
116 |
except (SMTPException, socket.error) as e: |
117 |
logger.exception(e) |
118 |
raise SendInvitationError()
|
119 |
else:
|
120 |
logger.info('Sent invitation %s', invitation)
|
121 |
|
122 |
def send_greeting(user, email_template_name='im/welcome_email.txt'): |
123 |
"""
|
124 |
Send welcome email.
|
125 |
|
126 |
Raises SMTPException, socket.error
|
127 |
"""
|
128 |
subject = _('Welcome to %s alpha2 testing' % SITENAME)
|
129 |
message = render_to_string(email_template_name, { |
130 |
'user': user,
|
131 |
'url': urljoin(BASEURL, reverse('astakos.im.views.index')), |
132 |
'baseurl': BASEURL,
|
133 |
'site_name': SITENAME,
|
134 |
'support': DEFAULT_CONTACT_EMAIL})
|
135 |
sender = DEFAULT_FROM_EMAIL |
136 |
try:
|
137 |
send_mail(subject, message, sender, [user.email]) |
138 |
except (SMTPException, socket.error) as e: |
139 |
logger.exception(e) |
140 |
raise SendGreetingError()
|
141 |
else:
|
142 |
logger.info('Sent greeting %s', user)
|
143 |
|
144 |
def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'): |
145 |
subject = _("Feedback from %s alpha2 testing" % SITENAME)
|
146 |
from_email = user.email |
147 |
recipient_list = [DEFAULT_CONTACT_EMAIL] |
148 |
content = render_to_string(email_template_name, { |
149 |
'message': msg,
|
150 |
'data': data,
|
151 |
'user': user})
|
152 |
try:
|
153 |
send_mail(subject, content, from_email, recipient_list) |
154 |
except (SMTPException, socket.error) as e: |
155 |
logger.exception(e) |
156 |
raise SendFeedbackError()
|
157 |
else:
|
158 |
logger.info('Sent feedback from %s', user.email)
|
159 |
|
160 |
def send_change_email(ec, request, email_template_name='registration/email_change_email.txt'): |
161 |
try:
|
162 |
url = reverse('email_change_confirm',
|
163 |
kwargs={'activation_key':ec.activation_key})
|
164 |
url = request.build_absolute_uri(url) |
165 |
t = loader.get_template(email_template_name) |
166 |
c = {'url': url, 'site_name': SITENAME} |
167 |
from_email = DEFAULT_FROM_EMAIL |
168 |
send_mail(_("Email change on %s alpha2 testing") % SITENAME,
|
169 |
t.render(Context(c)), from_email, [ec.new_email_address]) |
170 |
except (SMTPException, socket.error) as e: |
171 |
logger.exception(e) |
172 |
raise ChangeEmailError()
|
173 |
else:
|
174 |
logger.info('Sent change email for %s', ec.user.email)
|
175 |
|
176 |
def activate(user, email_template_name='im/welcome_email.txt'): |
177 |
"""
|
178 |
Activates the specific user and sends email.
|
179 |
|
180 |
Raises SendGreetingError, ValidationError
|
181 |
"""
|
182 |
user.is_active = True
|
183 |
user.save() |
184 |
send_greeting(user, email_template_name) |
185 |
|
186 |
def invite(invitation, inviter, email_template_name='im/welcome_email.txt'): |
187 |
"""
|
188 |
Send an invitation email and upon success reduces inviter's invitation by one.
|
189 |
|
190 |
Raises SendInvitationError
|
191 |
"""
|
192 |
invitation.inviter = inviter |
193 |
invitation.save() |
194 |
send_invitation(invitation, email_template_name) |
195 |
inviter.invitations = max(0, inviter.invitations - 1) |
196 |
inviter.save() |
197 |
|
198 |
def set_user_credibility(email, has_credits): |
199 |
try:
|
200 |
user = AstakosUser.objects.get(email=email, is_active=True)
|
201 |
user.has_credits = has_credits |
202 |
user.save() |
203 |
except AstakosUser.DoesNotExist, e:
|
204 |
logger.exception(e) |
205 |
except ValidationError, e:
|
206 |
logger.exception(e) |
207 |
|
208 |
class SendMailError(Exception): |
209 |
pass
|
210 |
|
211 |
class SendAdminNotificationError(SendMailError): |
212 |
def __init__(self): |
213 |
self.message = _('Failed to send notification') |
214 |
super(SendAdminNotificationError, self).__init__() |
215 |
|
216 |
class SendVerificationError(SendMailError): |
217 |
def __init__(self): |
218 |
self.message = _('Failed to send verification') |
219 |
super(SendVerificationError, self).__init__() |
220 |
|
221 |
class SendInvitationError(SendMailError): |
222 |
def __init__(self): |
223 |
self.message = _('Failed to send invitation') |
224 |
super(SendInvitationError, self).__init__() |
225 |
|
226 |
class SendGreetingError(SendMailError): |
227 |
def __init__(self): |
228 |
self.message = _('Failed to send greeting') |
229 |
super(SendGreetingError, self).__init__() |
230 |
|
231 |
class SendFeedbackError(SendMailError): |
232 |
def __init__(self): |
233 |
self.message = _('Failed to send feedback') |
234 |
super(SendFeedbackError, self).__init__() |
235 |
|
236 |
class ChangeEmailError(SendMailError): |
237 |
def __init__(self): |
238 |
self.message = _('Failed to send change email') |
239 |
super(ChangeEmailError, self).__init__() |