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