root / snf-astakos-app / astakos / im / functions.py @ 9096ffbb
History | View | Annotate | Download (32.7 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 |
|
36 |
from django.utils.translation import ugettext as _ |
37 |
from django.core.mail import send_mail, get_connection |
38 |
from django.core.urlresolvers import reverse |
39 |
from django.contrib.auth import login as auth_login, logout as auth_logout |
40 |
from django.db.models import Q |
41 |
|
42 |
from synnefo_branding.utils import render_to_string |
43 |
|
44 |
from synnefo.lib import join_urls |
45 |
from astakos.im.models import AstakosUser, Invitation, ProjectMembership, \ |
46 |
ProjectApplication, Project, new_chain, Resource, ProjectLock |
47 |
from astakos.im.quotas import qh_sync_user, get_pending_app_quota, \ |
48 |
register_pending_apps, qh_sync_project, qh_sync_locked_users, \ |
49 |
get_users_for_update, members_to_sync |
50 |
from astakos.im.project_notif import membership_change_notify, \ |
51 |
membership_enroll_notify, membership_request_notify, \ |
52 |
membership_leave_request_notify, application_submit_notify, \ |
53 |
application_approve_notify, application_deny_notify, \ |
54 |
project_termination_notify, project_suspension_notify, \ |
55 |
project_unsuspension_notify, project_reinstatement_notify |
56 |
from astakos.im import settings |
57 |
|
58 |
import astakos.im.messages as astakos_messages |
59 |
|
60 |
logger = logging.getLogger(__name__) |
61 |
|
62 |
|
63 |
def login(request, user): |
64 |
auth_login(request, user) |
65 |
from astakos.im.models import SessionCatalog |
66 |
SessionCatalog( |
67 |
session_key=request.session.session_key, |
68 |
user=user |
69 |
).save() |
70 |
logger.info('%s logged in.', user.log_display)
|
71 |
|
72 |
|
73 |
def logout(request, *args, **kwargs): |
74 |
user = request.user |
75 |
auth_logout(request, *args, **kwargs) |
76 |
logger.info('%s logged out.', user.log_display)
|
77 |
|
78 |
|
79 |
def send_verification(user, template_name='im/activation_email.txt'): |
80 |
"""
|
81 |
Send email to user to verify his/her email and activate his/her account.
|
82 |
"""
|
83 |
url = join_urls(settings.BASE_HOST, |
84 |
user.get_activation_url(nxt=reverse('index')))
|
85 |
message = render_to_string(template_name, { |
86 |
'user': user,
|
87 |
'url': url,
|
88 |
'baseurl': settings.BASE_URL,
|
89 |
'site_name': settings.SITENAME,
|
90 |
'support': settings.CONTACT_EMAIL})
|
91 |
sender = settings.SERVER_EMAIL |
92 |
send_mail(_(astakos_messages.VERIFICATION_EMAIL_SUBJECT), message, sender, |
93 |
[user.email], |
94 |
connection=get_connection()) |
95 |
logger.info("Sent user verirfication email: %s", user.log_display)
|
96 |
|
97 |
|
98 |
def _send_admin_notification(template_name, |
99 |
context=None,
|
100 |
user=None,
|
101 |
msg="",
|
102 |
subject='alpha2 testing notification',):
|
103 |
"""
|
104 |
Send notification email to settings.HELPDESK + settings.MANAGERS +
|
105 |
settings.ADMINS.
|
106 |
"""
|
107 |
if context is None: |
108 |
context = {} |
109 |
if not 'user' in context: |
110 |
context['user'] = user
|
111 |
|
112 |
message = render_to_string(template_name, context) |
113 |
sender = settings.SERVER_EMAIL |
114 |
recipient_list = [e[1] for e in settings.HELPDESK + |
115 |
settings.MANAGERS + settings.ADMINS] |
116 |
send_mail(subject, message, sender, recipient_list, |
117 |
connection=get_connection()) |
118 |
if user:
|
119 |
msg = 'Sent admin notification (%s) for user %s' % (msg,
|
120 |
user.log_display) |
121 |
else:
|
122 |
msg = 'Sent admin notification (%s)' % msg
|
123 |
|
124 |
logger.log(settings.LOGGING_LEVEL, msg) |
125 |
|
126 |
|
127 |
def send_account_pending_moderation_notification( |
128 |
user, |
129 |
template_name='im/account_pending_moderation_notification.txt'):
|
130 |
"""
|
131 |
Notify admins that a new user has verified his email address and moderation
|
132 |
step is required to activate his account.
|
133 |
"""
|
134 |
subject = (_(astakos_messages.ACCOUNT_CREATION_SUBJECT) % |
135 |
{'user': user.email})
|
136 |
return _send_admin_notification(template_name, {}, subject=subject,
|
137 |
user=user, msg="account creation")
|
138 |
|
139 |
|
140 |
def send_account_activated_notification( |
141 |
user, |
142 |
template_name='im/account_activated_notification.txt'):
|
143 |
"""
|
144 |
Send email to settings.HELPDESK + settings.MANAGERES + settings.ADMINS
|
145 |
lists to notify that a new account has been accepted and activated.
|
146 |
"""
|
147 |
message = render_to_string( |
148 |
template_name, |
149 |
{'user': user}
|
150 |
) |
151 |
sender = settings.SERVER_EMAIL |
152 |
recipient_list = [e[1] for e in settings.HELPDESK + |
153 |
settings.MANAGERS + settings.ADMINS] |
154 |
send_mail(_(astakos_messages.HELPDESK_NOTIFICATION_EMAIL_SUBJECT) % |
155 |
{'user': user.email},
|
156 |
message, sender, recipient_list, connection=get_connection()) |
157 |
msg = 'Sent helpdesk admin notification for %s' % user.email
|
158 |
logger.log(settings.LOGGING_LEVEL, msg) |
159 |
|
160 |
|
161 |
def send_invitation(invitation, template_name='im/invitation.txt'): |
162 |
"""
|
163 |
Send invitation email.
|
164 |
"""
|
165 |
subject = _(astakos_messages.INVITATION_EMAIL_SUBJECT) |
166 |
url = '%s?code=%d' % (join_urls(settings.BASE_HOST,
|
167 |
reverse('index')), invitation.code)
|
168 |
message = render_to_string(template_name, { |
169 |
'invitation': invitation,
|
170 |
'url': url,
|
171 |
'baseurl': settings.BASE_URL,
|
172 |
'site_name': settings.SITENAME,
|
173 |
'support': settings.CONTACT_EMAIL})
|
174 |
sender = settings.SERVER_EMAIL |
175 |
send_mail(subject, message, sender, [invitation.username], |
176 |
connection=get_connection()) |
177 |
msg = 'Sent invitation %s' % invitation
|
178 |
logger.log(settings.LOGGING_LEVEL, msg) |
179 |
inviter_invitations = invitation.inviter.invitations |
180 |
invitation.inviter.invitations = max(0, inviter_invitations - 1) |
181 |
invitation.inviter.save() |
182 |
|
183 |
|
184 |
def send_greeting(user, email_template_name='im/welcome_email.txt'): |
185 |
"""
|
186 |
Send welcome email to an accepted/activated user.
|
187 |
|
188 |
Raises SMTPException, socket.error
|
189 |
"""
|
190 |
subject = _(astakos_messages.GREETING_EMAIL_SUBJECT) |
191 |
message = render_to_string(email_template_name, { |
192 |
'user': user,
|
193 |
'url': join_urls(settings.BASE_HOST,
|
194 |
reverse('index')),
|
195 |
'baseurl': settings.BASE_URL,
|
196 |
'site_name': settings.SITENAME,
|
197 |
'support': settings.CONTACT_EMAIL})
|
198 |
sender = settings.SERVER_EMAIL |
199 |
send_mail(subject, message, sender, [user.email], |
200 |
connection=get_connection()) |
201 |
msg = 'Sent greeting %s' % user.log_display
|
202 |
logger.log(settings.LOGGING_LEVEL, msg) |
203 |
|
204 |
|
205 |
def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'): |
206 |
subject = _(astakos_messages.FEEDBACK_EMAIL_SUBJECT) |
207 |
from_email = settings.SERVER_EMAIL |
208 |
recipient_list = [e[1] for e in settings.HELPDESK] |
209 |
content = render_to_string(email_template_name, { |
210 |
'message': msg,
|
211 |
'data': data,
|
212 |
'user': user})
|
213 |
send_mail(subject, content, from_email, recipient_list, |
214 |
connection=get_connection()) |
215 |
msg = 'Sent feedback from %s' % user.log_display
|
216 |
logger.log(settings.LOGGING_LEVEL, msg) |
217 |
|
218 |
|
219 |
def send_change_email( |
220 |
ec, request, email_template_name='registration/email_change_email.txt'
|
221 |
): |
222 |
url = ec.get_url() |
223 |
url = request.build_absolute_uri(url) |
224 |
c = {'url': url, 'site_name': settings.SITENAME, |
225 |
'support': settings.CONTACT_EMAIL,
|
226 |
'ec': ec}
|
227 |
message = render_to_string(email_template_name, c) |
228 |
from_email = settings.SERVER_EMAIL |
229 |
send_mail(_(astakos_messages.EMAIL_CHANGE_EMAIL_SUBJECT), message, |
230 |
from_email, |
231 |
[ec.new_email_address], connection=get_connection()) |
232 |
msg = 'Sent change email for %s' % ec.user.log_display
|
233 |
logger.log(settings.LOGGING_LEVEL, msg) |
234 |
|
235 |
|
236 |
def invite(inviter, email, realname): |
237 |
inv = Invitation(inviter=inviter, username=email, realname=realname) |
238 |
inv.save() |
239 |
send_invitation(inv) |
240 |
inviter.invitations = max(0, inviter.invitations - 1) |
241 |
inviter.save() |
242 |
|
243 |
|
244 |
### PROJECT FUNCTIONS ###
|
245 |
|
246 |
|
247 |
class ProjectError(Exception): |
248 |
pass
|
249 |
|
250 |
|
251 |
class ProjectNotFound(ProjectError): |
252 |
pass
|
253 |
|
254 |
|
255 |
class ProjectForbidden(ProjectError): |
256 |
pass
|
257 |
|
258 |
|
259 |
class ProjectBadRequest(ProjectError): |
260 |
pass
|
261 |
|
262 |
|
263 |
class ProjectConflict(ProjectError): |
264 |
pass
|
265 |
|
266 |
AUTO_ACCEPT_POLICY = 1
|
267 |
MODERATED_POLICY = 2
|
268 |
CLOSED_POLICY = 3
|
269 |
|
270 |
POLICIES = [AUTO_ACCEPT_POLICY, MODERATED_POLICY, CLOSED_POLICY] |
271 |
|
272 |
|
273 |
def get_related_project_id(application_id): |
274 |
try:
|
275 |
app = ProjectApplication.objects.get(id=application_id) |
276 |
return app.chain_id
|
277 |
except ProjectApplication.DoesNotExist:
|
278 |
return None |
279 |
|
280 |
|
281 |
def get_project_by_id(project_id): |
282 |
try:
|
283 |
return Project.objects.get(id=project_id)
|
284 |
except Project.DoesNotExist:
|
285 |
m = _(astakos_messages.UNKNOWN_PROJECT_ID) % project_id |
286 |
raise ProjectNotFound(m)
|
287 |
|
288 |
|
289 |
def get_project_for_update(project_id): |
290 |
try:
|
291 |
return Project.objects.get_for_update(id=project_id)
|
292 |
except Project.DoesNotExist:
|
293 |
m = _(astakos_messages.UNKNOWN_PROJECT_ID) % project_id |
294 |
raise ProjectNotFound(m)
|
295 |
|
296 |
|
297 |
def get_project_of_application_for_update(app_id): |
298 |
app = get_application(app_id) |
299 |
return get_project_for_update(app.chain_id)
|
300 |
|
301 |
|
302 |
def get_project_lock(): |
303 |
ProjectLock.objects.get_for_update(pk=1)
|
304 |
|
305 |
|
306 |
def get_application(application_id): |
307 |
try:
|
308 |
return ProjectApplication.objects.get(id=application_id)
|
309 |
except ProjectApplication.DoesNotExist:
|
310 |
m = _(astakos_messages.UNKNOWN_PROJECT_APPLICATION_ID) % application_id |
311 |
raise ProjectNotFound(m)
|
312 |
|
313 |
|
314 |
def get_project_of_membership_for_update(memb_id): |
315 |
m = get_membership_by_id(memb_id) |
316 |
return get_project_for_update(m.project_id)
|
317 |
|
318 |
|
319 |
def get_user_by_uuid(uuid): |
320 |
try:
|
321 |
return AstakosUser.objects.get(uuid=uuid)
|
322 |
except AstakosUser.DoesNotExist:
|
323 |
m = _(astakos_messages.UNKNOWN_USER_ID) % uuid |
324 |
raise ProjectNotFound(m)
|
325 |
|
326 |
|
327 |
def get_membership(project_id, user_id): |
328 |
try:
|
329 |
objs = ProjectMembership.objects.select_related('project', 'person') |
330 |
return objs.get(project__id=project_id, person__id=user_id)
|
331 |
except ProjectMembership.DoesNotExist:
|
332 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
333 |
raise ProjectNotFound(m)
|
334 |
|
335 |
|
336 |
def get_membership_by_id(memb_id): |
337 |
try:
|
338 |
objs = ProjectMembership.objects.select_related('project', 'person') |
339 |
return objs.get(id=memb_id)
|
340 |
except ProjectMembership.DoesNotExist:
|
341 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
342 |
raise ProjectNotFound(m)
|
343 |
|
344 |
|
345 |
ALLOWED_CHECKS = [ |
346 |
(lambda u, a: not u or u.is_project_admin()), |
347 |
(lambda u, a: a.owner == u),
|
348 |
(lambda u, a: a.applicant == u),
|
349 |
(lambda u, a: a.chain.overall_state() == Project.O_ACTIVE
|
350 |
or bool(a.chain.projectmembership_set.any_accepted().filter(person=u))), |
351 |
] |
352 |
|
353 |
ADMIN_LEVEL = 0
|
354 |
OWNER_LEVEL = 1
|
355 |
APPLICANT_LEVEL = 2
|
356 |
ANY_LEVEL = 3
|
357 |
|
358 |
|
359 |
def _check_yield(b, silent=False): |
360 |
if b:
|
361 |
return True |
362 |
|
363 |
if silent:
|
364 |
return False |
365 |
|
366 |
m = _(astakos_messages.NOT_ALLOWED) |
367 |
raise ProjectForbidden(m)
|
368 |
|
369 |
|
370 |
def membership_check_allowed(membership, request_user, |
371 |
level=OWNER_LEVEL, silent=False):
|
372 |
r = project_check_allowed( |
373 |
membership.project, request_user, level, silent=True)
|
374 |
|
375 |
return _check_yield(r or membership.person == request_user, silent) |
376 |
|
377 |
|
378 |
def project_check_allowed(project, request_user, |
379 |
level=OWNER_LEVEL, silent=False):
|
380 |
return app_check_allowed(project.application, request_user, level, silent)
|
381 |
|
382 |
|
383 |
def app_check_allowed(application, request_user, |
384 |
level=OWNER_LEVEL, silent=False):
|
385 |
checks = (f(request_user, application) for f in ALLOWED_CHECKS[:level+1]) |
386 |
return _check_yield(any(checks), silent) |
387 |
|
388 |
|
389 |
def checkAlive(project): |
390 |
if not project.is_alive: |
391 |
m = _(astakos_messages.NOT_ALIVE_PROJECT) % project.id |
392 |
raise ProjectConflict(m)
|
393 |
|
394 |
|
395 |
def accept_membership_project_checks(project, request_user): |
396 |
project_check_allowed(project, request_user) |
397 |
checkAlive(project) |
398 |
|
399 |
join_policy = project.application.member_join_policy |
400 |
if join_policy == CLOSED_POLICY:
|
401 |
m = _(astakos_messages.MEMBER_JOIN_POLICY_CLOSED) |
402 |
raise ProjectConflict(m)
|
403 |
|
404 |
if project.violates_members_limit(adding=1): |
405 |
m = _(astakos_messages.MEMBER_NUMBER_LIMIT_REACHED) |
406 |
raise ProjectConflict(m)
|
407 |
|
408 |
|
409 |
def accept_membership_checks(membership, request_user): |
410 |
if not membership.check_action("accept"): |
411 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
412 |
raise ProjectConflict(m)
|
413 |
|
414 |
project = membership.project |
415 |
accept_membership_project_checks(project, request_user) |
416 |
|
417 |
|
418 |
def accept_membership(memb_id, request_user=None, reason=None): |
419 |
project = get_project_of_membership_for_update(memb_id) |
420 |
membership = get_membership_by_id(memb_id) |
421 |
accept_membership_checks(membership, request_user) |
422 |
user = membership.person |
423 |
membership.perform_action("accept", actor=request_user, reason=reason)
|
424 |
qh_sync_user(user) |
425 |
logger.info("User %s has been accepted in %s." %
|
426 |
(user.log_display, project)) |
427 |
|
428 |
membership_change_notify(project, user, 'accepted')
|
429 |
return membership
|
430 |
|
431 |
|
432 |
def reject_membership_checks(membership, request_user): |
433 |
if not membership.check_action("reject"): |
434 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
435 |
raise ProjectConflict(m)
|
436 |
|
437 |
project = membership.project |
438 |
project_check_allowed(project, request_user) |
439 |
checkAlive(project) |
440 |
|
441 |
|
442 |
def reject_membership(memb_id, request_user=None, reason=None): |
443 |
project = get_project_of_membership_for_update(memb_id) |
444 |
membership = get_membership_by_id(memb_id) |
445 |
reject_membership_checks(membership, request_user) |
446 |
user = membership.person |
447 |
membership.perform_action("reject", actor=request_user, reason=reason)
|
448 |
logger.info("Request of user %s for %s has been rejected." %
|
449 |
(user.log_display, project)) |
450 |
|
451 |
membership_change_notify(project, user, 'rejected')
|
452 |
return membership
|
453 |
|
454 |
|
455 |
def cancel_membership_checks(membership, request_user): |
456 |
if not membership.check_action("cancel"): |
457 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
458 |
raise ProjectConflict(m)
|
459 |
|
460 |
membership_check_allowed(membership, request_user, level=ADMIN_LEVEL) |
461 |
project = membership.project |
462 |
checkAlive(project) |
463 |
|
464 |
|
465 |
def cancel_membership(memb_id, request_user, reason=None): |
466 |
project = get_project_of_membership_for_update(memb_id) |
467 |
membership = get_membership_by_id(memb_id) |
468 |
cancel_membership_checks(membership, request_user) |
469 |
membership.perform_action("cancel", actor=request_user, reason=reason)
|
470 |
logger.info("Request of user %s for %s has been cancelled." %
|
471 |
(membership.person.log_display, project)) |
472 |
|
473 |
|
474 |
def remove_membership_checks(membership, request_user=None): |
475 |
if not membership.check_action("remove"): |
476 |
m = _(astakos_messages.NOT_ACCEPTED_MEMBERSHIP) |
477 |
raise ProjectConflict(m)
|
478 |
|
479 |
project = membership.project |
480 |
project_check_allowed(project, request_user) |
481 |
checkAlive(project) |
482 |
|
483 |
leave_policy = project.application.member_leave_policy |
484 |
if leave_policy == CLOSED_POLICY:
|
485 |
m = _(astakos_messages.MEMBER_LEAVE_POLICY_CLOSED) |
486 |
raise ProjectConflict(m)
|
487 |
|
488 |
|
489 |
def remove_membership(memb_id, request_user=None, reason=None): |
490 |
project = get_project_of_membership_for_update(memb_id) |
491 |
membership = get_membership_by_id(memb_id) |
492 |
remove_membership_checks(membership, request_user) |
493 |
user = membership.person |
494 |
membership.perform_action("remove", actor=request_user, reason=reason)
|
495 |
qh_sync_user(user) |
496 |
logger.info("User %s has been removed from %s." %
|
497 |
(user.log_display, project)) |
498 |
|
499 |
membership_change_notify(project, user, 'removed')
|
500 |
return membership
|
501 |
|
502 |
|
503 |
def enroll_member(project_id, user, request_user=None, reason=None): |
504 |
project = get_project_for_update(project_id) |
505 |
try:
|
506 |
project = get_project_for_update(project_id) |
507 |
except ProjectNotFound as e: |
508 |
raise ProjectConflict(e.message)
|
509 |
accept_membership_project_checks(project, request_user) |
510 |
|
511 |
try:
|
512 |
membership = get_membership(project_id, user.id) |
513 |
if not membership.check_action("enroll"): |
514 |
m = _(astakos_messages.MEMBERSHIP_ACCEPTED) |
515 |
raise ProjectConflict(m)
|
516 |
membership.perform_action("join", actor=request_user, reason=reason)
|
517 |
except ProjectNotFound:
|
518 |
membership = new_membership(project, user, actor=request_user) |
519 |
|
520 |
membership.perform_action("accept", actor=request_user, reason=reason)
|
521 |
qh_sync_user(user) |
522 |
logger.info("User %s has been enrolled in %s." %
|
523 |
(membership.person.log_display, project)) |
524 |
|
525 |
membership_enroll_notify(project, membership.person) |
526 |
return membership
|
527 |
|
528 |
|
529 |
def leave_project_checks(membership, request_user): |
530 |
if not membership.check_action("leave"): |
531 |
m = _(astakos_messages.NOT_ACCEPTED_MEMBERSHIP) |
532 |
raise ProjectConflict(m)
|
533 |
|
534 |
membership_check_allowed(membership, request_user, level=ADMIN_LEVEL) |
535 |
project = membership.project |
536 |
checkAlive(project) |
537 |
|
538 |
leave_policy = project.application.member_leave_policy |
539 |
if leave_policy == CLOSED_POLICY:
|
540 |
m = _(astakos_messages.MEMBER_LEAVE_POLICY_CLOSED) |
541 |
raise ProjectConflict(m)
|
542 |
|
543 |
|
544 |
def can_leave_request(project, user): |
545 |
m = user.get_membership(project) |
546 |
if m is None: |
547 |
return False |
548 |
try:
|
549 |
leave_project_checks(m, user) |
550 |
except ProjectError:
|
551 |
return False |
552 |
return True |
553 |
|
554 |
|
555 |
def leave_project(memb_id, request_user, reason=None): |
556 |
project = get_project_of_membership_for_update(memb_id) |
557 |
membership = get_membership_by_id(memb_id) |
558 |
leave_project_checks(membership, request_user) |
559 |
|
560 |
auto_accepted = False
|
561 |
leave_policy = project.application.member_leave_policy |
562 |
if leave_policy == AUTO_ACCEPT_POLICY:
|
563 |
membership.perform_action("remove", actor=request_user, reason=reason)
|
564 |
qh_sync_user(request_user) |
565 |
logger.info("User %s has left %s." %
|
566 |
(request_user.log_display, project)) |
567 |
auto_accepted = True
|
568 |
else:
|
569 |
membership.perform_action("leave_request", actor=request_user,
|
570 |
reason=reason) |
571 |
logger.info("User %s requested to leave %s." %
|
572 |
(request_user.log_display, project)) |
573 |
membership_leave_request_notify(project, membership.person) |
574 |
return auto_accepted
|
575 |
|
576 |
|
577 |
def join_project_checks(project): |
578 |
checkAlive(project) |
579 |
|
580 |
join_policy = project.application.member_join_policy |
581 |
if join_policy == CLOSED_POLICY:
|
582 |
m = _(astakos_messages.MEMBER_JOIN_POLICY_CLOSED) |
583 |
raise ProjectConflict(m)
|
584 |
|
585 |
|
586 |
def can_join_request(project, user): |
587 |
try:
|
588 |
join_project_checks(project) |
589 |
except ProjectError:
|
590 |
return False |
591 |
|
592 |
m = user.get_membership(project) |
593 |
if not m: |
594 |
return True |
595 |
return m.check_action("join") |
596 |
|
597 |
|
598 |
def new_membership(project, user, actor=None, reason=None): |
599 |
m = ProjectMembership.objects.create(project=project, person=user) |
600 |
m._log_create(None, ProjectMembership.REQUESTED, actor=actor,
|
601 |
reason=reason) |
602 |
return m
|
603 |
|
604 |
|
605 |
def join_project(project_id, request_user, reason=None): |
606 |
project = get_project_for_update(project_id) |
607 |
join_project_checks(project) |
608 |
|
609 |
try:
|
610 |
membership = get_membership(project.id, request_user.id) |
611 |
if not membership.check_action("join"): |
612 |
msg = _(astakos_messages.MEMBERSHIP_ASSOCIATED) |
613 |
raise ProjectConflict(msg)
|
614 |
membership.perform_action("join", actor=request_user, reason=reason)
|
615 |
except ProjectNotFound:
|
616 |
membership = new_membership(project, request_user, actor=request_user, |
617 |
reason=reason) |
618 |
|
619 |
join_policy = project.application.member_join_policy |
620 |
if (join_policy == AUTO_ACCEPT_POLICY and ( |
621 |
not project.violates_members_limit(adding=1))): |
622 |
membership.perform_action("accept", actor=request_user, reason=reason)
|
623 |
qh_sync_user(request_user) |
624 |
logger.info("User %s joined %s." %
|
625 |
(request_user.log_display, project)) |
626 |
else:
|
627 |
membership_request_notify(project, membership.person) |
628 |
logger.info("User %s requested to join %s." %
|
629 |
(request_user.log_display, project)) |
630 |
return membership
|
631 |
|
632 |
|
633 |
MEMBERSHIP_ACTION_CHECKS = { |
634 |
"leave": leave_project_checks,
|
635 |
"cancel": cancel_membership_checks,
|
636 |
"accept": accept_membership_checks,
|
637 |
"reject": reject_membership_checks,
|
638 |
"remove": remove_membership_checks,
|
639 |
} |
640 |
|
641 |
|
642 |
def membership_allowed_actions(membership, request_user): |
643 |
allowed = [] |
644 |
for action, check in MEMBERSHIP_ACTION_CHECKS.iteritems(): |
645 |
try:
|
646 |
check(membership, request_user) |
647 |
allowed.append(action) |
648 |
except ProjectError:
|
649 |
pass
|
650 |
return allowed
|
651 |
|
652 |
|
653 |
def submit_application(owner=None, |
654 |
name=None,
|
655 |
project_id=None,
|
656 |
homepage=None,
|
657 |
description=None,
|
658 |
start_date=None,
|
659 |
end_date=None,
|
660 |
member_join_policy=None,
|
661 |
member_leave_policy=None,
|
662 |
limit_on_members_number=None,
|
663 |
comments=None,
|
664 |
resources=None,
|
665 |
request_user=None):
|
666 |
|
667 |
project = None
|
668 |
if project_id is not None: |
669 |
project = get_project_for_update(project_id) |
670 |
project_check_allowed(project, request_user, level=APPLICANT_LEVEL) |
671 |
|
672 |
policies = validate_resource_policies(resources) |
673 |
|
674 |
force = request_user.is_project_admin() |
675 |
ok, limit = qh_add_pending_app(owner, project, force) |
676 |
if not ok: |
677 |
m = _(astakos_messages.REACHED_PENDING_APPLICATION_LIMIT) % limit |
678 |
raise ProjectConflict(m)
|
679 |
|
680 |
application = ProjectApplication( |
681 |
applicant=request_user, |
682 |
owner=owner, |
683 |
name=name, |
684 |
homepage=homepage, |
685 |
description=description, |
686 |
start_date=start_date, |
687 |
end_date=end_date, |
688 |
member_join_policy=member_join_policy, |
689 |
member_leave_policy=member_leave_policy, |
690 |
limit_on_members_number=limit_on_members_number, |
691 |
comments=comments) |
692 |
|
693 |
if project is None: |
694 |
chain = new_chain() |
695 |
application.chain_id = chain.chain |
696 |
application.save() |
697 |
Project.objects.create(id=chain.chain, application=application) |
698 |
else:
|
699 |
application.chain = project |
700 |
application.save() |
701 |
if project.application.state != ProjectApplication.APPROVED:
|
702 |
project.application = application |
703 |
project.save() |
704 |
|
705 |
pending = ProjectApplication.objects.filter( |
706 |
chain=project, |
707 |
state=ProjectApplication.PENDING).exclude(id=application.id) |
708 |
for app in pending: |
709 |
app.state = ProjectApplication.REPLACED |
710 |
app.save() |
711 |
|
712 |
if policies is not None: |
713 |
set_resource_policies(application, policies) |
714 |
logger.info("User %s submitted %s." %
|
715 |
(request_user.log_display, application.log_display)) |
716 |
application_submit_notify(application) |
717 |
return application
|
718 |
|
719 |
|
720 |
def validate_resource_policies(policies): |
721 |
if not isinstance(policies, dict): |
722 |
raise ProjectBadRequest("Malformed resource policies") |
723 |
|
724 |
resource_names = policies.keys() |
725 |
resources = Resource.objects.filter(name__in=resource_names) |
726 |
resource_d = {} |
727 |
for resource in resources: |
728 |
resource_d[resource.name] = resource |
729 |
|
730 |
found = resource_d.keys() |
731 |
nonex = [name for name in resource_names if name not in found] |
732 |
if nonex:
|
733 |
raise ProjectBadRequest("Malformed resource policies") |
734 |
|
735 |
pols = [] |
736 |
for resource_name, specs in policies.iteritems(): |
737 |
p_capacity = specs.get("project_capacity")
|
738 |
m_capacity = specs.get("member_capacity")
|
739 |
|
740 |
if p_capacity is not None and not isinstance(p_capacity, (int, long)): |
741 |
raise ProjectBadRequest("Malformed resource policies") |
742 |
if not isinstance(m_capacity, (int, long)): |
743 |
raise ProjectBadRequest("Malformed resource policies") |
744 |
pols.append((resource_d[resource_name], m_capacity, p_capacity)) |
745 |
return pols
|
746 |
|
747 |
|
748 |
def set_resource_policies(application, policies): |
749 |
for resource, m_capacity, p_capacity in policies: |
750 |
g = application.projectresourcegrant_set |
751 |
g.create(resource=resource, |
752 |
member_capacity=m_capacity, |
753 |
project_capacity=p_capacity) |
754 |
|
755 |
|
756 |
def cancel_application(application_id, request_user=None, reason=""): |
757 |
get_project_of_application_for_update(application_id) |
758 |
application = get_application(application_id) |
759 |
app_check_allowed(application, request_user, level=APPLICANT_LEVEL) |
760 |
|
761 |
if not application.can_cancel(): |
762 |
m = _(astakos_messages.APPLICATION_CANNOT_CANCEL % |
763 |
(application.id, application.state_display())) |
764 |
raise ProjectConflict(m)
|
765 |
|
766 |
qh_release_pending_app(application.owner) |
767 |
|
768 |
application.cancel(actor=request_user, reason=reason) |
769 |
logger.info("%s has been cancelled." % (application.log_display))
|
770 |
|
771 |
|
772 |
def dismiss_application(application_id, request_user=None, reason=""): |
773 |
get_project_of_application_for_update(application_id) |
774 |
application = get_application(application_id) |
775 |
app_check_allowed(application, request_user, level=APPLICANT_LEVEL) |
776 |
|
777 |
if not application.can_dismiss(): |
778 |
m = _(astakos_messages.APPLICATION_CANNOT_DISMISS % |
779 |
(application.id, application.state_display())) |
780 |
raise ProjectConflict(m)
|
781 |
|
782 |
application.dismiss(actor=request_user, reason=reason) |
783 |
logger.info("%s has been dismissed." % (application.log_display))
|
784 |
|
785 |
|
786 |
def deny_application(application_id, request_user=None, reason=""): |
787 |
get_project_of_application_for_update(application_id) |
788 |
application = get_application(application_id) |
789 |
|
790 |
app_check_allowed(application, request_user, level=ADMIN_LEVEL) |
791 |
|
792 |
if not application.can_deny(): |
793 |
m = _(astakos_messages.APPLICATION_CANNOT_DENY % |
794 |
(application.id, application.state_display())) |
795 |
raise ProjectConflict(m)
|
796 |
|
797 |
qh_release_pending_app(application.owner) |
798 |
|
799 |
application.deny(actor=request_user, reason=reason) |
800 |
logger.info("%s has been denied with reason \"%s\"." %
|
801 |
(application.log_display, reason)) |
802 |
application_deny_notify(application) |
803 |
|
804 |
|
805 |
def check_conflicting_projects(application): |
806 |
project = application.chain |
807 |
new_project_name = application.name |
808 |
try:
|
809 |
q = Q(name=new_project_name) & ~Q(state=Project.TERMINATED) |
810 |
conflicting_project = Project.objects.get(q) |
811 |
if (conflicting_project != project):
|
812 |
m = (_("cannot approve: project with name '%s' "
|
813 |
"already exists (id: %s)") %
|
814 |
(new_project_name, conflicting_project.id)) |
815 |
raise ProjectConflict(m) # invalid argument |
816 |
except Project.DoesNotExist:
|
817 |
pass
|
818 |
|
819 |
|
820 |
def approve_application(app_id, request_user=None, reason=""): |
821 |
get_project_lock() |
822 |
project = get_project_of_application_for_update(app_id) |
823 |
application = get_application(app_id) |
824 |
|
825 |
app_check_allowed(application, request_user, level=ADMIN_LEVEL) |
826 |
|
827 |
if not application.can_approve(): |
828 |
m = _(astakos_messages.APPLICATION_CANNOT_APPROVE % |
829 |
(application.id, application.state_display())) |
830 |
raise ProjectConflict(m)
|
831 |
|
832 |
check_conflicting_projects(application) |
833 |
|
834 |
# Pre-lock members and owner together in order to impose an ordering
|
835 |
# on locking users
|
836 |
members = members_to_sync(project) |
837 |
uids_to_sync = [member.id for member in members] |
838 |
owner = application.owner |
839 |
uids_to_sync.append(owner.id) |
840 |
get_users_for_update(uids_to_sync) |
841 |
|
842 |
qh_release_pending_app(owner, locked=True)
|
843 |
application.approve(actor=request_user, reason=reason) |
844 |
project.application = application |
845 |
project.name = application.name |
846 |
project.save() |
847 |
if project.is_deactivated():
|
848 |
project.resume(actor=request_user, reason="APPROVE")
|
849 |
qh_sync_locked_users(members) |
850 |
logger.info("%s has been approved." % (application.log_display))
|
851 |
application_approve_notify(application) |
852 |
|
853 |
|
854 |
def check_expiration(execute=False): |
855 |
objects = Project.objects |
856 |
expired = objects.expired_projects() |
857 |
if execute:
|
858 |
for project in expired: |
859 |
terminate(project.pk) |
860 |
|
861 |
return [project.expiration_info() for project in expired] |
862 |
|
863 |
|
864 |
def terminate(project_id, request_user=None, reason=None): |
865 |
project = get_project_for_update(project_id) |
866 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
867 |
checkAlive(project) |
868 |
|
869 |
project.terminate(actor=request_user, reason=reason) |
870 |
qh_sync_project(project) |
871 |
logger.info("%s has been terminated." % (project))
|
872 |
|
873 |
project_termination_notify(project) |
874 |
|
875 |
|
876 |
def suspend(project_id, request_user=None, reason=None): |
877 |
project = get_project_for_update(project_id) |
878 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
879 |
checkAlive(project) |
880 |
|
881 |
project.suspend(actor=request_user, reason=reason) |
882 |
qh_sync_project(project) |
883 |
logger.info("%s has been suspended." % (project))
|
884 |
|
885 |
project_suspension_notify(project) |
886 |
|
887 |
|
888 |
def unsuspend(project_id, request_user=None, reason=None): |
889 |
project = get_project_for_update(project_id) |
890 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
891 |
|
892 |
if not project.is_suspended: |
893 |
m = _(astakos_messages.NOT_SUSPENDED_PROJECT) % project.id |
894 |
raise ProjectConflict(m)
|
895 |
|
896 |
project.resume(actor=request_user, reason=reason) |
897 |
qh_sync_project(project) |
898 |
logger.info("%s has been unsuspended." % (project))
|
899 |
project_unsuspension_notify(project) |
900 |
|
901 |
|
902 |
def reinstate(project_id, request_user=None, reason=None): |
903 |
get_project_lock() |
904 |
project = get_project_for_update(project_id) |
905 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
906 |
|
907 |
if not project.is_terminated: |
908 |
m = _(astakos_messages.NOT_TERMINATED_PROJECT) % project.id |
909 |
raise ProjectConflict(m)
|
910 |
|
911 |
check_conflicting_projects(project.application) |
912 |
project.resume(actor=request_user, reason=reason) |
913 |
qh_sync_project(project) |
914 |
logger.info("%s has been reinstated" % (project))
|
915 |
project_reinstatement_notify(project) |
916 |
|
917 |
|
918 |
def _partition_by(f, l): |
919 |
d = {} |
920 |
for x in l: |
921 |
group = f(x) |
922 |
group_l = d.get(group, []) |
923 |
group_l.append(x) |
924 |
d[group] = group_l |
925 |
return d
|
926 |
|
927 |
|
928 |
def count_pending_app(users): |
929 |
users = list(users)
|
930 |
apps = ProjectApplication.objects.filter(state=ProjectApplication.PENDING, |
931 |
owner__in=users) |
932 |
apps_d = _partition_by(lambda a: a.owner.uuid, apps)
|
933 |
|
934 |
usage = {} |
935 |
for user in users: |
936 |
uuid = user.uuid |
937 |
usage[uuid] = len(apps_d.get(uuid, []))
|
938 |
return usage
|
939 |
|
940 |
|
941 |
def get_pending_app_diff(user, project): |
942 |
if project is None: |
943 |
diff = 1
|
944 |
else:
|
945 |
objs = ProjectApplication.objects |
946 |
q = objs.filter(chain=project, state=ProjectApplication.PENDING) |
947 |
count = q.count() |
948 |
diff = 1 - count
|
949 |
return diff
|
950 |
|
951 |
|
952 |
def qh_add_pending_app(user, project=None, force=False): |
953 |
user = AstakosUser.forupdate.get_for_update(id=user.id) |
954 |
diff = get_pending_app_diff(user, project) |
955 |
return register_pending_apps(user, diff, force)
|
956 |
|
957 |
|
958 |
def check_pending_app_quota(user, project=None): |
959 |
diff = get_pending_app_diff(user, project) |
960 |
quota = get_pending_app_quota(user) |
961 |
limit = quota['limit']
|
962 |
usage = quota['usage']
|
963 |
if usage + diff > limit:
|
964 |
return False, limit |
965 |
return True, None |
966 |
|
967 |
|
968 |
def qh_release_pending_app(user, locked=False): |
969 |
if not locked: |
970 |
user = AstakosUser.forupdate.get_for_update(id=user.id) |
971 |
register_pending_apps(user, -1)
|