Exemplo n.º 1
0
def project_join(request, project_uuid):
    project = get_object_or_404(Project, uuid=project_uuid)
    with ExceptionHandler(request):
        with transaction.commit_on_success():
            membership = join_project(project_uuid, request.user)
            if membership.state != membership.REQUESTED:
                m = _(astakos_messages.USER_JOINED_PROJECT)
            else:
                m = _(astakos_messages.USER_JOIN_REQUEST_SUBMITTED)
            messages.success(request, m)
    return redirect_to_next(request, 'project_detail', args=(project.uuid, ))
Exemplo n.º 2
0
def project_leave(request, project_uuid):
    project = get_object_or_404(Project, uuid=project_uuid)
    with ExceptionHandler(request):
        with transaction.atomic():
            memb_id = request.user.get_membership(project).pk
            auto_accepted = leave_project(memb_id, request.user)
            if auto_accepted:
                m = _(astakos_messages.USER_LEFT_PROJECT)
            else:
                m = _(astakos_messages.USER_LEAVE_REQUEST_SUBMITTED)
            messages.success(request, m)
    return redirect_to_next(request, 'project_detail', args=(project.uuid, ))
Exemplo n.º 3
0
def project_members_action(request, project_uuid, action=None, redirect_to='',
                           memb_id=None):

    actions_map = {
        'remove': {
            'method': 'remove_membership',
            'msg': _(astakos_messages.USER_MEMBERSHIP_REMOVED)
        },
        'accept': {
            'method': 'accept_membership',
            'msg': _(astakos_messages.USER_MEMBERSHIP_ACCEPTED)
        },
        'reject': {
            'method': 'reject_membership',
            'msg': _(astakos_messages.USER_MEMBERSHIP_REJECTED)
        }
    }

    if not action in actions_map.keys():
        raise PermissionDenied

    if memb_id:
        member_ids = [memb_id]
    else:
        member_ids = request.POST.getlist('members')

    project = get_object_or_404(Project, uuid=project_uuid)

    user = request.user
    if not user.owns_project(project) and not user.is_project_admin():
        messages.error(request, astakos_messages.NOT_ALLOWED)
        return redirect(reverse('index'))

    logger.info("Member(s) action from %s (project: %r, action: %s, "
                "members: %r)", user.log_display, project.uuid, action,
                member_ids)

    action = actions_map.get(action)
    action_func = getattr(project_actions, action.get('method'))
    for member_id in member_ids:
        member_id = int(member_id)
        with ExceptionHandler(request):
            with transaction.commit_on_success():
                try:
                    m = action_func(member_id, request.user)
                except ProjectError as e:
                    messages.error(request, e)
                else:
                    email = escape(m.person.email)
                    msg = action.get('msg') % email
                    messages.success(request, msg)

    return redirect_back(request, 'project_list')
Exemplo n.º 4
0
def project_join(request, chain_id):
    next = request.GET.get('next')
    if not next:
        next = reverse('astakos.im.views.project_detail',
                       args=(chain_id,))

    with ExceptionHandler(request):
        _project_join(request, chain_id)


    next = restrict_next(next, domain=settings.COOKIE_DOMAIN)
    return redirect(next)
Exemplo n.º 5
0
def project_app_cancel(request, application_id):
    next = request.GET.get('next')
    chain_id = None

    with ExceptionHandler(request):
        chain_id = _project_app_cancel(request, application_id)

    if not next:
        if chain_id:
            next = reverse('astakos.im.views.project_detail', args=(chain_id,))
        else:
            next = reverse('astakos.im.views.project_list')

    next = restrict_next(next, domain=settings.COOKIE_DOMAIN)
    return redirect(next)
