Revision 73fbaec4 snf-astakos-app/astakos/im/functions.py
b/snf-astakos-app/astakos/im/functions.py | ||
---|---|---|
41 | 41 |
from django.template import Context, loader |
42 | 42 |
from django.contrib.auth import ( |
43 | 43 |
login as auth_login, |
44 |
logout as auth_logout |
|
45 |
) |
|
44 |
logout as auth_logout) |
|
46 | 45 |
from django.conf import settings |
47 | 46 |
from django.contrib.auth.models import AnonymousUser |
47 |
from django.core.exceptions import PermissionDenied |
|
48 | 48 |
|
49 | 49 |
from urllib import quote |
50 | 50 |
from urlparse import urljoin |
... | ... | |
57 | 57 |
VERIFICATION_EMAIL_SUBJECT, ACCOUNT_CREATION_SUBJECT, |
58 | 58 |
GROUP_CREATION_SUBJECT, HELPDESK_NOTIFICATION_EMAIL_SUBJECT, |
59 | 59 |
INVITATION_EMAIL_SUBJECT, GREETING_EMAIL_SUBJECT, FEEDBACK_EMAIL_SUBJECT, |
60 |
EMAIL_CHANGE_EMAIL_SUBJECT |
|
61 |
) |
|
60 |
EMAIL_CHANGE_EMAIL_SUBJECT, |
|
61 |
PROJECT_CREATION_SUBJECT, PROJECT_APPROVED_SUBJECT, |
|
62 |
PROJECT_TERMINATION_SUBJECT, PROJECT_SUSPENSION_SUBJECT, |
|
63 |
PROJECT_MEMBERSHIP_CHANGE_SUBJECT) |
|
64 |
from astakos.im.notifications import build_notification, NotificationError |
|
65 |
from astakos.im.models import ( |
|
66 |
ProjectMembership, ProjectApplication) |
|
67 |
|
|
62 | 68 |
import astakos.im.messages as astakos_messages |
63 | 69 |
|
64 | 70 |
logger = logging.getLogger(__name__) |
... | ... | |
257 | 263 |
logger.log(LOGGING_LEVEL, msg) |
258 | 264 |
|
259 | 265 |
|
260 |
def send_change_email(ec, request, email_template_name='registration/email_change_email.txt'): |
|
266 |
def send_change_email( |
|
267 |
ec, request, email_template_name='registration/email_change_email.txt'): |
|
261 | 268 |
try: |
262 | 269 |
url = reverse('email_change_confirm', |
263 | 270 |
kwargs={'activation_key': ec.activation_key}) |
... | ... | |
279 | 286 |
user, |
280 | 287 |
email_template_name='im/welcome_email.txt', |
281 | 288 |
helpdesk_email_template_name='im/helpdesk_notification.txt', |
282 |
verify_email=False |
|
283 |
): |
|
289 |
verify_email=False): |
|
284 | 290 |
""" |
285 | 291 |
Activates the specific user and sends email. |
286 | 292 |
|
... | ... | |
293 | 299 |
send_helpdesk_notification(user, helpdesk_email_template_name) |
294 | 300 |
send_greeting(user, email_template_name) |
295 | 301 |
|
302 |
def invite(inviter, email, realname): |
|
303 |
inv = Invitation(inviter=inviter, username=email, realname=realname) |
|
304 |
inv.save() |
|
305 |
send_invitation(inv) |
|
306 |
inviter.invitations = max(0, self.invitations - 1) |
|
307 |
inviter.save() |
|
296 | 308 |
|
297 | 309 |
def switch_account_to_shibboleth(user, local_user, |
298 | 310 |
greeting_template_name='im/welcome_email.txt'): |
... | ... | |
355 | 367 |
def __init__(self): |
356 | 368 |
self.message = _(astakos_messages.NOTIFICATION_SEND_ERR) |
357 | 369 |
super(SendNotificationError, self).__init__() |
370 |
|
|
371 |
|
|
372 |
### PROJECT VIEWS ### |
|
373 |
def get_join_policy(str_policy): |
|
374 |
try: |
|
375 |
return MemberJoinPolicy.objects.get(policy=str_policy) |
|
376 |
except: |
|
377 |
return None |
|
378 |
|
|
379 |
def get_leave_policy(str_policy): |
|
380 |
try: |
|
381 |
return MemberLeavePolicy.objects.get(policy=str_policy) |
|
382 |
except: |
|
383 |
return None |
|
384 |
|
|
385 |
_auto_accept_join = False |
|
386 |
def get_auto_accept_join_policy(): |
|
387 |
global _auto_accept_join |
|
388 |
if _auto_accept_join is not False: |
|
389 |
return _auto_accept_join |
|
390 |
_auto_accept = get_join_policy('auto_accept') |
|
391 |
return _auto_accept |
|
392 |
|
|
393 |
_closed_join = False |
|
394 |
def get_closed_join_policy(): |
|
395 |
global _closed_join |
|
396 |
if _closed_join is not False: |
|
397 |
return _closed_join |
|
398 |
_closed_join = get_join_policy('closed') |
|
399 |
return _closed_join |
|
400 |
|
|
401 |
_auto_accept_leave = False |
|
402 |
def get_auto_accept_leave_policy(): |
|
403 |
global _auto_accept_leave |
|
404 |
if _auto_accept_leave is not False: |
|
405 |
return _auto_accept_leave |
|
406 |
_auto_accept_leave = get_leave_policy('auto_accept') |
|
407 |
return _auto_accept_leave |
|
408 |
|
|
409 |
_closed_leave = False |
|
410 |
def get_closed_leave_policy(): |
|
411 |
global _closed_leave |
|
412 |
if _closed_leave is not False: |
|
413 |
return _closed_leave |
|
414 |
_closed_leave = get_leave_policy('closed') |
|
415 |
return _closed_leave |
|
416 |
|
|
417 |
def get_project_by_application_id(project_application_id): |
|
418 |
try: |
|
419 |
return Project.objects.get(application__id=project_application_id) |
|
420 |
except Project.DoesNotExist: |
|
421 |
raise IOError( |
|
422 |
_(astakos_messages.UNKNOWN_PROJECT_APPLICATION_ID) % project_application_id) |
|
423 |
|
|
424 |
def get_user_by_id(user_id): |
|
425 |
try: |
|
426 |
return AstakosUser.objects.get(user__id=user_id) |
|
427 |
except AstakosUser.DoesNotExist: |
|
428 |
raise IOError(_(astakos_messages.UNKNOWN_USER_ID) % user_id) |
|
429 |
|
|
430 |
def create_membership(project_application_id, user_id): |
|
431 |
try: |
|
432 |
project = get_project_by_application_id(project_application_id) |
|
433 |
m = ProjectMembership( |
|
434 |
project=project, |
|
435 |
person__id=user_id, |
|
436 |
request_date=datetime.now()) |
|
437 |
except IntegrityError, e: |
|
438 |
raise IOError(_(astakos_messages.MEMBERSHIP_REQUEST_EXISTS)) |
|
439 |
else: |
|
440 |
m.save() |
|
441 |
|
|
442 |
def get_membership(project, user): |
|
443 |
if isinstace(project, int): |
|
444 |
project = get_project_by_application_id(project) |
|
445 |
if isinstace(user, int): |
|
446 |
user = get_user_by_id(user) |
|
447 |
try: |
|
448 |
return ProjectMembership.objects.select_related().get( |
|
449 |
project=project, |
|
450 |
person=user) |
|
451 |
except ProjectMembership.DoesNotExist: |
|
452 |
raise IOError(_(astakos_messages.NOT_MEMBERSHIP_REQUEST)) |
|
453 |
|
|
454 |
def accept_membership(request, project, user, request_user=None): |
|
455 |
""" |
|
456 |
Raises: |
|
457 |
django.core.exceptions.PermissionDenied |
|
458 |
IOError |
|
459 |
""" |
|
460 |
membership = get_membership(project, user) |
|
461 |
if request_user and \ |
|
462 |
(not membership.project.current_application.owner == request_user and \ |
|
463 |
not request_user.is_superuser): |
|
464 |
raise PermissionDenied(_(astakos_messages.NOT_ALLOWED)) |
|
465 |
if not self.project.is_alive: |
|
466 |
raise PermissionDenied( |
|
467 |
_(astakos_messages.NOT_ALIVE_PROJECT) % membership.project.__dict__) |
|
468 |
if len(self.project.approved_members) + 1 > \ |
|
469 |
self.project.definition.limit_on_members_number: |
|
470 |
raise PermissionDenied(_(astakos_messages.MEMBER_NUMBER_LIMIT_REACHED)) |
|
471 |
|
|
472 |
membership.accept() |
|
473 |
|
|
474 |
try: |
|
475 |
notification = build_notification( |
|
476 |
settings.SERVER_EMAIL, |
|
477 |
[self.person.email], |
|
478 |
_(PROJECT_MEMBERSHIP_CHANGE_SUBJECT) % membership.project.definition.__dict__, |
|
479 |
template='im/projects/project_membership_change_notification.txt', |
|
480 |
dictionary={'object':membership.project.current_application, 'action':'accepted'}) |
|
481 |
notification.send() |
|
482 |
except NotificationError, e: |
|
483 |
logger.error(e.messages) |
|
484 |
return membership |
|
485 |
|
|
486 |
def reject_membership(project, user, request_user=None): |
|
487 |
""" |
|
488 |
Raises: |
|
489 |
django.core.exceptions.PermissionDenied |
|
490 |
IOError |
|
491 |
""" |
|
492 |
membership = get_membership(project, user) |
|
493 |
if request_user and \ |
|
494 |
(not membership.project.current_application.owner == request_user and \ |
|
495 |
not request_user.is_superuser): |
|
496 |
raise PermissionDenied(_(astakos_messages.NOT_ALLOWED)) |
|
497 |
if not membership.project.is_alive: |
|
498 |
raise PermissionDenied(_(astakos_messages.NOT_ALIVE_PROJECT) % project.__dict__) |
|
499 |
|
|
500 |
membership.reject() |
|
501 |
|
|
502 |
try: |
|
503 |
notification = build_notification( |
|
504 |
settings.SERVER_EMAIL, |
|
505 |
[self.person.email], |
|
506 |
_(PROJECT_MEMBERSHIP_CHANGE_SUBJECT) % self.project.definition.__dict__, |
|
507 |
template='im/projects/project_membership_change_notification.txt', |
|
508 |
dictionary={'object':self.project.current_application, 'action':'rejected'}) |
|
509 |
notification.send() |
|
510 |
except NotificationError, e: |
|
511 |
logger.error(e.messages) |
|
512 |
return membership |
|
513 |
|
|
514 |
def remove_membership(project, user, request_user=None): |
|
515 |
""" |
|
516 |
Raises: |
|
517 |
django.core.exceptions.PermissionDenied |
|
518 |
IOError |
|
519 |
""" |
|
520 |
membership = get_membership(project, user) |
|
521 |
if request_user and \ |
|
522 |
(not membership.project.current_application.owner == request_user and \ |
|
523 |
not request_user.is_superuser): |
|
524 |
raise PermissionDenied(_(astakos_messages.NOT_ALLOWED)) |
|
525 |
if not self.project.is_alive: |
|
526 |
raise PermissionDenied(_(astakos_messages.NOT_ALIVE_PROJECT) % membership.project.__dict__) |
|
527 |
|
|
528 |
membership.remove() |
|
529 |
|
|
530 |
try: |
|
531 |
notification = build_notification( |
|
532 |
settings.SERVER_EMAIL, |
|
533 |
[self.person.email], |
|
534 |
_(PROJECT_MEMBERSHIP_CHANGE_SUBJECT) % membership.project.definition.__dict__, |
|
535 |
template='im/projects/project_membership_change_notification.txt', |
|
536 |
dictionary={'object':membership.project.current_application, 'action':'removed'}) |
|
537 |
notification.send() |
|
538 |
except NotificationError, e: |
|
539 |
logger.error(e.messages) |
|
540 |
return membership |
|
541 |
|
|
542 |
def leave_project(project_application_id, user_id): |
|
543 |
""" |
|
544 |
Raises: |
|
545 |
django.core.exceptions.PermissionDenied |
|
546 |
IOError |
|
547 |
""" |
|
548 |
project = get_project_by_application_id(project_application_id) |
|
549 |
leave_policy = project.current_application.definition.member_join_policy |
|
550 |
if leave_policy == get_closed_leave(): |
|
551 |
raise PermissionDenied(_(astakos_messages.MEMBER_LEAVE_POLICY_CLOSED)) |
|
552 |
|
|
553 |
membership = get_membership(project_application_id, user_id) |
|
554 |
if leave_policy == get_auto_accept_leave(): |
|
555 |
membership.remove() |
|
556 |
else: |
|
557 |
membership.leave_request_date = datetime.now() |
|
558 |
membership.save() |
|
559 |
return membership |
|
560 |
|
|
561 |
def join_project(project_application_id, user_id): |
|
562 |
""" |
|
563 |
Raises: |
|
564 |
django.core.exceptions.PermissionDenied |
|
565 |
IOError |
|
566 |
""" |
|
567 |
project = get_project_by_application_id(project_application_id) |
|
568 |
join_policy = project.current_application.definition.member_join_policy |
|
569 |
if join_policy == get_closed_join(): |
|
570 |
raise PermissionDenied(_(astakos_messages.MEMBER_JOIN_POLICY_CLOSED)) |
|
571 |
|
|
572 |
membership = create_membership(project_application_id, user_id) |
|
573 |
|
|
574 |
if join_policy == get_auto_accept_join(): |
|
575 |
membership.accept() |
|
576 |
return membership |
|
577 |
|
|
578 |
def submit_application( |
|
579 |
application, resource_policies, applicant, comments, precursor_application=None): |
|
580 |
|
|
581 |
application.submit( |
|
582 |
resource_policies, applicant, comments, precursor_application) |
|
583 |
|
|
584 |
try: |
|
585 |
notification = build_notification( |
|
586 |
settings.SERVER_EMAIL, |
|
587 |
[i[1] for i in settings.ADMINS], |
|
588 |
_(PROJECT_CREATION_SUBJECT) % application.__dict__, |
|
589 |
template='im/projects/project_creation_notification.txt', |
|
590 |
dictionary={'object':application}) |
|
591 |
notification.send() |
|
592 |
except NotificationError, e: |
|
593 |
logger.error(e.messages) |
|
594 |
return application |
|
595 |
|
|
596 |
def approve_application(application): |
|
597 |
application.approve() |
|
598 |
# rejected = application.project.sync() |
|
599 |
|
|
600 |
try: |
|
601 |
notification = build_notification( |
|
602 |
settings.SERVER_EMAIL, |
|
603 |
[self.owner.email], |
|
604 |
_(PROJECT_APPROVED_SUBJECT) % application.definition.__dict__, |
|
605 |
template='im/projects/project_approval_notification.txt', |
|
606 |
dictionary={'object':application}) |
|
607 |
notification.send() |
|
608 |
except NotificationError, e: |
|
609 |
logger.error(e.messages) |
Also available in: Unified diff