root / snf-astakos-app / astakos / im / functions.py @ 2556cf45
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.get_for_update(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.get_for_update(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 |
project = get_project_for_update(project_id) |
515 |
try:
|
516 |
project = get_project_for_update(project_id) |
517 |
except ProjectNotFound as e: |
518 |
raise ProjectConflict(e.message)
|
519 |
accept_membership_project_checks(project, request_user) |
520 |
|
521 |
try:
|
522 |
membership = get_membership(project_id, user.id) |
523 |
if not membership.check_action("enroll"): |
524 |
m = _(astakos_messages.MEMBERSHIP_ACCEPTED) |
525 |
raise ProjectConflict(m)
|
526 |
membership.perform_action("enroll", actor=request_user, reason=reason)
|
527 |
except ProjectNotFound:
|
528 |
membership = new_membership(project, user, actor=request_user, |
529 |
enroll=True)
|
530 |
|
531 |
qh_sync_user(user) |
532 |
logger.info("User %s has been enrolled in %s." %
|
533 |
(membership.person.log_display, project)) |
534 |
|
535 |
membership_enroll_notify(project, membership.person) |
536 |
return membership
|
537 |
|
538 |
|
539 |
def leave_project_checks(membership, request_user): |
540 |
if not membership.check_action("leave"): |
541 |
m = _(astakos_messages.NOT_ACCEPTED_MEMBERSHIP) |
542 |
raise ProjectConflict(m)
|
543 |
|
544 |
membership_check_allowed(membership, request_user, level=ADMIN_LEVEL) |
545 |
project = membership.project |
546 |
checkAlive(project) |
547 |
|
548 |
leave_policy = project.application.member_leave_policy |
549 |
if leave_policy == CLOSED_POLICY:
|
550 |
m = _(astakos_messages.MEMBER_LEAVE_POLICY_CLOSED) |
551 |
raise ProjectConflict(m)
|
552 |
|
553 |
|
554 |
def can_leave_request(project, user): |
555 |
m = user.get_membership(project) |
556 |
if m is None: |
557 |
return False |
558 |
try:
|
559 |
leave_project_checks(m, user) |
560 |
except ProjectError:
|
561 |
return False |
562 |
return True |
563 |
|
564 |
|
565 |
def leave_project(memb_id, request_user, reason=None): |
566 |
project = get_project_of_membership_for_update(memb_id) |
567 |
membership = get_membership_by_id(memb_id) |
568 |
leave_project_checks(membership, request_user) |
569 |
|
570 |
auto_accepted = False
|
571 |
leave_policy = project.application.member_leave_policy |
572 |
if leave_policy == AUTO_ACCEPT_POLICY:
|
573 |
membership.perform_action("remove", actor=request_user, reason=reason)
|
574 |
qh_sync_user(request_user) |
575 |
logger.info("User %s has left %s." %
|
576 |
(request_user.log_display, project)) |
577 |
auto_accepted = True
|
578 |
else:
|
579 |
membership.perform_action("leave_request", actor=request_user,
|
580 |
reason=reason) |
581 |
logger.info("User %s requested to leave %s." %
|
582 |
(request_user.log_display, project)) |
583 |
membership_leave_request_notify(project, membership.person) |
584 |
return auto_accepted
|
585 |
|
586 |
|
587 |
def join_project_checks(project): |
588 |
checkAlive(project) |
589 |
|
590 |
join_policy = project.application.member_join_policy |
591 |
if join_policy == CLOSED_POLICY:
|
592 |
m = _(astakos_messages.MEMBER_JOIN_POLICY_CLOSED) |
593 |
raise ProjectConflict(m)
|
594 |
|
595 |
|
596 |
Nothing = type('Nothing', (), {}) |
597 |
|
598 |
|
599 |
def can_join_request(project, user, membership=Nothing): |
600 |
try:
|
601 |
join_project_checks(project) |
602 |
except ProjectError:
|
603 |
return False |
604 |
|
605 |
m = (membership if membership is not Nothing |
606 |
else user.get_membership(project))
|
607 |
if not m: |
608 |
return True |
609 |
return m.check_action("join") |
610 |
|
611 |
|
612 |
def new_membership(project, user, actor=None, reason=None, enroll=False): |
613 |
state = (ProjectMembership.ACCEPTED if enroll
|
614 |
else ProjectMembership.REQUESTED)
|
615 |
m = ProjectMembership.objects.create( |
616 |
project=project, person=user, state=state) |
617 |
m._log_create(None, state, actor=actor, reason=reason)
|
618 |
return m
|
619 |
|
620 |
|
621 |
def join_project(project_id, request_user, reason=None): |
622 |
project = get_project_for_update(project_id) |
623 |
join_project_checks(project) |
624 |
|
625 |
try:
|
626 |
membership = get_membership(project.id, request_user.id) |
627 |
if not membership.check_action("join"): |
628 |
msg = _(astakos_messages.MEMBERSHIP_ASSOCIATED) |
629 |
raise ProjectConflict(msg)
|
630 |
membership.perform_action("join", actor=request_user, reason=reason)
|
631 |
except ProjectNotFound:
|
632 |
membership = new_membership(project, request_user, actor=request_user, |
633 |
reason=reason) |
634 |
|
635 |
join_policy = project.application.member_join_policy |
636 |
if (join_policy == AUTO_ACCEPT_POLICY and ( |
637 |
not project.violates_members_limit(adding=1))): |
638 |
membership.perform_action("accept", actor=request_user, reason=reason)
|
639 |
qh_sync_user(request_user) |
640 |
logger.info("User %s joined %s." %
|
641 |
(request_user.log_display, project)) |
642 |
else:
|
643 |
membership_request_notify(project, membership.person) |
644 |
logger.info("User %s requested to join %s." %
|
645 |
(request_user.log_display, project)) |
646 |
return membership
|
647 |
|
648 |
|
649 |
MEMBERSHIP_ACTION_CHECKS = { |
650 |
"leave": leave_project_checks,
|
651 |
"cancel": cancel_membership_checks,
|
652 |
"accept": accept_membership_checks,
|
653 |
"reject": reject_membership_checks,
|
654 |
"remove": remove_membership_checks,
|
655 |
} |
656 |
|
657 |
|
658 |
def membership_allowed_actions(membership, request_user): |
659 |
allowed = [] |
660 |
for action, check in MEMBERSHIP_ACTION_CHECKS.iteritems(): |
661 |
try:
|
662 |
check(membership, request_user) |
663 |
allowed.append(action) |
664 |
except ProjectError:
|
665 |
pass
|
666 |
return allowed
|
667 |
|
668 |
|
669 |
def submit_application(owner=None, |
670 |
name=None,
|
671 |
project_id=None,
|
672 |
homepage=None,
|
673 |
description=None,
|
674 |
start_date=None,
|
675 |
end_date=None,
|
676 |
member_join_policy=None,
|
677 |
member_leave_policy=None,
|
678 |
limit_on_members_number=None,
|
679 |
comments=None,
|
680 |
resources=None,
|
681 |
request_user=None):
|
682 |
|
683 |
project = None
|
684 |
if project_id is not None: |
685 |
project = get_project_for_update(project_id) |
686 |
project_check_allowed(project, request_user, level=APPLICANT_LEVEL) |
687 |
|
688 |
policies = validate_resource_policies(resources) |
689 |
|
690 |
force = request_user.is_project_admin() |
691 |
ok, limit = qh_add_pending_app(owner, project, force) |
692 |
if not ok: |
693 |
m = _(astakos_messages.REACHED_PENDING_APPLICATION_LIMIT) % limit |
694 |
raise ProjectConflict(m)
|
695 |
|
696 |
application = ProjectApplication( |
697 |
applicant=request_user, |
698 |
owner=owner, |
699 |
name=name, |
700 |
homepage=homepage, |
701 |
description=description, |
702 |
start_date=start_date, |
703 |
end_date=end_date, |
704 |
member_join_policy=member_join_policy, |
705 |
member_leave_policy=member_leave_policy, |
706 |
limit_on_members_number=limit_on_members_number, |
707 |
comments=comments) |
708 |
|
709 |
if project is None: |
710 |
chain = new_chain() |
711 |
application.chain_id = chain.chain |
712 |
application.save() |
713 |
Project.objects.create(id=chain.chain, application=application) |
714 |
else:
|
715 |
application.chain = project |
716 |
application.save() |
717 |
if project.application.state != ProjectApplication.APPROVED:
|
718 |
project.application = application |
719 |
project.save() |
720 |
|
721 |
pending = ProjectApplication.objects.filter( |
722 |
chain=project, |
723 |
state=ProjectApplication.PENDING).exclude(id=application.id) |
724 |
for app in pending: |
725 |
app.state = ProjectApplication.REPLACED |
726 |
app.save() |
727 |
|
728 |
if policies is not None: |
729 |
set_resource_policies(application, policies) |
730 |
logger.info("User %s submitted %s." %
|
731 |
(request_user.log_display, application.log_display)) |
732 |
application_submit_notify(application) |
733 |
return application
|
734 |
|
735 |
|
736 |
def validate_resource_policies(policies): |
737 |
if not isinstance(policies, dict): |
738 |
raise ProjectBadRequest("Malformed resource policies") |
739 |
|
740 |
resource_names = policies.keys() |
741 |
resources = Resource.objects.filter(name__in=resource_names) |
742 |
resource_d = {} |
743 |
for resource in resources: |
744 |
resource_d[resource.name] = resource |
745 |
|
746 |
found = resource_d.keys() |
747 |
nonex = [name for name in resource_names if name not in found] |
748 |
if nonex:
|
749 |
raise ProjectBadRequest("Malformed resource policies") |
750 |
|
751 |
pols = [] |
752 |
for resource_name, specs in policies.iteritems(): |
753 |
p_capacity = specs.get("project_capacity")
|
754 |
m_capacity = specs.get("member_capacity")
|
755 |
|
756 |
if p_capacity is not None and not isinstance(p_capacity, (int, long)): |
757 |
raise ProjectBadRequest("Malformed resource policies") |
758 |
if not isinstance(m_capacity, (int, long)): |
759 |
raise ProjectBadRequest("Malformed resource policies") |
760 |
pols.append((resource_d[resource_name], m_capacity, p_capacity)) |
761 |
return pols
|
762 |
|
763 |
|
764 |
def set_resource_policies(application, policies): |
765 |
for resource, m_capacity, p_capacity in policies: |
766 |
g = application.projectresourcegrant_set |
767 |
g.create(resource=resource, |
768 |
member_capacity=m_capacity, |
769 |
project_capacity=p_capacity) |
770 |
|
771 |
|
772 |
def cancel_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_cancel(): |
778 |
m = _(astakos_messages.APPLICATION_CANNOT_CANCEL % |
779 |
(application.id, application.state_display())) |
780 |
raise ProjectConflict(m)
|
781 |
|
782 |
qh_release_pending_app(application.owner) |
783 |
|
784 |
application.cancel(actor=request_user, reason=reason) |
785 |
logger.info("%s has been cancelled." % (application.log_display))
|
786 |
|
787 |
|
788 |
def dismiss_application(application_id, request_user=None, reason=""): |
789 |
get_project_of_application_for_update(application_id) |
790 |
application = get_application(application_id) |
791 |
app_check_allowed(application, request_user, level=APPLICANT_LEVEL) |
792 |
|
793 |
if not application.can_dismiss(): |
794 |
m = _(astakos_messages.APPLICATION_CANNOT_DISMISS % |
795 |
(application.id, application.state_display())) |
796 |
raise ProjectConflict(m)
|
797 |
|
798 |
application.dismiss(actor=request_user, reason=reason) |
799 |
logger.info("%s has been dismissed." % (application.log_display))
|
800 |
|
801 |
|
802 |
def deny_application(application_id, request_user=None, reason=""): |
803 |
get_project_of_application_for_update(application_id) |
804 |
application = get_application(application_id) |
805 |
|
806 |
app_check_allowed(application, request_user, level=ADMIN_LEVEL) |
807 |
|
808 |
if not application.can_deny(): |
809 |
m = _(astakos_messages.APPLICATION_CANNOT_DENY % |
810 |
(application.id, application.state_display())) |
811 |
raise ProjectConflict(m)
|
812 |
|
813 |
qh_release_pending_app(application.owner) |
814 |
|
815 |
application.deny(actor=request_user, reason=reason) |
816 |
logger.info("%s has been denied with reason \"%s\"." %
|
817 |
(application.log_display, reason)) |
818 |
application_deny_notify(application) |
819 |
|
820 |
|
821 |
def check_conflicting_projects(application): |
822 |
project = application.chain |
823 |
new_project_name = application.name |
824 |
try:
|
825 |
q = Q(name=new_project_name) & ~Q(state=Project.TERMINATED) |
826 |
conflicting_project = Project.objects.get(q) |
827 |
if (conflicting_project != project):
|
828 |
m = (_("cannot approve: project with name '%s' "
|
829 |
"already exists (id: %s)") %
|
830 |
(new_project_name, conflicting_project.id)) |
831 |
raise ProjectConflict(m) # invalid argument |
832 |
except Project.DoesNotExist:
|
833 |
pass
|
834 |
|
835 |
|
836 |
def approve_application(app_id, request_user=None, reason=""): |
837 |
get_project_lock() |
838 |
project = get_project_of_application_for_update(app_id) |
839 |
application = get_application(app_id) |
840 |
|
841 |
app_check_allowed(application, request_user, level=ADMIN_LEVEL) |
842 |
|
843 |
if not application.can_approve(): |
844 |
m = _(astakos_messages.APPLICATION_CANNOT_APPROVE % |
845 |
(application.id, application.state_display())) |
846 |
raise ProjectConflict(m)
|
847 |
|
848 |
check_conflicting_projects(application) |
849 |
|
850 |
# Pre-lock members and owner together in order to impose an ordering
|
851 |
# on locking users
|
852 |
members = members_to_sync(project) |
853 |
uids_to_sync = [member.id for member in members] |
854 |
owner = application.owner |
855 |
uids_to_sync.append(owner.id) |
856 |
get_users_for_update(uids_to_sync) |
857 |
|
858 |
qh_release_pending_app(owner, locked=True)
|
859 |
application.approve(actor=request_user, reason=reason) |
860 |
project.application = application |
861 |
project.name = application.name |
862 |
project.save() |
863 |
if project.is_deactivated():
|
864 |
project.resume(actor=request_user, reason="APPROVE")
|
865 |
qh_sync_locked_users(members) |
866 |
logger.info("%s has been approved." % (application.log_display))
|
867 |
application_approve_notify(application) |
868 |
|
869 |
|
870 |
def check_expiration(execute=False): |
871 |
objects = Project.objects |
872 |
expired = objects.expired_projects() |
873 |
if execute:
|
874 |
for project in expired: |
875 |
terminate(project.pk) |
876 |
|
877 |
return [project.expiration_info() for project in expired] |
878 |
|
879 |
|
880 |
def terminate(project_id, request_user=None, reason=None): |
881 |
project = get_project_for_update(project_id) |
882 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
883 |
checkAlive(project) |
884 |
|
885 |
project.terminate(actor=request_user, reason=reason) |
886 |
qh_sync_project(project) |
887 |
logger.info("%s has been terminated." % (project))
|
888 |
|
889 |
project_termination_notify(project) |
890 |
|
891 |
|
892 |
def suspend(project_id, request_user=None, reason=None): |
893 |
project = get_project_for_update(project_id) |
894 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
895 |
checkAlive(project) |
896 |
|
897 |
project.suspend(actor=request_user, reason=reason) |
898 |
qh_sync_project(project) |
899 |
logger.info("%s has been suspended." % (project))
|
900 |
|
901 |
project_suspension_notify(project) |
902 |
|
903 |
|
904 |
def unsuspend(project_id, request_user=None, reason=None): |
905 |
project = get_project_for_update(project_id) |
906 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
907 |
|
908 |
if not project.is_suspended: |
909 |
m = _(astakos_messages.NOT_SUSPENDED_PROJECT) % project.id |
910 |
raise ProjectConflict(m)
|
911 |
|
912 |
project.resume(actor=request_user, reason=reason) |
913 |
qh_sync_project(project) |
914 |
logger.info("%s has been unsuspended." % (project))
|
915 |
project_unsuspension_notify(project) |
916 |
|
917 |
|
918 |
def reinstate(project_id, request_user=None, reason=None): |
919 |
get_project_lock() |
920 |
project = get_project_for_update(project_id) |
921 |
project_check_allowed(project, request_user, level=ADMIN_LEVEL) |
922 |
|
923 |
if not project.is_terminated: |
924 |
m = _(astakos_messages.NOT_TERMINATED_PROJECT) % project.id |
925 |
raise ProjectConflict(m)
|
926 |
|
927 |
check_conflicting_projects(project.application) |
928 |
project.resume(actor=request_user, reason=reason) |
929 |
qh_sync_project(project) |
930 |
logger.info("%s has been reinstated" % (project))
|
931 |
project_reinstatement_notify(project) |
932 |
|
933 |
|
934 |
def _partition_by(f, l): |
935 |
d = {} |
936 |
for x in l: |
937 |
group = f(x) |
938 |
group_l = d.get(group, []) |
939 |
group_l.append(x) |
940 |
d[group] = group_l |
941 |
return d
|
942 |
|
943 |
|
944 |
def count_pending_app(users): |
945 |
users = list(users)
|
946 |
apps = ProjectApplication.objects.filter(state=ProjectApplication.PENDING, |
947 |
owner__in=users) |
948 |
apps_d = _partition_by(lambda a: a.owner.uuid, apps)
|
949 |
|
950 |
usage = {} |
951 |
for user in users: |
952 |
uuid = user.uuid |
953 |
usage[uuid] = len(apps_d.get(uuid, []))
|
954 |
return usage
|
955 |
|
956 |
|
957 |
def get_pending_app_diff(user, project): |
958 |
if project is None: |
959 |
diff = 1
|
960 |
else:
|
961 |
objs = ProjectApplication.objects |
962 |
q = objs.filter(chain=project, state=ProjectApplication.PENDING) |
963 |
count = q.count() |
964 |
diff = 1 - count
|
965 |
return diff
|
966 |
|
967 |
|
968 |
def qh_add_pending_app(user, project=None, force=False): |
969 |
user = AstakosUser.forupdate.get_for_update(id=user.id) |
970 |
diff = get_pending_app_diff(user, project) |
971 |
return register_pending_apps(user, diff, force)
|
972 |
|
973 |
|
974 |
def check_pending_app_quota(user, project=None): |
975 |
diff = get_pending_app_diff(user, project) |
976 |
quota = get_pending_app_quota(user) |
977 |
limit = quota['limit']
|
978 |
usage = quota['usage']
|
979 |
if usage + diff > limit:
|
980 |
return False, limit |
981 |
return True, None |
982 |
|
983 |
|
984 |
def qh_release_pending_app(user, locked=False): |
985 |
if not locked: |
986 |
user = AstakosUser.forupdate.get_for_update(id=user.id) |
987 |
register_pending_apps(user, -1)
|