Exemplo n.º 6
0
def project_add(request):
    user = request.user
    if not user.is_project_admin():
        ok, limit = check_pending_app_quota(user)
        if not ok:
            m = _(astakos_messages.PENDING_APPLICATION_LIMIT_ADD) % limit
            messages.error(request, m)
            next = reverse('astakos.im.views.project_list')
            next = restrict_next(next, domain=settings.COOKIE_DOMAIN)
            return redirect(next)

    details_fields = ["name", "homepage", "description", "start_date",
                      "end_date", "comments"]
    membership_fields = ["member_join_policy", "member_leave_policy",
                         "limit_on_members_number"]
    resource_catalog, resource_groups = _resources_catalog(for_project=True)
    if resource_catalog is False:
        # on fail resource_groups contains the result object
        result = resource_groups
        messages.error(request, 'Unable to retrieve system resources: %s' %
                       result.reason)
    extra_context = {
        'resource_catalog': resource_catalog,
        'resource_groups': resource_groups,
        'show_form': True,
        'details_fields': details_fields,
        'membership_fields': membership_fields}

    response = None
    with ExceptionHandler(request):
        response = _create_object(
            request,
            template_name='im/projects/projectapplication_form.html',
            extra_context=extra_context,
            post_save_redirect=reverse('project_list'),
            form_class=ProjectApplicationForm,
            msg=_("The %(verbose_name)s has been received and "
                  "is under consideration."),
            )

    if response is not None:
        return response

    next = reverse('astakos.im.views.project_list')
    next = restrict_next(next, domain=settings.COOKIE_DOMAIN)
    return redirect(next)
Exemplo n.º 7
0
def project_app_approve(request, application_id):

    if not request.user.is_project_admin():
        m = _(astakos_messages.NOT_ALLOWED)
        raise PermissionDenied(m)

    try:
        ProjectApplication.objects.get(id=application_id)
    except ProjectApplication.DoesNotExist:
        raise Http404

    with ExceptionHandler(request):
        _project_app_approve(request, application_id)

    chain_id = get_related_project_id(application_id)
    if not chain_id:
        return redirect_back(request, 'project_list')
    return redirect(reverse('project_detail', args=(chain_id, )))
Exemplo n.º 8
0
def project_app_deny(request, application_id):

    reason = request.POST.get('reason', None)
    if not reason:
        reason = None

    if not request.user.is_project_admin():
        m = _(astakos_messages.NOT_ALLOWED)
        raise PermissionDenied(m)

    try:
        ProjectApplication.objects.get(id=application_id)
    except ProjectApplication.DoesNotExist:
        raise Http404

    with ExceptionHandler(request):
        _project_app_deny(request, application_id, reason)

    return redirect(reverse('project_list'))
Exemplo n.º 9
0
def project_app_dismiss(request, application_id):
    try:
        app = ProjectApplication.objects.get(id=application_id)
    except ProjectApplication.DoesNotExist:
        raise Http404

    if not request.user.owns_application(app):
        m = _(astakos_messages.NOT_ALLOWED)
        raise PermissionDenied(m)

    with ExceptionHandler(request):
        _project_app_dismiss(request, application_id)

    chain_id = None
    chain_id = get_related_project_id(application_id)
    if chain_id:
        next = reverse('project_detail', args=(chain_id, ))
    else:
        next = reverse('project_list')
    return redirect(next)
Exemplo n.º 10
0
def project_members(request,
                    project_uuid,
                    members_status_filter=None,
                    template_name='im/projects/project_members.html'):
    project = get_object_or_404(Project, uuid=project_uuid)

    user = request.user
    if not user.owns_project(project) and not user.is_project_admin():
        return redirect(reverse('index'))

    if not project.is_alive:
        return redirect(reverse('project_list'))

    if request.method == 'POST':
        addmembers_form = AddProjectMembersForm(request.POST,
                                                chain_id=int(chain_id),
                                                request_user=request.user)
        with ExceptionHandler(request):
            handle_valid_members_form(request, chain_id, addmembers_form)

        if addmembers_form.is_valid():
            addmembers_form = AddProjectMembersForm()  # clear form data
    else:
        addmembers_form = AddProjectMembersForm()  # initialize form

    query = api.make_membership_query({'project': project_uuid})
    members = api._get_memberships(query, request_user=user)
    approved_members_count = project.members_count()
    pending_members_count = project.count_pending_memberships()
    _limit = project.limit_on_members_number
    if _limit is not None:
        remaining_memberships_count = \
            max(0, _limit - approved_members_count)
    flt = MEMBERSHIP_STATUS_FILTER.get(members_status_filter)
    if flt is not None:
        members = members.filter(**flt)
    else:
        members = members.filter(state__in=ProjectMembership.ASSOCIATED_STATES)

    members = members.select_related()
    members_table = tables.ProjectMembersTable(project,
                                               members,
                                               user=request.user,
                                               prefix="members_")
    RequestConfig(request, paginate={
        "per_page": settings.PAGINATE_BY
    }).configure(members_table)

    user = request.user
    is_project_admin = user.is_project_admin()
    is_owner = user.owns_application(project)
    if (not (is_owner or is_project_admin)
            and not user.non_owner_can_view(project)):
        m = _(astakos_messages.NOT_ALLOWED)
        raise PermissionDenied(m)

    membership = user.get_membership(project) if project else None
    membership_id = membership.id if membership else None
    mem_display = user.membership_display(project) if project else None
    can_join_req = can_join_request(project, user) if project else False
    can_leave_req = can_leave_request(project, user) if project else False

    return DetailViewExtra.as_view(
        queryset=Project.objects.select_related(),
        template_name='im/projects/project_members.html',
        extra_context={
            'addmembers_form': addmembers_form,
            'approved_members_count': approved_members_count,
            'pending_members_count': pending_members_count,
            'members_table': members_table,
            'owner_mode': is_owner,
            'admin_mode': is_project_admin,
            'mem_display': mem_display,
            'membership_id': membership_id,
            'can_join_request': can_join_req,
            'can_leave_request': can_leave_req,
            'members_status_filter': members_status_filter,
            'project': project,
            'remaining_memberships_count': remaining_memberships_count,
        })(request, pk=project.id)
