Revision e7cb4085 snf-astakos-app/astakos/im/functions.py
b/snf-astakos-app/astakos/im/functions.py | ||
---|---|---|
61 | 61 |
EMAIL_CHANGE_EMAIL_SUBJECT, |
62 | 62 |
PROJECT_CREATION_SUBJECT, PROJECT_APPROVED_SUBJECT, |
63 | 63 |
PROJECT_TERMINATION_SUBJECT, PROJECT_SUSPENSION_SUBJECT, |
64 |
PROJECT_MEMBERSHIP_CHANGE_SUBJECT, |
|
65 |
) |
|
64 |
PROJECT_MEMBERSHIP_CHANGE_SUBJECT) |
|
66 | 65 |
from astakos.im.notifications import build_notification, NotificationError |
67 | 66 |
from astakos.im.models import ( |
68 | 67 |
AstakosUser, Invitation, ProjectMembership, ProjectApplication, Project, |
69 | 68 |
UserSetting, |
70 | 69 |
get_resource_names, new_chain) |
71 |
from astakos.im.quotas import (qh_sync_user, qh_sync_project,
|
|
72 |
register_pending_apps) |
|
70 |
from astakos.im.quotas import (qh_sync_user, qh_sync_users,
|
|
71 |
register_pending_apps, qh_sync_project)
|
|
73 | 72 |
from astakos.im.project_notif import ( |
74 | 73 |
membership_change_notify, membership_enroll_notify, |
75 | 74 |
membership_request_notify, membership_leave_request_notify, |
... | ... | |
101 | 100 |
def send_verification(user, template_name='im/activation_email.txt'): |
102 | 101 |
""" |
103 | 102 |
Send email to user to verify his/her email and activate his/her account. |
104 |
|
|
105 |
Raises SendVerificationError |
|
106 | 103 |
""" |
107 |
url = '%s?auth=%s&next=%s' % (join_urls(BASEURL, reverse('activate')), |
|
108 |
quote(user.auth_token), |
|
109 |
quote(join_urls(BASEURL, reverse('index')))) |
|
104 |
url = join_urls(BASEURL, user.get_activation_url(nxt=reverse('index'))) |
|
110 | 105 |
message = render_to_string(template_name, { |
111 | 106 |
'user': user, |
112 | 107 |
'url': url, |
... | ... | |
114 | 109 |
'site_name': SITENAME, |
115 | 110 |
'support': CONTACT_EMAIL}) |
116 | 111 |
sender = settings.SERVER_EMAIL |
117 |
try: |
|
118 |
send_mail(_(VERIFICATION_EMAIL_SUBJECT), message, sender, [user.email], |
|
119 |
connection=get_connection()) |
|
120 |
|
|
121 |
except (SMTPException, socket.error) as e: |
|
122 |
logger.exception(e) |
|
123 |
raise SendVerificationError() |
|
124 |
else: |
|
125 |
msg = 'Sent activation %s' % user.email |
|
126 |
logger.log(LOGGING_LEVEL, msg) |
|
127 |
|
|
128 |
|
|
129 |
def send_activation(user, template_name='im/activation_email.txt'): |
|
130 |
send_verification(user, template_name) |
|
131 |
user.activation_sent = datetime.now() |
|
132 |
user.save() |
|
112 |
send_mail(_(VERIFICATION_EMAIL_SUBJECT), message, sender, [user.email], |
|
113 |
connection=get_connection()) |
|
114 |
logger.info("Sent user verirfication email: %s", user.log_display) |
|
133 | 115 |
|
134 | 116 |
|
135 | 117 |
def _send_admin_notification(template_name, |
136 |
dictionary=None, |
|
118 |
context=None, |
|
119 |
user=None, |
|
120 |
msg="", |
|
137 | 121 |
subject='alpha2 testing notification',): |
138 | 122 |
""" |
139 |
Send notification email to settings.HELPDESK + settings.MANAGERS. |
|
140 |
|
|
141 |
Raises SendNotificationError |
|
123 |
Send notification email to settings.HELPDESK + settings.MANAGERS + |
|
124 |
settings.ADMINS. |
|
142 | 125 |
""" |
143 |
dictionary = dictionary or {} |
|
144 |
message = render_to_string(template_name, dictionary) |
|
126 |
if context is None: |
|
127 |
context = {} |
|
128 |
if not 'user' in context: |
|
129 |
context['user'] = user |
|
130 |
|
|
131 |
message = render_to_string(template_name, context) |
|
145 | 132 |
sender = settings.SERVER_EMAIL |
146 |
recipient_list = [e[1] for e in settings.HELPDESK + settings.MANAGERS]
|
|
147 |
try:
|
|
148 |
send_mail(subject, message, sender, recipient_list,
|
|
149 |
connection=get_connection())
|
|
150 |
except (SMTPException, socket.error) as e:
|
|
151 |
logger.exception(e)
|
|
152 |
raise SendNotificationError()
|
|
133 |
recipient_list = [e[1] for e in settings.HELPDESK + |
|
134 |
settings.MANAGERS + settings.ADMINS]
|
|
135 |
send_mail(subject, message, sender, recipient_list, |
|
136 |
connection=get_connection()) |
|
137 |
if user:
|
|
138 |
msg = 'Sent admin notification (%s) for user %s' % (msg,
|
|
139 |
user.log_display)
|
|
153 | 140 |
else: |
154 |
user = dictionary.get('user')
|
|
155 |
msg = 'Sent admin notification for user %s' % user.log_display |
|
156 |
logger.log(LOGGING_LEVEL, msg)
|
|
141 |
msg = 'Sent admin notification (%s)' % msg
|
|
142 |
|
|
143 |
logger.log(LOGGING_LEVEL, msg) |
|
157 | 144 |
|
158 | 145 |
|
159 |
def send_account_creation_notification(template_name, dictionary=None): |
|
160 |
user = dictionary.get('user') |
|
146 |
def send_account_pending_moderation_notification( |
|
147 |
user, |
|
148 |
template_name='im/account_pending_moderation_notification.txt'): |
|
149 |
""" |
|
150 |
Notify admins that a new user has verified his email address and moderation |
|
151 |
step is required to activate his account. |
|
152 |
""" |
|
161 | 153 |
subject = _(ACCOUNT_CREATION_SUBJECT) % {'user': user.email} |
162 |
return _send_admin_notification(template_name, dictionary, subject=subject) |
|
154 |
return _send_admin_notification(template_name, {}, subject=subject, |
|
155 |
user=user, msg="account creation") |
|
163 | 156 |
|
164 | 157 |
|
165 |
def send_helpdesk_notification(user, template_name='im/helpdesk_notification.txt'): |
|
158 |
def send_account_activated_notification( |
|
159 |
user, |
|
160 |
template_name='im/account_activated_notification.txt'): |
|
166 | 161 |
""" |
167 |
Send email to settings.HELPDESK list to notify for a new user activation. |
|
168 |
|
|
169 |
Raises SendNotificationError |
|
162 |
Send email to settings.HELPDESK + settings.MANAGERES + settings.ADMINS |
|
163 |
lists to notify that a new account has been accepted and activated. |
|
170 | 164 |
""" |
171 | 165 |
message = render_to_string( |
172 | 166 |
template_name, |
173 | 167 |
{'user': user} |
174 | 168 |
) |
175 | 169 |
sender = settings.SERVER_EMAIL |
176 |
recipient_list = [e[1] for e in settings.HELPDESK + settings.MANAGERS] |
|
177 |
try: |
|
178 |
send_mail(_(HELPDESK_NOTIFICATION_EMAIL_SUBJECT) % {'user': user.email}, |
|
179 |
message, sender, recipient_list, connection=get_connection()) |
|
180 |
except (SMTPException, socket.error) as e: |
|
181 |
logger.exception(e) |
|
182 |
raise SendNotificationError() |
|
183 |
else: |
|
184 |
msg = 'Sent helpdesk admin notification for %s' % user.email |
|
185 |
logger.log(LOGGING_LEVEL, msg) |
|
170 |
recipient_list = [e[1] for e in settings.HELPDESK + |
|
171 |
settings.MANAGERS + settings.ADMINS] |
|
172 |
send_mail(_(HELPDESK_NOTIFICATION_EMAIL_SUBJECT) % {'user': user.email}, |
|
173 |
message, sender, recipient_list, connection=get_connection()) |
|
174 |
msg = 'Sent helpdesk admin notification for %s' % user.email |
|
175 |
logger.log(LOGGING_LEVEL, msg) |
|
186 | 176 |
|
187 | 177 |
|
188 | 178 |
def send_invitation(invitation, template_name='im/invitation.txt'): |
189 | 179 |
""" |
190 | 180 |
Send invitation email. |
191 |
|
|
192 |
Raises SendInvitationError |
|
193 | 181 |
""" |
194 | 182 |
subject = _(INVITATION_EMAIL_SUBJECT) |
195 | 183 |
url = '%s?code=%d' % (join_urls(BASEURL, reverse('index')), invitation.code) |
... | ... | |
200 | 188 |
'site_name': SITENAME, |
201 | 189 |
'support': CONTACT_EMAIL}) |
202 | 190 |
sender = settings.SERVER_EMAIL |
203 |
try: |
|
204 |
send_mail(subject, message, sender, [invitation.username], |
|
205 |
connection=get_connection()) |
|
206 |
except (SMTPException, socket.error) as e: |
|
207 |
logger.exception(e) |
|
208 |
raise SendInvitationError() |
|
209 |
else: |
|
210 |
msg = 'Sent invitation %s' % invitation |
|
211 |
logger.log(LOGGING_LEVEL, msg) |
|
212 |
inviter_invitations = invitation.inviter.invitations |
|
213 |
invitation.inviter.invitations = max(0, inviter_invitations - 1) |
|
214 |
invitation.inviter.save() |
|
191 |
send_mail(subject, message, sender, [invitation.username], |
|
192 |
connection=get_connection()) |
|
193 |
msg = 'Sent invitation %s' % invitation |
|
194 |
logger.log(LOGGING_LEVEL, msg) |
|
195 |
inviter_invitations = invitation.inviter.invitations |
|
196 |
invitation.inviter.invitations = max(0, inviter_invitations - 1) |
|
197 |
invitation.inviter.save() |
|
215 | 198 |
|
216 | 199 |
|
217 | 200 |
def send_greeting(user, email_template_name='im/welcome_email.txt'): |
218 | 201 |
""" |
219 |
Send welcome email. |
|
202 |
Send welcome email to an accepted/activated user.
|
|
220 | 203 |
|
221 | 204 |
Raises SMTPException, socket.error |
222 | 205 |
""" |
... | ... | |
228 | 211 |
'site_name': SITENAME, |
229 | 212 |
'support': CONTACT_EMAIL}) |
230 | 213 |
sender = settings.SERVER_EMAIL |
231 |
try: |
|
232 |
send_mail(subject, message, sender, [user.email], |
|
233 |
connection=get_connection()) |
|
234 |
except (SMTPException, socket.error) as e: |
|
235 |
logger.exception(e) |
|
236 |
raise SendGreetingError() |
|
237 |
else: |
|
238 |
msg = 'Sent greeting %s' % user.log_display |
|
239 |
logger.log(LOGGING_LEVEL, msg) |
|
214 |
send_mail(subject, message, sender, [user.email], |
|
215 |
connection=get_connection()) |
|
216 |
msg = 'Sent greeting %s' % user.log_display |
|
217 |
logger.log(LOGGING_LEVEL, msg) |
|
240 | 218 |
|
241 | 219 |
|
242 | 220 |
def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'): |
... | ... | |
278 | 256 |
logger.log(LOGGING_LEVEL, msg) |
279 | 257 |
|
280 | 258 |
|
281 |
def activate( |
|
282 |
user, |
|
283 |
email_template_name='im/welcome_email.txt', |
|
284 |
helpdesk_email_template_name='im/helpdesk_notification.txt', |
|
285 |
verify_email=False): |
|
286 |
""" |
|
287 |
Activates the specific user and sends email. |
|
288 |
|
|
289 |
Raises SendGreetingError, ValidationError |
|
290 |
""" |
|
291 |
user.is_active = True |
|
292 |
user.email_verified = True |
|
293 |
if not user.activation_sent: |
|
294 |
user.activation_sent = datetime.now() |
|
295 |
user.save() |
|
296 |
qh_sync_user(user) |
|
297 |
send_helpdesk_notification(user, helpdesk_email_template_name) |
|
298 |
send_greeting(user, email_template_name) |
|
299 |
|
|
300 |
def deactivate(user): |
|
301 |
user.is_active = False |
|
302 |
user.save() |
|
303 |
|
|
304 | 259 |
def invite(inviter, email, realname): |
305 | 260 |
inv = Invitation(inviter=inviter, username=email, realname=realname) |
306 | 261 |
inv.save() |
... | ... | |
308 | 263 |
inviter.invitations = max(0, inviter.invitations - 1) |
309 | 264 |
inviter.save() |
310 | 265 |
|
311 |
def switch_account_to_shibboleth(user, local_user, |
|
312 |
greeting_template_name='im/welcome_email.txt'): |
|
313 |
try: |
|
314 |
provider = user.provider |
|
315 |
except AttributeError: |
|
316 |
return |
|
317 |
else: |
|
318 |
if not provider == 'shibboleth': |
|
319 |
return |
|
320 |
user.delete() |
|
321 |
local_user.provider = 'shibboleth' |
|
322 |
local_user.third_party_identifier = user.third_party_identifier |
|
323 |
local_user.save() |
|
324 |
send_greeting(local_user, greeting_template_name) |
|
325 |
return local_user |
|
326 |
|
|
327 |
|
|
328 |
class SendMailError(Exception): |
|
329 |
pass |
|
330 |
|
|
331 |
|
|
332 |
class SendAdminNotificationError(SendMailError): |
|
333 |
def __init__(self): |
|
334 |
self.message = _(astakos_messages.ADMIN_NOTIFICATION_SEND_ERR) |
|
335 |
super(SendAdminNotificationError, self).__init__() |
|
336 |
|
|
337 |
|
|
338 |
class SendVerificationError(SendMailError): |
|
339 |
def __init__(self): |
|
340 |
self.message = _(astakos_messages.VERIFICATION_SEND_ERR) |
|
341 |
super(SendVerificationError, self).__init__() |
|
342 |
|
|
343 |
|
|
344 |
class SendInvitationError(SendMailError): |
|
345 |
def __init__(self): |
|
346 |
self.message = _(astakos_messages.INVITATION_SEND_ERR) |
|
347 |
super(SendInvitationError, self).__init__() |
|
348 |
|
|
349 |
|
|
350 |
class SendGreetingError(SendMailError): |
|
351 |
def __init__(self): |
|
352 |
self.message = _(astakos_messages.GREETING_SEND_ERR) |
|
353 |
super(SendGreetingError, self).__init__() |
|
354 |
|
|
355 |
|
|
356 |
class SendFeedbackError(SendMailError): |
|
357 |
def __init__(self): |
|
358 |
self.message = _(astakos_messages.FEEDBACK_SEND_ERR) |
|
359 |
super(SendFeedbackError, self).__init__() |
|
360 |
|
|
361 |
|
|
362 |
class ChangeEmailError(SendMailError): |
|
363 |
def __init__(self): |
|
364 |
self.message = _(astakos_messages.CHANGE_EMAIL_SEND_ERR) |
|
365 |
super(ChangeEmailError, self).__init__() |
|
366 |
|
|
367 |
|
|
368 |
class SendNotificationError(SendMailError): |
|
369 |
def __init__(self): |
|
370 |
self.message = _(astakos_messages.NOTIFICATION_SEND_ERR) |
|
371 |
super(SendNotificationError, self).__init__() |
|
372 |
|
|
373 | 266 |
|
374 | 267 |
### PROJECT FUNCTIONS ### |
375 | 268 |
|
Also available in: Unified diff