root / snf-astakos-app / astakos / im / functions.py @ c7e03d20
History | View | Annotate | Download (33.3 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.select_related(
|
284 |
"application", "application__owner", |
285 |
"application__applicant").get(id=project_id)
|
286 |
except Project.DoesNotExist:
|
287 |
m = _(astakos_messages.UNKNOWN_PROJECT_ID) % project_id |
288 |
raise ProjectNotFound(m)
|
289 |
|
290 |
|
291 |
def get_project_for_update(project_id): |
292 |
try:
|
293 |
return Project.objects.select_for_update().get(id=project_id)
|
294 |
except Project.DoesNotExist:
|
295 |
m = _(astakos_messages.UNKNOWN_PROJECT_ID) % project_id |
296 |
raise ProjectNotFound(m)
|
297 |
|
298 |
|
299 |
def get_project_of_application_for_update(app_id): |
300 |
app = get_application(app_id) |
301 |
return get_project_for_update(app.chain_id)
|
302 |
|
303 |
|
304 |
def get_project_lock(): |
305 |
ProjectLock.objects.select_for_update().get(pk=1)
|
306 |
|
307 |
|
308 |
def get_application(application_id): |
309 |
try:
|
310 |
return ProjectApplication.objects.get(id=application_id)
|
311 |
except ProjectApplication.DoesNotExist:
|
312 |
m = _(astakos_messages.UNKNOWN_PROJECT_APPLICATION_ID) % application_id |
313 |
raise ProjectNotFound(m)
|
314 |
|
315 |
|
316 |
def get_project_of_membership_for_update(memb_id): |
317 |
m = get_membership_by_id(memb_id) |
318 |
return get_project_for_update(m.project_id)
|
319 |
|
320 |
|
321 |
def get_user_by_uuid(uuid): |
322 |
try:
|
323 |
return AstakosUser.objects.get(uuid=uuid)
|
324 |
except AstakosUser.DoesNotExist:
|
325 |
m = _(astakos_messages.UNKNOWN_USER_ID) % uuid |
326 |
raise ProjectNotFound(m)
|
327 |
|
328 |
|
329 |
def get_membership(project_id, user_id): |
330 |
try:
|
331 |
objs = ProjectMembership.objects.select_related('project', 'person') |
332 |
return objs.get(project__id=project_id, person__id=user_id)
|
333 |
except ProjectMembership.DoesNotExist:
|
334 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
335 |
raise ProjectNotFound(m)
|
336 |
|
337 |
|
338 |
def get_membership_by_id(memb_id): |
339 |
try:
|
340 |
objs = ProjectMembership.objects.select_related('project', 'person') |
341 |
return objs.get(id=memb_id)
|
342 |
except ProjectMembership.DoesNotExist:
|
343 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
344 |
raise ProjectNotFound(m)
|
345 |
|
346 |
|
347 |
ALLOWED_CHECKS = [ |
348 |
(lambda u, a: not u or u.is_project_admin()), |
349 |
(lambda u, a: a.owner == u),
|
350 |
(lambda u, a: a.applicant == u),
|
351 |
(lambda u, a: a.chain.overall_state() == Project.O_ACTIVE
|
352 |
or bool(a.chain.projectmembership_set.any_accepted().filter(person=u))), |
353 |
] |
354 |
|
355 |
ADMIN_LEVEL = 0
|
356 |
OWNER_LEVEL = 1
|
357 |
APPLICANT_LEVEL = 2
|
358 |
ANY_LEVEL = 3
|
359 |
|
360 |
|
361 |
def _check_yield(b, silent=False): |
362 |
if b:
|
363 |
return True |
364 |
|
365 |
if silent:
|
366 |
return False |
367 |
|
368 |
m = _(astakos_messages.NOT_ALLOWED) |
369 |
raise ProjectForbidden(m)
|
370 |
|
371 |
|
372 |
def membership_check_allowed(membership, request_user, |
373 |
level=OWNER_LEVEL, silent=False):
|
374 |
r = project_check_allowed( |
375 |
membership.project, request_user, level, silent=True)
|
376 |
|
377 |
return _check_yield(r or membership.person == request_user, silent) |
378 |
|
379 |
|
380 |
def project_check_allowed(project, request_user, |
381 |
level=OWNER_LEVEL, silent=False):
|
382 |
return app_check_allowed(project.application, request_user, level, silent)
|
383 |
|
384 |
|
385 |
def app_check_allowed(application, request_user, |
386 |
level=OWNER_LEVEL, silent=False):
|
387 |
checks = (f(request_user, application) for f in ALLOWED_CHECKS[:level+1]) |
388 |
return _check_yield(any(checks), silent) |
389 |
|
390 |
|
391 |
def checkAlive(project): |
392 |
if not project.is_alive: |
393 |
m = _(astakos_messages.NOT_ALIVE_PROJECT) % project.id |
394 |
raise ProjectConflict(m)
|
395 |
|
396 |
|
397 |
def accept_membership_project_checks(project, request_user): |
398 |
project_check_allowed(project, request_user) |
399 |
checkAlive(project) |
400 |
|
401 |
join_policy = project.application.member_join_policy |
402 |
if join_policy == CLOSED_POLICY:
|
403 |
m = _(astakos_messages.MEMBER_JOIN_POLICY_CLOSED) |
404 |
raise ProjectConflict(m)
|
405 |
|
406 |
if project.violates_members_limit(adding=1): |
407 |
m = _(astakos_messages.MEMBER_NUMBER_LIMIT_REACHED) |
408 |
raise ProjectConflict(m)
|
409 |
|
410 |
|
411 |
def accept_membership_checks(membership, request_user): |
412 |
if not membership.check_action("accept"): |
413 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
414 |
raise ProjectConflict(m)
|
415 |
|
416 |
project = membership.project |
417 |
accept_membership_project_checks(project, request_user) |
418 |
|
419 |
|
420 |
def accept_membership(memb_id, request_user=None, reason=None): |
421 |
project = get_project_of_membership_for_update(memb_id) |
422 |
membership = get_membership_by_id(memb_id) |
423 |
accept_membership_checks(membership, request_user) |
424 |
user = membership.person |
425 |
membership.perform_action("accept", actor=request_user, reason=reason)
|
426 |
qh_sync_user(user) |
427 |
logger.info("User %s has been accepted in %s." %
|
428 |
(user.log_display, project)) |
429 |
|
430 |
membership_change_notify(project, user, 'accepted')
|
431 |
return membership
|
432 |
|
433 |
|
434 |
def reject_membership_checks(membership, request_user): |
435 |
if not membership.check_action("reject"): |
436 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
437 |
raise ProjectConflict(m)
|
438 |
|
439 |
project = membership.project |
440 |
project_check_allowed(project, request_user) |
441 |
checkAlive(project) |
442 |
|
443 |
|
444 |
def reject_membership(memb_id, request_user=None, reason=None): |
445 |
project = get_project_of_membership_for_update(memb_id) |
446 |
membership = get_membership_by_id(memb_id) |
447 |
reject_membership_checks(membership, request_user) |
448 |
user = membership.person |
449 |
membership.perform_action("reject", actor=request_user, reason=reason)
|
450 |
logger.info("Request of user %s for %s has been rejected." %
|
451 |
(user.log_display, project)) |
452 |
|
453 |
membership_change_notify(project, user, 'rejected')
|
454 |
return membership
|
455 |
|
456 |
|
457 |
def cancel_membership_checks(membership, request_user): |
458 |
if not membership.check_action("cancel"): |
459 |
m = _(astakos_messages.NOT_MEMBERSHIP_REQUEST) |
460 |
raise ProjectConflict(m)
|
461 |
|
462 |
membership_check_allowed(membership, request_user, level=ADMIN_LEVEL) |
463 |
project = membership.project |
464 |
checkAlive(project) |
465 |
|
466 |
|
467 |
def cancel_membership(memb_id, request_user, reason=None): |
468 |
project = get_project_of_membership_for_update(memb_id) |
469 |
membership = get_membership_by_id(memb_id) |
470 |
cancel_membership_checks(membership, request_user) |
471 |
membership.perform_action("cancel", actor=request_user, reason=reason)
|
472 |
logger.info("Request of user %s for %s has been cancelled." %
|
473 |
(membership.person.log_display, project)) |
474 |
|
475 |
|
476 |
def remove_membership_checks(membership, request_user=None): |
477 |
if not membership.check_action("remove"): |
478 |
m = _(astakos_messages.NOT_ACCEPTED_MEMBERSHIP) |
479 |
raise ProjectConflict(m)
|
480 |
|
481 |
project = membership.project |
482 |
project_check_allowed(project, request_user) |
483 |
checkAlive(project) |
484 |
|
485 |
leave_policy = project.application.member_leave_policy |
486 |
if leave_policy == CLOSED_POLICY:
|
487 |
m = _(astakos_messages.MEMBER_LEAVE_POLICY_CLOSED) |
488 |
raise ProjectConflict(m)
|
489 |
|
490 |
|
491 |
def remove_membership(memb_id, request_user=None, reason=None): |
492 |
project = get_project_of_membership_for_update(memb_id) |
493 |
membership = get_membership_by_id(memb_id) |
494 |
remove_membership_checks(membership, request_user) |
495 |
user = membership.person |
496 |
membership.perform_action("remove", actor=request_user, reason=reason)
|
497 |
qh_sync_user(user) |
498 |
logger.info("User %s has been removed from %s." %
|
499 |
(user.log_display, project)) |
500 |
|
501 |
membership_change_notify(project, user, 'removed')
|
502 |
return membership
|
503 |
|
504 |
|
505 |
def enroll_member_by_email(project_id, email, request_user=None, reason=None): |
506 |
try:
|
507 |
user = AstakosUser.objects.verified().get(email=email) |
508 |
return enroll_member(project_id, user, request_user, reason=reason)
|
509 |
except AstakosUser.DoesNotExist:
|
510 |
raise ProjectConflict(astakos_messages.UNKNOWN_USERS)
|
511 |
|
512 |
|
513 |
def enroll_member(project_id, user, request_user=None, reason=None): |
514 |
try:
|
515 |
project = get_project_for_update(project_id) |
516 |
except ProjectNotFound as e: |
517 |
raise ProjectConflict(e.message)
|
518 |
accept_membership_project_checks(project, request_user) |
519 |
|
520 |
try:
|
521 |
membership = get_membership(project_id, user.id) |
522 |
if not membership.check_action("enroll"): |
523 |
m = _(astakos_messages.MEMBERSHIP_ACCEPTED) |
524 |
raise ProjectConflict(m)
|
525 |
membership.perform_action("enroll", actor=request_user, reason=reason)
|
526 |
except ProjectNotFound:
|
527 |
membership = new_membership(project, user, actor=request_user, |
528 |
enroll=True)
|
529 |
|
530 |
qh_sync_user(user) |
531 |
logger.info("User %s has been enrolled in %s." %
|
532 |
(membership.person.log_display, project)) |
533 |
|
534 |
membership_enroll_notify(project, membership.person) |
535 |
return membership
|
536 |
|
537 |
|
538 |
def leave_project_checks(membership, request_user): |
539 |
if not membership.check_action("leave"): |
540 |
m = _(astakos_messages.NOT_ACCEPTED_MEMBERSHIP) |
541 |
raise ProjectConflict(m)
|
542 |
|
543 |
membership_check_allowed(membership, request_user, level=ADMIN_LEVEL) |
544 |
project = membership.project |
545 |
checkAlive(project) |
546 |
|
547 |
leave_policy = project.application.member_leave_policy |
548 |
if leave_policy == CLOSED_POLICY:
|
549 |
m = _(astakos_messages.MEMBER_LEAVE_POLICY_CLOSED) |
550 |
raise ProjectConflict(m)
|
551 |
|
552 |
|
553 |
def can_leave_request(project, user): |
554 |
m = user.get_membership(project) |
555 |
if m is None: |
556 |
return False |
557 |
try:
|
558 |
leave_project_checks(m, user) |
559 |
except ProjectError:
|
560 |
return False |
561 |
return True |
562 |
|
563 |
|
564 |
def leave_project(memb_id, request_user, reason=None): |
565 |
project = get_project_of_membership_for_update(memb_id) |
566 |
membership = get_membership_by_id(memb_id) |
567 |
leave_project_checks(membership, request_user) |
568 |
|
569 |
auto_accepted = False
|
570 |
leave_policy = project.application.member_leave_policy |
571 |
if leave_policy == AUTO_ACCEPT_POLICY:
|
572 |
membership.perform_action("remove", actor=request_user, reason=reason)
|
573 |
qh_sync_user(request_user) |
574 |
logger.info("User %s has left %s." %
|
575 |
(request_user.log_display, project)) |
576 |
auto_accepted = True
|
577 |
else:
|
578 |
membership.perform_action("leave_request", actor=request_user,
|
579 |
reason=reason) |
580 |
logger.info("User %s requested to leave %s." %
|
581 |
(request_user.log_display, project)) |
582 |
membership_leave_request_notify(project, membership.person) |
583 |
return auto_accepted
|
584 |
|
585 |
|
586 |
def join_project_checks(project): |
587 |
checkAlive(project) |
588 |
|
589 |
join_policy = project.application.member_join_policy |
590 |
if join_policy == CLOSED_POLICY:
|
591 |
m = _(astakos_messages.MEMBER_JOIN_POLICY_CLOSED) |
592 |
raise ProjectConflict(m)
|
593 |
|
594 |
|
595 |
Nothing = type('Nothing', (), {}) |
596 |
|
597 |
|
598 |
def can_join_request(project, user, membership=Nothing): |
599 |
try:
|
600 |
join_project_checks(project) |
601 |
except ProjectError:
|
602 |
return False |
603 |
|
604 |
m = (membership if membership is not Nothing |
605 |
else user.get_membership(project))
|
606 |
if not m: |
607 |
return True |
608 |
return m.check_action("join") |
609 |
|
610 |
|
611 |
def new_membership(project, user, actor=None, reason=None, enroll=False): |
612 |
state = (ProjectMembership.ACCEPTED if enroll
|
613 |
else ProjectMembership.REQUESTED)
|
614 |
m = ProjectMembership.objects.create( |
615 |
project=project, person=user, state=state) |
616 |
m._log_create(None, state, actor=actor, reason=reason)
|
617 |
return m
|
618 |
|
619 |
|
620 |
def join_project(project_id, request_user, reason=None): |
621 |
project = get_project_for_update(project_id) |
622 |
join_project_checks(project) |
623 |
|
624 |
try:
|
625 |
membership = get_membership(project.id, request_user.id) |
626 |
if not membership.check_action("join"): |
627 |
msg = _(astakos_messages.MEMBERSHIP_ASSOCIATED) |
628 |
raise ProjectConflict(msg)
|
629 |
membership.perform_action("join", actor=request_user, reason=reason)
|
630 |
except ProjectNotFound:
|
631 |
membership = new_membership(project, request_user, actor=request_user, |
632 |
reason=reason) |
633 |
|
634 |
join_policy = project.application.member_join_policy |
635 |
if (join_policy == AUTO_ACCEPT_POLICY and ( |
636 |
not project.violates_members_limit(adding=1))): |
637 |
membership.perform_action("accept", actor=request_user, reason=reason)
|
638 |
qh_sync_user(request_user) |
639 |
logger.info("User %s joined %s." %
|
640 |
(request_user.log_display, project)) |
641 |
else:
|
642 |
membership_request_notify(project, membership.person) |
643 |
logger.info("User %s requested to join %s." %
|
644 |
(request_user.log_display, project)) |
645 |
return membership
|
646 |
|
647 |
|
648 |
MEMBERSHIP_ACTION_CHECKS = { |
649 |
"leave": leave_project_checks,
|
650 |
"cancel": cancel_membership_checks,
|
651 |
"accept": accept_membership_checks,
|
652 |
"reject": reject_membership_checks,
|
653 |
"remove": remove_membership_checks,
|
654 |
} |
655 |
|
656 |
|
657 |
def membership_allowed_actions(membership, request_user): |
658 |
allowed = [] |
659 |
for action, check in MEMBERSHIP_ACTION_CHECKS.iteritems(): |
660 |
try:
|
661 |
check(membership, request_user) |
662 |
allowed.append(action) |
663 |
except ProjectError:
|
664 |
pass
|
665 |
return allowed
|
666 |
|
667 |
|
668 |
def submit_application(owner=None, |
669 |
name=None,
|
670 |
project_id=None,
|
671 |
homepage=None,
|
672 |
description=None,
|
673 |
start_date=None,
|
674 |
end_date=None,
|
675 |
member_join_policy=None,
|
676 |
member_leave_policy=None,
|
677 |
limit_on_members_number=None,
|
678 |
comments=None,
|
679 |
resources=None,
|
680 |
request_user=None):
|
681 |
|
682 |
project = None
|
683 |
if project_id is not None: |
684 |
project = get_project_for_update(project_id) |
685 |
project_check_allowed(project, request_user, level=APPLICANT_LEVEL) |
686 |
|
687 |
policies = validate_resource_policies(resources) |
688 |
|
689 |
force = request_user.is_project_admin() |
690 |
ok, limit = qh_add_pending_app(owner, project, force) |
691 |
if not ok: |
692 |
m = _(astakos_messages.REACHED_PENDING_APPLICATION_LIMIT) % limit |
693 |
raise ProjectConflict(m)
|
694 |
|
695 |
application = ProjectApplication( |
696 |
applicant=request_user, |
697 |
owner=owner, |
698 |
name=name, |
699 |
homepage=homepage, |
700 |
description=description, |
701 |
start_date=start_date, |
702 |
end_date=end_date, |
703 |
member_join_policy=member_join_policy, |
704 |
member_leave_policy=member_leave_policy, |
705 |
limit_on_members_number=limit_on_members_number, |
706 |
comments=comments) |
707 |
|
708 |
if project is None: |
709 |
chain = new_chain() |
710 |
application.chain_id = chain.chain |
711 |
application.save() |
712 |
Project.objects.create(id=chain.chain, application=application) |
713 |
else:
|
714 |
application.chain = project |
715 |
application.save() |
716 |
if project.application.state != ProjectApplication.APPROVED:
|
717 |
project.application = application |
718 |
project.save() |
719 |
|
720 |
pending = ProjectApplication.objects.filter( |
721 |
chain=project, |
722 |
state=ProjectApplication.PENDING).exclude(id=application.id) |
723 |
for app in pending: |
724 |
app.state = ProjectApplication.REPLACED |
725 |
app.save() |
726 |
|
727 |
if policies is not None: |
728 |
set_resource_policies(application, policies) |
729 |
logger.info("User %s submitted %s." %
|
730 |
(request_user.log_display, application.log_display)) |
731 |
application_submit_notify(application) |
732 |
return application
|
733 |
|
734 |
|
735 |
def validate_resource_policies(policies): |
736 |
if not isinstance(policies, dict): |
737 |
raise ProjectBadRequest("Malformed resource policies") |
738 |
|
739 |
resource_names = policies.keys() |
740 |
resources = Resource.objects.filter(name__in=resource_names) |
741 |
resource_d = {} |
742 |
for resource in resources: |
743 |
resource_d[resource.name] = resource |
744 |
|
745 |
found = resource_d.keys() |
746 |
nonex = [name for name in resource_names if name not in found] |
747 |
if nonex:
|
748 |
raise ProjectBadRequest("Malformed resource policies") |
749 |
|
750 |
pols = [] |
751 |
for resource_name, specs in policies.iteritems(): |
752 |
p_capacity = specs.get("project_capacity")
|
753 |
m_capacity = specs.get("member_capacity")
|
754 |
|
755 |
if p_capacity is not None and not isinstance(p_capacity, (int, long)): |
756 |
raise ProjectBadRequest("Malformed resource policies") |
757 |
if not isinstance(m_capacity, (int, long)): |
758 |
raise ProjectBadRequest("Malformed resource policies") |
759 |
pols.append((resource_d[resource_name], m_capacity, p_capacity)) |
760 |
return pols
|
761 |
|
762 |
|
763 |
def set_resource_policies(application, policies): |
764 |
for resource, m_capacity, p_capacity in policies: |
765 |
g = application.projectresourcegrant_set |
766 |
g.create(resource=resource, |
767 |
member_capacity=m_capacity, |
768 |
project_capacity=p_capacity) |
769 |
|
770 |
|
771 |
def cancel_application(application_id, request_user=None, reason=""): |
772 |
get_project_of_application_for_update(application_id) |
773 |
application = get_application(application_id) |
774 |
app_check_allowed(application, request_user, level=APPLICANT_LEVEL) |
775 |
|
776 |
if not application.can_cancel(): |
777 |
m = _(astakos_messages.APPLICATION_CANNOT_CANCEL % |
778 |
(application.id, application.state_display())) |
779 |
raise ProjectConflict(m)
|
780 |
|
781 |
qh_release_pending_app(application.owner) |
782 |
|
783 |
application.cancel(actor=request_user, reason=reason) |
784 |
logger.info("%s has been cancelled." % (application.log_display))
|
785 |
|
786 |
|
787 |
def dismiss_application(application_id, request_user=None, reason=""): |
788 |
get_project_of_application_for_update(application_id) |
789 |
application = get_application(application_id) |
790 |
app_check_allowed(application, request_user, level=APPLICANT_LEVEL) |
791 |
|
792 |
if not application.can_dismiss(): |
793 |
m = _(astakos_messages.APPLICATION_CANNOT_DISMISS % |
794 |
(application.id, application.state_display())) |
795 |
raise ProjectConflict(m)
|
796 |
|
797 |
application.dismiss(actor=request_user, reason=reason) |
798 |
logger.info("%s has been dismissed." % (application.log_display))
|
799 |
|
800 |
|
801 |
def deny_application(application_id, request_user=None, reason=""): |
802 |
get_project_of_application_for_update(application_id) |
803 |
application = get_application(application_id) |
804 |
|
805 |
app_check_allowed(application, request_user, level=ADMIN_LEVEL) |
806 |
|
807 |
if not application.can_deny(): |
808 |
m = _(astakos_messages.APPLICATION_CANNOT_DENY % |
809 |
(application.id, application.state_display())) |
810 |
raise ProjectConflict(m)
|
811 |
|
812 |
qh_release_pending_app(application.owner) |
813 |
|
814 |
application.deny(actor=request_user, reason=reason) |
815 |
logger.info("%s has been denied with reason \"%s\"." %
|
816 |
(application.log_display, reason)) |
817 |
application_deny_notify(application) |
818 |
|
819 |
|
820 |
def check_conflicting_projects(application): |
821 |
project = application.chain |
822 |
new_project_name = application.name |
823 |
try:
|
824 |
q = Q(name=new_project_name) & ~Q(state=Project.TERMINATED) |
825 |
conflicting_project = Project.objects.get(q) |
826 |
if (conflicting_project != project):
|
827 |
m = (_("cannot approve: project with name '%s' "
|
828 |
"already exists (id: %s)") %
|
829 |
(new_project_name, conflicting_project.id)) |
830 |
raise ProjectConflict(m) # invalid argument |
831 |
except Project.DoesNotExist:
|
832 |
pass
|
833 |
|
834 |
|
835 |
def approve_application(app_id, request_user=None, reason=""): |
836 |
get_project_lock() |
837 |
project = get_project_of_application_for_update(app_id) |
838 |
application = get_application(app_id) |
839 |
|
840 |
app_check_allowed(application, request_user, level=ADMIN_LEVEL) |
841 |
|
842 |
if not application.can_approve(): |
843 |
m = _(astakos_messages.APPLICATION_CANNOT_APPROVE % |
844 |
(application.id, application.state_display())) |
845 |
raise ProjectConflict(m)
|
846 |
|
847 |
check_conflicting_projects(application) |
848 |
|
849 |
# Pre-lock members and owner together in order to impose an ordering
|
850 |
# on locking users
|
851 |
members = members_to_sync(project) |
852 |
uids_to_sync = [member.id for member in members] |
853 |
owner = application.owner |
854 |
uids_to_sync.append(owner.id) |
855 |
get_users_for_update(uids_to_sync) |
856 |
|
857 |
qh_release_pending_app(owner, locked=True)
|
858 |
application.approve(actor=request_user, reason=reason) |
859 |
project.application = application |
860 |
project.name = application.name |
861 |
project.save() |
862 |
if project.is_deactivated():
|
863 |
project.resume(actor=request_user, reason="APPROVE")
|
864 |
qh_sync_locked_users(members) |
865 |
logger.info("%s has been approved." % (application.log_display))
|
866 |
application_approve_notify(application) |
867 |
|
868 |
|
869 |
def check_expiration(execute=False): |
870 |
objects = Project.objects |
871 |
expired = objects.expired_projects() |
872 |
if execute:
|
873 |
for project in expired: |
874 |
terminate(project.pk) |
875 |
|
876 |
return [project.expiration_info() for project in expired] |
877 |
|
878 |
|
879 |
def terminate(project_id, request_user=None, reason=None): |
880 |
project = get_project_for_update(project_id) |
881 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
882 |
checkAlive(project) |
883 |
|
884 |
project.terminate(actor=request_user, reason=reason) |
885 |
qh_sync_project(project) |
886 |
logger.info("%s has been terminated." % (project))
|
887 |
|
888 |
project_termination_notify(project) |
889 |
|
890 |
|
891 |
def suspend(project_id, request_user=None, reason=None): |
892 |
project = get_project_for_update(project_id) |
893 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
894 |
checkAlive(project) |
895 |
|
896 |
project.suspend(actor=request_user, reason=reason) |
897 |
qh_sync_project(project) |
898 |
logger.info("%s has been suspended." % (project))
|
899 |
|
900 |
project_suspension_notify(project) |
901 |
|
902 |
|
903 |
def unsuspend(project_id, request_user=None, reason=None): |
904 |
project = get_project_for_update(project_id) |
905 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
906 |
|
907 |
if not project.is_suspended: |
908 |
m = _(astakos_messages.NOT_SUSPENDED_PROJECT) % project.id |
909 |
raise ProjectConflict(m)
|
910 |
|
911 |
project.resume(actor=request_user, reason=reason) |
912 |
qh_sync_project(project) |
913 |
logger.info("%s has been unsuspended." % (project))
|
914 |
project_unsuspension_notify(project) |
915 |
|
916 |
|
917 |
def reinstate(project_id, request_user=None, reason=None): |
918 |
get_project_lock() |
919 |
project = get_project_for_update(project_id) |
920 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
921 |
|
922 |
if not project.is_terminated: |
923 |
m = _(astakos_messages.NOT_TERMINATED_PROJECT) % project.id |
924 |
raise ProjectConflict(m)
|
925 |
|
926 |
check_conflicting_projects(project.application) |
927 |
project.resume(actor=request_user, reason=reason) |
928 |
qh_sync_project(project) |
929 |
logger.info("%s has been reinstated" % (project))
|
930 |
project_reinstatement_notify(project) |
931 |
|
932 |
|
933 |
def _partition_by(f, l): |
934 |
d = {} |
935 |
for x in l: |
936 |
group = f(x) |
937 |
group_l = d.get(group, []) |
938 |
group_l.append(x) |
939 |
d[group] = group_l |
940 |
return d
|
941 |
|
942 |
|
943 |
def count_pending_app(users): |
944 |
users = list(users)
|
945 |
apps = ProjectApplication.objects.filter(state=ProjectApplication.PENDING, |
946 |
owner__in=users) |
947 |
apps_d = _partition_by(lambda a: a.owner.uuid, apps)
|
948 |
|
949 |
usage = {} |
950 |
for user in users: |
951 |
uuid = user.uuid |
952 |
usage[uuid] = len(apps_d.get(uuid, []))
|
953 |
return usage
|
954 |
|
955 |
|
956 |
def get_pending_app_diff(user, project): |
957 |
if project is None: |
958 |
diff = 1
|
959 |
else:
|
960 |
objs = ProjectApplication.objects |
961 |
q = objs.filter(chain=project, state=ProjectApplication.PENDING) |
962 |
count = q.count() |
963 |
diff = 1 - count
|
964 |
return diff
|
965 |
|
966 |
|
967 |
def qh_add_pending_app(user, project=None, force=False): |
968 |
user = AstakosUser.objects.select_for_update().get(id=user.id) |
969 |
diff = get_pending_app_diff(user, project) |
970 |
return register_pending_apps(user, diff, force)
|
971 |
|
972 |
|
973 |
def check_pending_app_quota(user, project=None): |
974 |
diff = get_pending_app_diff(user, project) |
975 |
quota = get_pending_app_quota(user) |
976 |
limit = quota['limit']
|
977 |
usage = quota['usage']
|
978 |
if usage + diff > limit:
|
979 |
return False, limit |
980 |
return True, None |
981 |
|
982 |
|
983 |
def qh_release_pending_app(user, locked=False): |
984 |
if not locked: |
985 |
user = AstakosUser.objects.select_for_update().get(id=user.id) |
986 |
register_pending_apps(user, -1)
|