Exemplo n.º 11
0
def project_or_app_detail(request, project_uuid, app_id=None):

    project = get_object_or_404(Project, uuid=project_uuid)
    application = None
    if app_id:
        application = get_object_or_404(ProjectApplication, id=app_id)
        app_check_allowed(application, request.user)
        if request.method == "POST":
            raise PermissionDenied

    if project.state in [Project.O_PENDING] and not application and \
       project.last_application:
        return redirect(
            reverse('project_app',
                    args=(
                        project.uuid,
                        project.last_application.id,
                    )))

    members = project.projectmembership_set

    # handle members form submission
    if request.method == 'POST' and not application:
        project_check_allowed(project, request.user)
        addmembers_form = AddProjectMembersForm(request.POST,
                                                project_id=project.pk,
                                                request_user=request.user)
        with ExceptionHandler(request):
            handle_valid_members_form(request, project.pk, addmembers_form)

        if addmembers_form.is_valid():
            addmembers_form = AddProjectMembersForm()  # clear form data
    else:
        addmembers_form = AddProjectMembersForm()  # initialize form

    approved_members_count = project.members_count()
    pending_members_count = project.count_pending_memberships()
    _limit = project.limit_on_members_number
    remaining_memberships_count = (max(0, _limit - approved_members_count)
                                   if _limit is not None else None)
    members = members.associated()
    members = members.select_related()
    members_table = tables.ProjectMembersTable(project,
                                               members,
                                               user=request.user,
                                               prefix="members_")
    paginate = {"per_page": settings.PAGINATE_BY}
    RequestConfig(request, paginate=paginate).configure(members_table)

    user = request.user
    owns_base = False
    if project and project.is_base and \
                           project.realname == "system:%s" % request.user.uuid:
        owns_base = True
    is_project_admin = user.is_project_admin()
    is_owner = user.owns_project(project)
    is_applicant = False
    last_pending_app = project.last_pending_application()
    if last_pending_app:
        is_applicant = last_pending_app and \
                last_pending_app.applicant.pk == user.pk

    if not (is_owner or is_project_admin) and \
            not user.non_owner_can_view(project):
        m = _(astakos_messages.NOT_ALLOWED)
        raise PermissionDenied(m)

    if project and project.is_base and not (owns_base or is_project_admin):
        m = _(astakos_messages.NOT_ALLOWED)
        raise PermissionDenied(m)

    membership = user.get_membership(project) if project else None
    membership_id = membership.id if membership else None
    mem_display = user.membership_display(project) if project else None
    can_join_req = can_join_request(project, user) if project else False
    can_leave_req = can_leave_request(project, user) if project else False
    can_cancel_req = \
            can_cancel_join_request(project, user) if project else False

    is_modification = application.is_modification() if application else False

    queryset = Project.objects.select_related()
    object_id = project.pk
    resources_set = project.resource_set
    template_name = "im/projects/project_detail.html"
    if application:
        queryset = ProjectApplication.objects.select_related()
        object_id = application.pk
        is_applicant = application.applicant.pk == user.pk
        resources_set = application.resource_set
        template_name = "im/projects/project_application_detail.html"

    display_usage = False
    if (owns_base or is_owner or membership or is_project_admin) \
                                                               and not app_id:
        display_usage = True

    return DetailViewExtra.as_view(
        queryset=queryset,
        template_name=template_name,
        extra_context={
            'project': project,
            'application': application,
            'is_application': bool(application),
            'display_usage': display_usage,
            'is_modification': is_modification,
            'addmembers_form': addmembers_form,
            'approved_members_count': approved_members_count,
            'pending_members_count': pending_members_count,
            'members_table': members_table,
            'owner_mode': is_owner,
            'admin_mode': is_project_admin,
            'applicant_mode': is_applicant,
            'mem_display': mem_display,
            'membership_id': membership_id,
            'can_join_request': can_join_req,
            'can_leave_request': can_leave_req,
            'can_cancel_join_request': can_cancel_req,
            'resources_set': resources_set,
            'last_app': None if application else project.last_application,
            'remaining_memberships_count': remaining_memberships_count
        })(request, pk=object_id)
