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