Esempio n. 1
0
def _get_application(app_id, request_user=None):
    application = functions.get_application(app_id)
    functions.app_check_allowed(
        application, request_user, level=functions.APPLICANT_LEVEL)
    return application
Esempio n. 2
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)
Esempio n. 3
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 object_detail(
        request,
        queryset=queryset,
        object_id=object_id,
        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
        })