Exemplo n.º 12
0
def project_reject_member(request, memb_id):

    with ExceptionHandler(request):
        _project_reject_member(request, memb_id)

    return redirect_back(request, 'project_list')
Exemplo n.º 13
0
def common_detail(request,
                  chain_or_app_id,
                  project_view=True,
                  template_name='im/projects/project_detail.html',
                  members_status_filter=None):
    project = None
    approved_members_count = 0
    pending_members_count = 0
    remaining_memberships_count = None
    if project_view:
        chain_id = chain_or_app_id
        if request.method == 'POST':
            addmembers_form = AddProjectMembersForm(request.POST,
                                                    chain_id=int(chain_id),
                                                    request_user=request.user)
            with ExceptionHandler(request):
                addmembers(request, chain_id, addmembers_form)

            if addmembers_form.is_valid():
                addmembers_form = AddProjectMembersForm()  # clear form data
        else:
            addmembers_form = AddProjectMembersForm()  # initialize form

        project = get_object_or_404(Project, pk=chain_id)
        application = project.application
        if project:
            members = project.projectmembership_set
            approved_members_count = project.members_count()
            pending_members_count = project.count_pending_memberships()
            _limit = application.limit_on_members_number
            if _limit is not None:
                remaining_memberships_count = \
                    max(0, _limit - approved_members_count)
            flt = MEMBERSHIP_STATUS_FILTER.get(members_status_filter)
            if flt is not None:
                members = flt(members)
            else:
                members = members.associated()
            members = members.select_related()
            members_table = tables.ProjectMembersTable(project,
                                                       members,
                                                       user=request.user,
                                                       prefix="members_")
        else:
            members_table = None

    else:
        # is application
        application_id = chain_or_app_id
        application = get_object_or_404(ProjectApplication, pk=application_id)
        members_table = None
        addmembers_form = None

    user = request.user
    is_project_admin = user.is_project_admin(application_id=application.id)
    is_owner = user.owns_application(application)
    if not (is_owner or is_project_admin) and not project_view:
        m = _(astakos_messages.NOT_ALLOWED)
        raise PermissionDenied(m)

    if (not (is_owner or is_project_admin) and project_view
            and not user.non_owner_can_view(project)):
        m = _(astakos_messages.NOT_ALLOWED)
        raise PermissionDenied(m)

    membership = user.get_membership(project) if project else None
    membership_id = membership.id if membership else None
    mem_display = user.membership_display(project) if project else None
    can_join_req = can_join_request(project, user) if project else False
    can_leave_req = can_leave_request(project, user) if project else False

    return object_detail(request,
                         queryset=ProjectApplication.objects.select_related(),
                         object_id=application.id,
                         template_name=template_name,
                         extra_context={
                             'project_view':
                             project_view,
                             'chain_id':
                             chain_or_app_id,
                             'application':
                             application,
                             'addmembers_form':
                             addmembers_form,
                             'approved_members_count':
                             approved_members_count,
                             'pending_members_count':
                             pending_members_count,
                             'members_table':
                             members_table,
                             'owner_mode':
                             is_owner,
                             'admin_mode':
                             is_project_admin,
                             'mem_display':
                             mem_display,
                             'membership_id':
                             membership_id,
                             'can_join_request':
                             can_join_req,
                             'can_leave_request':
                             can_leave_req,
                             'members_status_filter':
                             members_status_filter,
                             'remaining_memberships_count':
                             remaining_memberships_count,
                         })