root / snf-astakos-app / astakos / im / functions.py @ 63fa03fe
History | View | Annotate | Download (7.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 urllib import quote |
42 |
from urlparse import urljoin |
43 |
from smtplib import SMTPException |
44 |
|
45 |
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL |
46 |
from astakos.im.models import Invitation, AstakosUser |
47 |
|
48 |
logger = logging.getLogger(__name__) |
49 |
|
50 |
def send_verification(user, template_name='im/activation_email.txt'): |
51 |
"""
|
52 |
Send email to user to verify his/her email and activate his/her account.
|
53 |
|
54 |
Raises SendVerificationError
|
55 |
"""
|
56 |
url = '%s?auth=%s&next=%s' % (urljoin(BASEURL, reverse('astakos.im.views.activate')), |
57 |
quote(user.auth_token), |
58 |
quote(BASEURL)) |
59 |
message = render_to_string(template_name, { |
60 |
'user': user,
|
61 |
'url': url,
|
62 |
'baseurl': BASEURL,
|
63 |
'site_name': SITENAME,
|
64 |
'support': DEFAULT_CONTACT_EMAIL})
|
65 |
sender = DEFAULT_FROM_EMAIL |
66 |
try:
|
67 |
send_mail('%s alpha2 testing account activation is needed' % SITENAME, message, sender, [user.email])
|
68 |
except (SMTPException, socket.error) as e: |
69 |
logger.exception(e) |
70 |
raise SendVerificationError()
|
71 |
else:
|
72 |
logger.info('Sent activation %s', user)
|
73 |
|
74 |
def send_admin_notification(user, template_name='im/admin_notification.txt'): |
75 |
"""
|
76 |
Send email to DEFAULT_ADMIN_EMAIL to notify for a new user registration.
|
77 |
|
78 |
Raises SendNotificationError
|
79 |
"""
|
80 |
if not DEFAULT_ADMIN_EMAIL: |
81 |
return
|
82 |
message = render_to_string(template_name, { |
83 |
'user': user,
|
84 |
'baseurl': BASEURL,
|
85 |
'site_name': SITENAME,
|
86 |
'support': DEFAULT_CONTACT_EMAIL})
|
87 |
sender = DEFAULT_FROM_EMAIL |
88 |
try:
|
89 |
send_mail('%s alpha2 testing account notification' % SITENAME, message, sender, [DEFAULT_ADMIN_EMAIL])
|
90 |
except (SMTPException, socket.error) as e: |
91 |
logger.exception(e) |
92 |
raise SendNotificationError()
|
93 |
else:
|
94 |
logger.info('Sent admin notification for user %s', user)
|
95 |
|
96 |
def send_invitation(invitation, template_name='im/invitation.txt'): |
97 |
"""
|
98 |
Send invitation email.
|
99 |
|
100 |
Raises SendInvitationError
|
101 |
"""
|
102 |
subject = _('Invitation to %s alpha2 testing' % SITENAME)
|
103 |
url = '%s?code=%d' % (urljoin(BASEURL, reverse('astakos.im.views.index')), invitation.code) |
104 |
message = render_to_string('im/invitation.txt', {
|
105 |
'invitation': invitation,
|
106 |
'url': url,
|
107 |
'baseurl': BASEURL,
|
108 |
'site_name': SITENAME,
|
109 |
'support': DEFAULT_CONTACT_EMAIL})
|
110 |
sender = DEFAULT_FROM_EMAIL |
111 |
try:
|
112 |
send_mail(subject, message, sender, [invitation.username]) |
113 |
except (SMTPException, socket.error) as e: |
114 |
logger.exception(e) |
115 |
raise SendInvitationError()
|
116 |
else:
|
117 |
logger.info('Sent invitation %s', invitation)
|
118 |
|
119 |
def send_greeting(user, email_template_name='im/welcome_email.txt'): |
120 |
"""
|
121 |
Send welcome email.
|
122 |
|
123 |
Raises SMTPException, socket.error
|
124 |
"""
|
125 |
subject = _('Welcome to %s alpha2 testing' % SITENAME)
|
126 |
message = render_to_string(email_template_name, { |
127 |
'user': user,
|
128 |
'url': urljoin(BASEURL, reverse('astakos.im.views.index')), |
129 |
'baseurl': BASEURL,
|
130 |
'site_name': SITENAME,
|
131 |
'support': DEFAULT_CONTACT_EMAIL})
|
132 |
sender = DEFAULT_FROM_EMAIL |
133 |
try:
|
134 |
send_mail(subject, message, sender, [user.email]) |
135 |
except (SMTPException, socket.error) as e: |
136 |
logger.exception(e) |
137 |
raise SendGreetingError()
|
138 |
else:
|
139 |
logger.info('Sent greeting %s', user)
|
140 |
|
141 |
def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'): |
142 |
subject = _("Feedback from %s alpha2 testing" % SITENAME)
|
143 |
from_email = user.email |
144 |
recipient_list = [DEFAULT_CONTACT_EMAIL] |
145 |
content = render_to_string(email_template_name, { |
146 |
'message': msg,
|
147 |
'data': data,
|
148 |
'user': user})
|
149 |
try:
|
150 |
send_mail(subject, content, from_email, recipient_list) |
151 |
except (SMTPException, socket.error) as e: |
152 |
logger.exception(e) |
153 |
raise SendFeedbackError()
|
154 |
else:
|
155 |
logger.info('Sent feedback from %s', user.email)
|
156 |
|
157 |
def activate(user, email_template_name='im/welcome_email.txt'): |
158 |
"""
|
159 |
Activates the specific user and sends email.
|
160 |
|
161 |
Raises SendGreetingError
|
162 |
"""
|
163 |
user.is_active = True
|
164 |
user.save() |
165 |
send_greeting(user, email_template_name) |
166 |
|
167 |
def invite(invitation, inviter, email_template_name='im/welcome_email.txt'): |
168 |
"""
|
169 |
Send an invitation email and upon success reduces inviter's invitation by one.
|
170 |
|
171 |
Raises SendInvitationError
|
172 |
"""
|
173 |
invitation.inviter = inviter |
174 |
invitation.save() |
175 |
send_invitation(invitation, email_template_name) |
176 |
inviter.invitations = max(0, inviter.invitations - 1) |
177 |
inviter.save() |
178 |
|
179 |
def set_user_credibility(email, has_credits): |
180 |
try:
|
181 |
user = AstakosUser.objects.get(email=email) |
182 |
user.has_credits = has_credits |
183 |
user.save() |
184 |
except AstakosUser.DoesNotExist, e:
|
185 |
logger.exception(e) |
186 |
|
187 |
class SendMailError(Exception): |
188 |
pass
|
189 |
|
190 |
class SendAdminNotificationError(SendMailError): |
191 |
def __init__(self): |
192 |
self.message = _('Failed to send notification') |
193 |
super(SendAdminNotificationError, self).__init__() |
194 |
|
195 |
class SendVerificationError(SendMailError): |
196 |
def __init__(self): |
197 |
self.message = _('Failed to send verification') |
198 |
super(SendVerificationError, self).__init__() |
199 |
|
200 |
class SendInvitationError(SendMailError): |
201 |
def __init__(self): |
202 |
self.message = _('Failed to send invitation') |
203 |
super(SendInvitationError, self).__init__() |
204 |
|
205 |
class SendGreetingError(SendMailError): |
206 |
def __init__(self): |
207 |
self.message = _('Failed to send greeting') |
208 |
super(SendGreetingError, self).__init__() |
209 |
|
210 |
class SendFeedbackError(SendMailError): |
211 |
def __init__(self): |
212 |
self.message = _('Failed to send feedback') |
213 |
super(SendFeedbackError, self).__init__() |