Revision 9a06d96f snf-astakos-app/astakos/im/functions.py
b/snf-astakos-app/astakos/im/functions.py | ||
---|---|---|
43 | 43 |
from django.contrib.auth import login as auth_login, logout as auth_logout |
44 | 44 |
from django.http import HttpRequest |
45 | 45 |
from django.conf import settings |
46 |
from django.contrib.auth.models import AnonymousUser |
|
46 | 47 |
|
47 | 48 |
from urllib import quote |
48 | 49 |
from urlparse import urljoin |
... | ... | |
52 | 53 |
|
53 | 54 |
from astakos.im.settings import (DEFAULT_CONTACT_EMAIL, SITENAME, BASEURL, |
54 | 55 |
LOGGING_LEVEL, VERIFICATION_EMAIL_SUBJECT, |
55 |
ADMIN_NOTIFICATION_EMAIL_SUBJECT, |
|
56 |
ACCOUNT_CREATION_SUBJECT, |
|
57 |
GROUP_CREATION_SUBJECT, |
|
56 | 58 |
HELPDESK_NOTIFICATION_EMAIL_SUBJECT, |
57 | 59 |
INVITATION_EMAIL_SUBJECT, |
58 | 60 |
GREETING_EMAIL_SUBJECT, |
59 | 61 |
FEEDBACK_EMAIL_SUBJECT, |
60 | 62 |
EMAIL_CHANGE_EMAIL_SUBJECT) |
61 |
from astakos.im.models import AstakosUser
|
|
63 |
import astakos.im.models
|
|
62 | 64 |
|
63 | 65 |
logger = logging.getLogger(__name__) |
64 | 66 |
|
... | ... | |
68 | 70 |
def with_logging(*args, **kwargs): |
69 | 71 |
email = '' |
70 | 72 |
user = None |
71 |
if len(args) == 2 and isinstance(args[1], AstakosUser): |
|
72 |
user = args[1] |
|
73 |
elif len(args) == 1 and isinstance(args[0], HttpRequest): |
|
73 |
try: |
|
74 | 74 |
request = args[0] |
75 |
user = request.user |
|
76 |
email = user.email if user and user.is_authenticated() else '' |
|
75 |
email = request.user.email |
|
76 |
except (KeyError, AttributeError), e: |
|
77 |
email = '' |
|
77 | 78 |
r = func(*args, **kwargs) |
78 | 79 |
if LOGGING_LEVEL: |
79 | 80 |
logger.log(LOGGING_LEVEL, msg % email) |
... | ... | |
116 | 117 |
user.save() |
117 | 118 |
|
118 | 119 |
|
119 |
def send_admin_notification(template_name, |
|
120 |
dictionary=None, |
|
121 |
subject='alpha2 testing notification', |
|
122 |
): |
|
120 |
def _send_admin_notification(template_name, |
|
121 |
dictionary=None, |
|
122 |
subject='alpha2 testing notification',): |
|
123 | 123 |
""" |
124 | 124 |
Send notification email to settings.ADMINS. |
125 | 125 |
|
... | ... | |
131 | 131 |
message = render_to_string(template_name, dictionary) |
132 | 132 |
sender = settings.SERVER_EMAIL |
133 | 133 |
try: |
134 |
send_mail(_(ADMIN_NOTIFICATION_EMAIL_SUBJECT) % {'user': user.email},
|
|
134 |
send_mail(subject,
|
|
135 | 135 |
message, sender, [i[1] for i in settings.ADMINS]) |
136 | 136 |
except (SMTPException, socket.error) as e: |
137 | 137 |
logger.exception(e) |
... | ... | |
141 | 141 |
logger.log(LOGGING_LEVEL, msg) |
142 | 142 |
|
143 | 143 |
|
144 |
def send_account_creation_notification(template_name, dictionary=None): |
|
145 |
user = dictionary.get('user', AnonymousUser()) |
|
146 |
subject = _(ACCOUNT_CREATION_SUBJECT) % {'user': user.email} |
|
147 |
return _send_admin_notification(template_name, dictionary, subject=subject) |
|
148 |
|
|
149 |
|
|
150 |
def send_group_creation_notification(template_name, dictionary=None): |
|
151 |
group = dictionary.get('group', astakos.im.models.AstakosGroup()) |
|
152 |
subject = _(GROUP_CREATION_SUBJECT) % {'group': group.name} |
|
153 |
return _send_admin_notification(template_name, dictionary, subject=subject) |
|
154 |
|
|
155 |
|
|
144 | 156 |
def send_helpdesk_notification(user, template_name='im/account_notification.txt'): |
145 | 157 |
""" |
146 | 158 |
Send email to DEFAULT_CONTACT_EMAIL to notify for a new user activation. |
... | ... | |
155 | 167 |
) |
156 | 168 |
sender = settings.SERVER_EMAIL |
157 | 169 |
try: |
158 |
send_mail(_(HELPDESK_NOTIFICATION_EMAIL_SUBJECT) % {'user': user.email}, |
|
159 |
message, sender, [DEFAULT_CONTACT_EMAIL]) |
|
170 |
send_mail( |
|
171 |
_(HELPDESK_NOTIFICATION_EMAIL_SUBJECT) % {'user': user.email}, |
|
172 |
message, sender, [DEFAULT_CONTACT_EMAIL]) |
|
160 | 173 |
except (SMTPException, socket.error) as e: |
161 | 174 |
logger.exception(e) |
162 | 175 |
raise SendNotificationError() |
... | ... | |
188 | 201 |
else: |
189 | 202 |
msg = 'Sent invitation %s' % invitation |
190 | 203 |
logger.log(LOGGING_LEVEL, msg) |
204 |
invitation.inviter.invitations = max(0, self.invitations - 1) |
|
205 |
invitation.inviter.save() |
|
191 | 206 |
|
192 | 207 |
|
193 | 208 |
def send_greeting(user, email_template_name='im/welcome_email.txt'): |
... | ... | |
241 | 256 |
c = {'url': url, 'site_name': SITENAME} |
242 | 257 |
from_email = settings.SERVER_EMAIL |
243 | 258 |
send_mail(_(EMAIL_CHANGE_EMAIL_SUBJECT), |
244 |
t.render(Context(c)), from_email, [ec.new_email_address]) |
|
259 |
t.render(Context(c)), from_email, [ec.new_email_address])
|
|
245 | 260 |
except (SMTPException, socket.error) as e: |
246 | 261 |
logger.exception(e) |
247 | 262 |
raise ChangeEmailError() |
... | ... | |
265 | 280 |
send_greeting(user, email_template_name) |
266 | 281 |
|
267 | 282 |
|
268 |
def switch_account_to_shibboleth(user, local_user, greeting_template_name='im/welcome_email.txt'): |
|
269 |
if not user or not isinstance(user, AstakosUser): |
|
270 |
return |
|
271 |
|
|
272 |
if not local_user or not isinstance(user, AstakosUser): |
|
273 |
return |
|
274 |
|
|
275 |
if not user.provider == 'shibboleth': |
|
283 |
def switch_account_to_shibboleth(user, local_user, |
|
284 |
greeting_template_name='im/welcome_email.txt'): |
|
285 |
try: |
|
286 |
provider = user.provider |
|
287 |
except AttributeError: |
|
276 | 288 |
return |
277 |
|
|
278 |
user.delete() |
|
279 |
local_user.provider = 'shibboleth' |
|
280 |
local_user.third_party_identifier = user.third_party_identifier |
|
281 |
local_user.save() |
|
282 |
send_greeting(local_user, greeting_template_name) |
|
283 |
return local_user |
|
284 |
|
|
285 |
|
|
286 |
def invite(invitation, inviter, email_template_name='im/welcome_email.txt'): |
|
287 |
""" |
|
288 |
Send an invitation email and upon success reduces inviter's invitation by one. |
|
289 |
|
|
290 |
Raises SendInvitationError |
|
291 |
""" |
|
292 |
invitation.inviter = inviter |
|
293 |
invitation.save() |
|
294 |
send_invitation(invitation, email_template_name) |
|
295 |
inviter.invitations = max(0, inviter.invitations - 1) |
|
296 |
inviter.save() |
|
289 |
else: |
|
290 |
if not provider == 'shibboleth': |
|
291 |
return |
|
292 |
user.delete() |
|
293 |
local_user.provider = 'shibboleth' |
|
294 |
local_user.third_party_identifier = user.third_party_identifier |
|
295 |
local_user.save() |
|
296 |
send_greeting(local_user, greeting_template_name) |
|
297 |
return local_user |
|
297 | 298 |
|
298 | 299 |
|
299 | 300 |
class SendMailError(Exception): |
Also available in: Unified diff