root / snf-astakos-app / astakos / im / functions.py @ 734107ef
History | View | Annotate | Download (28 kB)
1 |
# Copyright 2011, 2012, 2013 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.core.mail import send_mail, get_connection |
39 |
from django.core.urlresolvers import reverse |
40 |
from django.template import Context, loader |
41 |
from django.contrib.auth import ( |
42 |
login as auth_login,
|
43 |
logout as auth_logout)
|
44 |
from django.contrib.auth.models import AnonymousUser |
45 |
from django.core.exceptions import PermissionDenied |
46 |
from django.db import IntegrityError |
47 |
from django.http import Http404 |
48 |
|
49 |
from synnefo_branding.utils import render_to_string |
50 |
|
51 |
from urllib import quote |
52 |
from smtplib import SMTPException |
53 |
from datetime import datetime |
54 |
from functools import wraps |
55 |
|
56 |
from synnefo.lib import join_urls |
57 |
from astakos.im.settings import ( |
58 |
CONTACT_EMAIL, SITENAME, BASEURL, LOGGING_LEVEL, |
59 |
VERIFICATION_EMAIL_SUBJECT, ACCOUNT_CREATION_SUBJECT, |
60 |
GROUP_CREATION_SUBJECT, HELPDESK_NOTIFICATION_EMAIL_SUBJECT, |
61 |
INVITATION_EMAIL_SUBJECT, GREETING_EMAIL_SUBJECT, FEEDBACK_EMAIL_SUBJECT, |
62 |
EMAIL_CHANGE_EMAIL_SUBJECT, |
63 |
PROJECT_CREATION_SUBJECT, PROJECT_APPROVED_SUBJECT, |
64 |
PROJECT_TERMINATION_SUBJECT, PROJECT_SUSPENSION_SUBJECT, |
65 |
PROJECT_MEMBERSHIP_CHANGE_SUBJECT) |
66 |
from astakos.im.notifications import build_notification, NotificationError |
67 |
from astakos.im.models import ( |
68 |
AstakosUser, Invitation, ProjectMembership, ProjectApplication, Project, |
69 |
UserSetting, |
70 |
get_resource_names, new_chain) |
71 |
from astakos.im.quotas import (qh_sync_user, qh_sync_users, |
72 |
register_pending_apps, qh_sync_project) |
73 |
from astakos.im.project_notif import ( |
74 |
membership_change_notify, membership_enroll_notify, |
75 |
membership_request_notify, membership_leave_request_notify, |
76 |
application_submit_notify, application_approve_notify, |
77 |
application_deny_notify, |
78 |
project_termination_notify, project_suspension_notify) |
79 |
from astakos.im import settings |
80 |
import astakos.im.messages as astakos_messages |
81 |
|
82 |
logger = logging.getLogger(__name__) |
83 |
|
84 |
|
85 |
def login(request, user): |
86 |
auth_login(request, user) |
87 |
from astakos.im.models import SessionCatalog |
88 |
SessionCatalog( |
89 |
session_key=request.session.session_key, |
90 |
user=user |
91 |
).save() |
92 |
logger.info('%s logged in.', user.log_display)
|
93 |
|
94 |
|
95 |
def logout(request, *args, **kwargs): |
96 |
user = request.user |
97 |
auth_logout(request, *args, **kwargs) |
98 |
logger.info('%s logged out.', user.log_display)
|
99 |
|
100 |
|
101 |
def send_verification(user, template_name='im/activation_email.txt'): |
102 |
"""
|
103 |
Send email to user to verify his/her email and activate his/her account.
|
104 |
"""
|
105 |
url = join_urls(BASEURL, user.get_activation_url(nxt=reverse('index')))
|
106 |
message = render_to_string(template_name, { |
107 |
'user': user,
|
108 |
'url': url,
|
109 |
'baseurl': BASEURL,
|
110 |
'site_name': SITENAME,
|
111 |
'support': CONTACT_EMAIL})
|
112 |
sender = settings.SERVER_EMAIL |
113 |
send_mail(_(VERIFICATION_EMAIL_SUBJECT), message, sender, [user.email], |
114 |
connection=get_connection()) |
115 |
logger.info("Sent user verirfication email: %s", user.log_display)
|
116 |
|
117 |
|
118 |
def _send_admin_notification(template_name, |
119 |
context=None,
|
120 |
user=None,
|
121 |
msg="",
|
122 |
subject='alpha2 testing notification',):
|
123 |
"""
|
124 |
Send notification email to settings.HELPDESK + settings.MANAGERS +
|
125 |
settings.ADMINS.
|
126 |
"""
|
127 |
if context is None: |
128 |
context = {} |
129 |
if not 'user' in context: |
130 |
context['user'] = user
|
131 |
|
132 |
message = render_to_string(template_name, context) |
133 |
sender = settings.SERVER_EMAIL |
134 |
recipient_list = [e[1] for e in settings.HELPDESK + |
135 |
settings.MANAGERS + settings.ADMINS] |
136 |
send_mail(subject, message, sender, recipient_list, |
137 |
connection=get_connection()) |
138 |
if user:
|
139 |
msg = 'Sent admin notification (%s) for user %s' % (msg,
|
140 |
user.log_display) |
141 |
else:
|
142 |
msg = 'Sent admin notification (%s)' % msg
|
143 |
|
144 |
logger.log(LOGGING_LEVEL, msg) |
145 |
|
146 |
|
147 |
def send_account_pending_moderation_notification( |
148 |
user, |
149 |
template_name='im/account_pending_moderation_notification.txt'):
|
150 |
"""
|
151 |
Notify admins that a new user has verified his email address and moderation
|
152 |
step is required to activate his account.
|
153 |
"""
|
154 |
subject = _(ACCOUNT_CREATION_SUBJECT) % {'user': user.email}
|
155 |
return _send_admin_notification(template_name, {}, subject=subject,
|
156 |
user=user, msg="account creation")
|
157 |
|
158 |
|
159 |
def send_account_activated_notification( |
160 |
user, |
161 |
template_name='im/account_activated_notification.txt'):
|
162 |
"""
|
163 |
Send email to settings.HELPDESK + settings.MANAGERES + settings.ADMINS
|
164 |
lists to notify that a new account has been accepted and activated.
|
165 |
"""
|
166 |
message = render_to_string( |
167 |
template_name, |
168 |
{'user': user}
|
169 |
) |
170 |
sender = settings.SERVER_EMAIL |
171 |
recipient_list = [e[1] for e in settings.HELPDESK + |
172 |
settings.MANAGERS + settings.ADMINS] |
173 |
send_mail(_(HELPDESK_NOTIFICATION_EMAIL_SUBJECT) % {'user': user.email},
|
174 |
message, sender, recipient_list, connection=get_connection()) |
175 |
msg = 'Sent helpdesk admin notification for %s' % user.email
|
176 |
logger.log(LOGGING_LEVEL, msg) |
177 |
|
178 |
|
179 |
def send_invitation(invitation, template_name='im/invitation.txt'): |
180 |
"""
|
181 |
Send invitation email.
|
182 |
"""
|
183 |
subject = _(INVITATION_EMAIL_SUBJECT) |
184 |
url = '%s?code=%d' % (join_urls(BASEURL, reverse('index')), invitation.code) |
185 |
message = render_to_string(template_name, { |
186 |
'invitation': invitation,
|
187 |
'url': url,
|
188 |
'baseurl': BASEURL,
|
189 |
'site_name': SITENAME,
|
190 |
'support': CONTACT_EMAIL})
|
191 |
sender = settings.SERVER_EMAIL |
192 |
send_mail(subject, message, sender, [invitation.username], |
193 |
connection=get_connection()) |
194 |
msg = 'Sent invitation %s' % invitation
|
195 |
logger.log(LOGGING_LEVEL, msg) |
196 |
inviter_invitations = invitation.inviter.invitations |
197 |
invitation.inviter.invitations = max(0, inviter_invitations - 1) |
198 |
invitation.inviter.save() |
199 |
|
200 |
|
201 |
def send_greeting(user, email_template_name='im/welcome_email.txt'): |
202 |
"""
|
203 |
Send welcome email to an accepted/activated user.
|
204 |
|
205 |
Raises SMTPException, socket.error
|
206 |
"""
|
207 |
subject = _(GREETING_EMAIL_SUBJECT) |
208 |
message = render_to_string(email_template_name, { |
209 |
'user': user,
|
210 |
'url': join_urls(BASEURL, reverse('index')), |
211 |
'baseurl': BASEURL,
|
212 |
'site_name': SITENAME,
|
213 |
'support': CONTACT_EMAIL})
|
214 |
sender = settings.SERVER_EMAIL |
215 |
send_mail(subject, message, sender, [user.email], |
216 |
connection=get_connection()) |
217 |
msg = 'Sent greeting %s' % user.log_display
|
218 |
logger.log(LOGGING_LEVEL, msg) |
219 |
|
220 |
|
221 |
def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'): |
222 |
subject = _(FEEDBACK_EMAIL_SUBJECT) |
223 |
from_email = settings.SERVER_EMAIL |
224 |
recipient_list = [e[1] for e in settings.HELPDESK] |
225 |
content = render_to_string(email_template_name, { |
226 |
'message': msg,
|
227 |
'data': data,
|
228 |
'user': user})
|
229 |
send_mail(subject, content, from_email, recipient_list, |
230 |
connection=get_connection()) |
231 |
msg = 'Sent feedback from %s' % user.log_display
|
232 |
logger.log(LOGGING_LEVEL, msg) |
233 |
|
234 |
|
235 |
def send_change_email( |
236 |
ec, request, email_template_name='registration/email_change_email.txt'):
|
237 |
url = ec.get_url() |
238 |
url = request.build_absolute_uri(url) |
239 |
c = {'url': url, 'site_name': SITENAME, 'support': CONTACT_EMAIL, |
240 |
'ec': ec}
|
241 |
message = render_to_string(email_template_name, c) |
242 |
from_email = settings.SERVER_EMAIL |
243 |
send_mail(_(EMAIL_CHANGE_EMAIL_SUBJECT), message, from_email, |
244 |
[ec.new_email_address], connection=get_connection()) |
245 |
msg = 'Sent change email for %s' % ec.user.log_display
|
246 |
logger.log(LOGGING_LEVEL, msg) |
247 |
|
248 |
|
249 |
def invite(inviter, email, realname): |
250 |
inv = Invitation(inviter=inviter, username=email, realname=realname) |
251 |
inv.save() |
252 |
send_invitation(inv) |
253 |
inviter.invitations = max(0, inviter.invitations - 1) |
254 |
inviter.save() |
255 |
|
256 |
|
257 |
### PROJECT FUNCTIONS ###
|
258 |
|
259 |
AUTO_ACCEPT_POLICY = 1
|
260 |
MODERATED_POLICY = 2
|
261 |
CLOSED_POLICY = 3
|
262 |
|
263 |
POLICIES = [AUTO_ACCEPT_POLICY, MODERATED_POLICY, CLOSED_POLICY] |
264 |
|
265 |
|
266 |
def get_project_by_application_id(project_application_id): |
267 |
try:
|
268 |
return Project.objects.get(application__id=project_application_id)
|
269 |
except Project.DoesNotExist:
|
270 |
m = (_(astakos_messages.UNKNOWN_PROJECT_APPLICATION_ID) % |
271 |
project_application_id) |
272 |
raise IOError(m) |
273 |
|
274 |
|
275 |
def get_related_project_id(application_id): |
276 |
try:
|
277 |
app = ProjectApplication.objects.get(id=application_id) |
278 |
chain = app.chain |
279 |
Project.objects.get(id=chain) |
280 |
return chain
|
281 |
except (ProjectApplication.DoesNotExist, Project.DoesNotExist):
|
282 |
return None |
283 |
|
284 |
|
285 |
def get_chain_of_application_id(application_id): |
286 |
try:
|
287 |
app = ProjectApplication.objects.get(id=application_id) |
288 |
chain = app.chain |
289 |
return chain.chain
|
290 |
except ProjectApplication.DoesNotExist:
|
291 |
return None |
292 |
|
293 |
|
294 |
def get_project_by_id(project_id): |
295 |
try:
|
296 |
return Project.objects.get(id=project_id)
|
297 |
except Project.DoesNotExist:
|
298 |
m = _(astakos_messages.UNKNOWN_PROJECT_ID) % project_id |
299 |
raise IOError(m) |
300 |
|
301 |
|
302 |
def get_project_by_name(name): |
303 |
try:
|
304 |
return Project.objects.get(name=name)
|
305 |
except Project.DoesNotExist:
|
306 |
m = _(astakos_messages.UNKNOWN_PROJECT_ID) % name |
307 |
raise IOError(m) |
308 |
|
309 |
|
310 |
def get_project_for_update(project_id): |
311 |
try:
|
312 |
return Project.objects.get_for_update(id=project_id)
|
313 |
except Project.DoesNotExist:
|
314 |
m = _(astakos_messages.UNKNOWN_PROJECT_ID) % project_id |
315 |
raise IOError(m) |
316 |
|
317 |
|
318 |
def get_application_for_update(application_id): |
319 |
try:
|
320 |
return ProjectApplication.objects.get_for_update(id=application_id)
|
321 |
except ProjectApplication.DoesNotExist:
|
322 |
m = _(astakos_messages.UNKNOWN_PROJECT_APPLICATION_ID) % application_id |
323 |
raise IOError(m) |
324 |
|
325 |
|
326 |
def get_user_by_id(user_id): |
327 |
try:
|
328 |
return AstakosUser.objects.get(id=user_id)
|
329 |
except AstakosUser.DoesNotExist:
|
330 |
m = _(astakos_messages.UNKNOWN_USER_ID) % user_id |
331 |
raise IOError(m) |
332 |
|
333 |
|
334 |
def get_user_by_uuid(uuid): |
335 |
try:
|
336 |
return AstakosUser.objects.get(uuid=uuid)
|
337 |
except AstakosUser.DoesNotExist:
|
338 |
m = _(astakos_messages.UNKNOWN_USER_ID) % uuid |
339 |
raise IOError(m) |
340 |
|
341 |
|
342 |
def get_membership_for_update(project_id, user_id): |
343 |
try:
|
344 |
objs = ProjectMembership.objects |
345 |
return objs.get_for_update(project__id=project_id,
|
346 |
person__id=user_id) |
347 |
except ProjectMembership.DoesNotExist:
|
348 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
349 |
raise IOError(m) |
350 |
|
351 |
|
352 |
def get_membership_for_update_by_id(project_id, memb_id): |
353 |
try:
|
354 |
objs = ProjectMembership.objects |
355 |
return objs.get_for_update(project__id=project_id,
|
356 |
id=memb_id) |
357 |
except ProjectMembership.DoesNotExist:
|
358 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
359 |
raise IOError(m) |
360 |
|
361 |
|
362 |
def checkAllowed(entity, request_user, admin_only=False): |
363 |
if isinstance(entity, Project): |
364 |
application = entity.application |
365 |
elif isinstance(entity, ProjectApplication): |
366 |
application = entity |
367 |
else:
|
368 |
m = "%s not a Project nor a ProjectApplication" % (entity,)
|
369 |
raise ValueError(m) |
370 |
|
371 |
if not request_user or request_user.is_project_admin(): |
372 |
return
|
373 |
|
374 |
if not admin_only and application.owner == request_user: |
375 |
return
|
376 |
|
377 |
m = _(astakos_messages.NOT_ALLOWED) |
378 |
raise PermissionDenied(m)
|
379 |
|
380 |
|
381 |
def checkAlive(project): |
382 |
if not project.is_alive: |
383 |
m = _(astakos_messages.NOT_ALIVE_PROJECT) % project.__dict__ |
384 |
raise PermissionDenied(m)
|
385 |
|
386 |
|
387 |
def accept_membership_checks(project, request_user): |
388 |
checkAllowed(project, request_user) |
389 |
checkAlive(project) |
390 |
|
391 |
join_policy = project.application.member_join_policy |
392 |
if join_policy == CLOSED_POLICY:
|
393 |
m = _(astakos_messages.MEMBER_JOIN_POLICY_CLOSED) |
394 |
raise PermissionDenied(m)
|
395 |
|
396 |
if project.violates_members_limit(adding=1): |
397 |
m = _(astakos_messages.MEMBER_NUMBER_LIMIT_REACHED) |
398 |
raise PermissionDenied(m)
|
399 |
|
400 |
|
401 |
def accept_membership(project_id, memb_id, request_user=None): |
402 |
project = get_project_for_update(project_id) |
403 |
accept_membership_checks(project, request_user) |
404 |
|
405 |
membership = get_membership_for_update_by_id(project_id, memb_id) |
406 |
if not membership.can_accept(): |
407 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
408 |
raise PermissionDenied(m)
|
409 |
|
410 |
user = membership.person |
411 |
membership.accept() |
412 |
qh_sync_user(user) |
413 |
logger.info("User %s has been accepted in %s." %
|
414 |
(user.log_display, project)) |
415 |
|
416 |
membership_change_notify(project, user, 'accepted')
|
417 |
return membership
|
418 |
|
419 |
|
420 |
def reject_membership_checks(project, request_user): |
421 |
checkAllowed(project, request_user) |
422 |
checkAlive(project) |
423 |
|
424 |
|
425 |
def reject_membership(project_id, memb_id, request_user=None): |
426 |
project = get_project_for_update(project_id) |
427 |
reject_membership_checks(project, request_user) |
428 |
membership = get_membership_for_update_by_id(project_id, memb_id) |
429 |
if not membership.can_reject(): |
430 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
431 |
raise PermissionDenied(m)
|
432 |
|
433 |
user = membership.person |
434 |
membership.reject() |
435 |
logger.info("Request of user %s for %s has been rejected." %
|
436 |
(user.log_display, project)) |
437 |
|
438 |
membership_change_notify(project, user, 'rejected')
|
439 |
return membership
|
440 |
|
441 |
|
442 |
def cancel_membership_checks(project): |
443 |
checkAlive(project) |
444 |
|
445 |
|
446 |
def cancel_membership(project_id, request_user): |
447 |
project = get_project_for_update(project_id) |
448 |
cancel_membership_checks(project) |
449 |
membership = get_membership_for_update(project_id, request_user.id) |
450 |
if not membership.can_cancel(): |
451 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
452 |
raise PermissionDenied(m)
|
453 |
|
454 |
membership.cancel() |
455 |
logger.info("Request of user %s for %s has been cancelled." %
|
456 |
(membership.person.log_display, project)) |
457 |
|
458 |
|
459 |
def remove_membership_checks(project, request_user=None): |
460 |
checkAllowed(project, request_user) |
461 |
checkAlive(project) |
462 |
|
463 |
leave_policy = project.application.member_leave_policy |
464 |
if leave_policy == CLOSED_POLICY:
|
465 |
m = _(astakos_messages.MEMBER_LEAVE_POLICY_CLOSED) |
466 |
raise PermissionDenied(m)
|
467 |
|
468 |
|
469 |
def remove_membership(project_id, memb_id, request_user=None): |
470 |
project = get_project_for_update(project_id) |
471 |
remove_membership_checks(project, request_user) |
472 |
membership = get_membership_for_update_by_id(project_id, memb_id) |
473 |
if not membership.can_remove(): |
474 |
m = _(astakos_messages.NOT_ACCEPTED_MEMBERSHIP) |
475 |
raise PermissionDenied(m)
|
476 |
|
477 |
user = membership.person |
478 |
membership.remove() |
479 |
qh_sync_user(user) |
480 |
logger.info("User %s has been removed from %s." %
|
481 |
(user.log_display, project)) |
482 |
|
483 |
membership_change_notify(project, user, 'removed')
|
484 |
return membership
|
485 |
|
486 |
|
487 |
def enroll_member(project_id, user, request_user=None): |
488 |
project = get_project_for_update(project_id) |
489 |
accept_membership_checks(project, request_user) |
490 |
|
491 |
membership, created = ProjectMembership.objects.get_or_create( |
492 |
project=project, |
493 |
person=user) |
494 |
|
495 |
if not membership.can_accept(): |
496 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
497 |
raise PermissionDenied(m)
|
498 |
|
499 |
membership.accept() |
500 |
qh_sync_user(user) |
501 |
logger.info("User %s has been enrolled in %s." %
|
502 |
(membership.person.log_display, project)) |
503 |
|
504 |
membership_enroll_notify(project, membership.person) |
505 |
return membership
|
506 |
|
507 |
|
508 |
def leave_project_checks(project): |
509 |
checkAlive(project) |
510 |
|
511 |
leave_policy = project.application.member_leave_policy |
512 |
if leave_policy == CLOSED_POLICY:
|
513 |
m = _(astakos_messages.MEMBER_LEAVE_POLICY_CLOSED) |
514 |
raise PermissionDenied(m)
|
515 |
|
516 |
|
517 |
def can_leave_request(project, user): |
518 |
leave_policy = project.application.member_leave_policy |
519 |
if leave_policy == CLOSED_POLICY:
|
520 |
return False |
521 |
m = user.get_membership(project) |
522 |
if m is None: |
523 |
return False |
524 |
if m.state != ProjectMembership.ACCEPTED:
|
525 |
return False |
526 |
return True |
527 |
|
528 |
|
529 |
def leave_project(project_id, request_user): |
530 |
project = get_project_for_update(project_id) |
531 |
leave_project_checks(project) |
532 |
membership = get_membership_for_update(project_id, request_user.id) |
533 |
if not membership.can_leave(): |
534 |
m = _(astakos_messages.NOT_ACCEPTED_MEMBERSHIP) |
535 |
raise PermissionDenied(m)
|
536 |
|
537 |
auto_accepted = False
|
538 |
leave_policy = project.application.member_leave_policy |
539 |
if leave_policy == AUTO_ACCEPT_POLICY:
|
540 |
membership.remove() |
541 |
qh_sync_user(request_user) |
542 |
logger.info("User %s has left %s." %
|
543 |
(membership.person.log_display, project)) |
544 |
auto_accepted = True
|
545 |
else:
|
546 |
membership.leave_request() |
547 |
logger.info("User %s requested to leave %s." %
|
548 |
(membership.person.log_display, project)) |
549 |
membership_leave_request_notify(project, membership.person) |
550 |
return auto_accepted
|
551 |
|
552 |
|
553 |
def join_project_checks(project): |
554 |
checkAlive(project) |
555 |
|
556 |
join_policy = project.application.member_join_policy |
557 |
if join_policy == CLOSED_POLICY:
|
558 |
m = _(astakos_messages.MEMBER_JOIN_POLICY_CLOSED) |
559 |
raise PermissionDenied(m)
|
560 |
|
561 |
|
562 |
def can_join_request(project, user): |
563 |
join_policy = project.application.member_join_policy |
564 |
if join_policy == CLOSED_POLICY:
|
565 |
return False |
566 |
m = user.get_membership(project) |
567 |
if m:
|
568 |
return False |
569 |
return True |
570 |
|
571 |
|
572 |
def join_project(project_id, request_user): |
573 |
project = get_project_for_update(project_id) |
574 |
join_project_checks(project) |
575 |
|
576 |
membership, created = ProjectMembership.objects.get_or_create( |
577 |
project=project, |
578 |
person=request_user) |
579 |
|
580 |
if not created: |
581 |
msg = _(astakos_messages.MEMBERSHIP_REQUEST_EXISTS) |
582 |
raise PermissionDenied(msg)
|
583 |
|
584 |
auto_accepted = False
|
585 |
join_policy = project.application.member_join_policy |
586 |
if (join_policy == AUTO_ACCEPT_POLICY and ( |
587 |
not project.violates_members_limit(adding=1))): |
588 |
membership.accept() |
589 |
qh_sync_user(request_user) |
590 |
logger.info("User %s joined %s." %
|
591 |
(membership.person.log_display, project)) |
592 |
auto_accepted = True
|
593 |
else:
|
594 |
membership_request_notify(project, membership.person) |
595 |
logger.info("User %s requested to join %s." %
|
596 |
(membership.person.log_display, project)) |
597 |
return auto_accepted
|
598 |
|
599 |
|
600 |
def submit_application(owner=None, |
601 |
name=None,
|
602 |
precursor_id=None,
|
603 |
homepage=None,
|
604 |
description=None,
|
605 |
start_date=None,
|
606 |
end_date=None,
|
607 |
member_join_policy=None,
|
608 |
member_leave_policy=None,
|
609 |
limit_on_members_number=None,
|
610 |
comments=None,
|
611 |
resource_policies=None,
|
612 |
request_user=None):
|
613 |
|
614 |
precursor = None
|
615 |
if precursor_id is not None: |
616 |
objs = ProjectApplication.objects |
617 |
precursor = objs.get_for_update(id=precursor_id) |
618 |
|
619 |
if (request_user and |
620 |
(not precursor.owner == request_user and |
621 |
not request_user.is_superuser
|
622 |
and not request_user.is_project_admin())): |
623 |
m = _(astakos_messages.NOT_ALLOWED) |
624 |
raise PermissionDenied(m)
|
625 |
|
626 |
force = request_user.is_project_admin() |
627 |
ok, limit = qh_add_pending_app(owner, precursor, force) |
628 |
if not ok: |
629 |
m = _(astakos_messages.REACHED_PENDING_APPLICATION_LIMIT) % limit |
630 |
raise PermissionDenied(m)
|
631 |
|
632 |
application = ProjectApplication( |
633 |
applicant=request_user, |
634 |
owner=owner, |
635 |
name=name, |
636 |
precursor_application_id=precursor_id, |
637 |
homepage=homepage, |
638 |
description=description, |
639 |
start_date=start_date, |
640 |
end_date=end_date, |
641 |
member_join_policy=member_join_policy, |
642 |
member_leave_policy=member_leave_policy, |
643 |
limit_on_members_number=limit_on_members_number, |
644 |
comments=comments) |
645 |
|
646 |
if precursor is None: |
647 |
application.chain = new_chain() |
648 |
else:
|
649 |
chain = precursor.chain |
650 |
application.chain = chain |
651 |
objs = ProjectApplication.objects |
652 |
q = objs.filter(chain=chain, state=ProjectApplication.PENDING) |
653 |
pending = q.select_for_update() |
654 |
for app in pending: |
655 |
app.state = ProjectApplication.REPLACED |
656 |
app.save() |
657 |
|
658 |
application.save() |
659 |
if resource_policies is not None: |
660 |
application.set_resource_policies(resource_policies) |
661 |
logger.info("User %s submitted %s." %
|
662 |
(request_user.log_display, application.log_display)) |
663 |
application_submit_notify(application) |
664 |
return application
|
665 |
|
666 |
|
667 |
def cancel_application(application_id, request_user=None, reason=""): |
668 |
application = get_application_for_update(application_id) |
669 |
checkAllowed(application, request_user) |
670 |
|
671 |
if not application.can_cancel(): |
672 |
m = _(astakos_messages.APPLICATION_CANNOT_CANCEL % |
673 |
(application.id, application.state_display())) |
674 |
raise PermissionDenied(m)
|
675 |
|
676 |
qh_release_pending_app(application.owner) |
677 |
|
678 |
application.cancel() |
679 |
logger.info("%s has been cancelled." % (application.log_display))
|
680 |
|
681 |
|
682 |
def dismiss_application(application_id, request_user=None, reason=""): |
683 |
application = get_application_for_update(application_id) |
684 |
checkAllowed(application, request_user) |
685 |
|
686 |
if not application.can_dismiss(): |
687 |
m = _(astakos_messages.APPLICATION_CANNOT_DISMISS % |
688 |
(application.id, application.state_display())) |
689 |
raise PermissionDenied(m)
|
690 |
|
691 |
application.dismiss() |
692 |
logger.info("%s has been dismissed." % (application.log_display))
|
693 |
|
694 |
|
695 |
def deny_application(application_id, request_user=None, reason=""): |
696 |
application = get_application_for_update(application_id) |
697 |
|
698 |
checkAllowed(application, request_user, admin_only=True)
|
699 |
|
700 |
if not application.can_deny(): |
701 |
m = _(astakos_messages.APPLICATION_CANNOT_DENY % |
702 |
(application.id, application.state_display())) |
703 |
raise PermissionDenied(m)
|
704 |
|
705 |
qh_release_pending_app(application.owner) |
706 |
|
707 |
application.deny(reason) |
708 |
logger.info("%s has been denied with reason \"%s\"." %
|
709 |
(application.log_display, reason)) |
710 |
application_deny_notify(application) |
711 |
|
712 |
|
713 |
def approve_application(app_id, request_user=None, reason=""): |
714 |
|
715 |
try:
|
716 |
objects = ProjectApplication.objects |
717 |
application = objects.get_for_update(id=app_id) |
718 |
except ProjectApplication.DoesNotExist:
|
719 |
m = _(astakos_messages.UNKNOWN_PROJECT_APPLICATION_ID % (app_id,)) |
720 |
raise PermissionDenied(m)
|
721 |
|
722 |
checkAllowed(application, request_user, admin_only=True)
|
723 |
|
724 |
if not application.can_approve(): |
725 |
m = _(astakos_messages.APPLICATION_CANNOT_APPROVE % |
726 |
(application.id, application.state_display())) |
727 |
raise PermissionDenied(m)
|
728 |
|
729 |
qh_release_pending_app(application.owner) |
730 |
project = application.approve(reason) |
731 |
qh_sync_project(project) |
732 |
logger.info("%s has been approved." % (application.log_display))
|
733 |
application_approve_notify(application) |
734 |
|
735 |
|
736 |
def check_expiration(execute=False): |
737 |
objects = Project.objects |
738 |
expired = objects.expired_projects() |
739 |
if execute:
|
740 |
for project in expired: |
741 |
terminate(project.id) |
742 |
|
743 |
return [project.expiration_info() for project in expired] |
744 |
|
745 |
|
746 |
def terminate(project_id, request_user=None): |
747 |
project = get_project_for_update(project_id) |
748 |
checkAllowed(project, request_user, admin_only=True)
|
749 |
checkAlive(project) |
750 |
|
751 |
project.terminate() |
752 |
qh_sync_project(project) |
753 |
logger.info("%s has been terminated." % (project))
|
754 |
|
755 |
project_termination_notify(project) |
756 |
|
757 |
|
758 |
def suspend(project_id, request_user=None): |
759 |
project = get_project_for_update(project_id) |
760 |
checkAllowed(project, request_user, admin_only=True)
|
761 |
checkAlive(project) |
762 |
|
763 |
project.suspend() |
764 |
qh_sync_project(project) |
765 |
logger.info("%s has been suspended." % (project))
|
766 |
|
767 |
project_suspension_notify(project) |
768 |
|
769 |
|
770 |
def resume(project_id, request_user=None): |
771 |
project = get_project_for_update(project_id) |
772 |
checkAllowed(project, request_user, admin_only=True)
|
773 |
|
774 |
if not project.is_suspended: |
775 |
m = _(astakos_messages.NOT_SUSPENDED_PROJECT) % project.__dict__ |
776 |
raise PermissionDenied(m)
|
777 |
|
778 |
project.resume() |
779 |
qh_sync_project(project) |
780 |
logger.info("%s has been unsuspended." % (project))
|
781 |
|
782 |
|
783 |
def get_by_chain_or_404(chain_id): |
784 |
try:
|
785 |
project = Project.objects.get(id=chain_id) |
786 |
application = project.application |
787 |
return project, application
|
788 |
except:
|
789 |
application = ProjectApplication.objects.latest_of_chain(chain_id) |
790 |
if application is None: |
791 |
raise Http404
|
792 |
else:
|
793 |
return None, application |
794 |
|
795 |
|
796 |
def get_user_setting(user_id, key): |
797 |
try:
|
798 |
setting = UserSetting.objects.get( |
799 |
user=user_id, setting=key) |
800 |
return setting.value
|
801 |
except UserSetting.DoesNotExist:
|
802 |
return getattr(settings, key) |
803 |
|
804 |
|
805 |
def set_user_setting(user_id, key, value): |
806 |
try:
|
807 |
setting = UserSetting.objects.get_for_update( |
808 |
user=user_id, setting=key) |
809 |
except UserSetting.DoesNotExist:
|
810 |
setting = UserSetting(user_id=user_id, setting=key) |
811 |
setting.value = value |
812 |
setting.save() |
813 |
|
814 |
|
815 |
def unset_user_setting(user_id, key): |
816 |
UserSetting.objects.filter(user=user_id, setting=key).delete() |
817 |
|
818 |
|
819 |
def _partition_by(f, l): |
820 |
d = {} |
821 |
for x in l: |
822 |
group = f(x) |
823 |
group_l = d.get(group, []) |
824 |
group_l.append(x) |
825 |
d[group] = group_l |
826 |
return d
|
827 |
|
828 |
|
829 |
def count_pending_app(users): |
830 |
users = list(users)
|
831 |
apps = ProjectApplication.objects.filter(state=ProjectApplication.PENDING, |
832 |
owner__in=users) |
833 |
apps_d = _partition_by(lambda a: a.owner.uuid, apps)
|
834 |
|
835 |
usage = {} |
836 |
for user in users: |
837 |
uuid = user.uuid |
838 |
usage[uuid] = len(apps_d.get(uuid, []))
|
839 |
return usage
|
840 |
|
841 |
|
842 |
def qh_add_pending_app(user, precursor=None, force=False, dry_run=False): |
843 |
if precursor is None: |
844 |
diff = 1
|
845 |
else:
|
846 |
chain = precursor.chain |
847 |
objs = ProjectApplication.objects |
848 |
q = objs.filter(chain=chain, state=ProjectApplication.PENDING) |
849 |
count = q.count() |
850 |
diff = 1 - count
|
851 |
|
852 |
return register_pending_apps(user, diff, force, dry_run)
|
853 |
|
854 |
|
855 |
def qh_release_pending_app(user): |
856 |
register_pending_apps(user, -1)
|