예제 #1
0
def resource_revision_add(request, pk):
    """
    Add a code resource revision.  The form will initially be populated with values of the last
    revision to this code resource.
    """
    t = loader.get_template('method/resource_revision_add.html')
    c = {}
    creating_user = request.user
    parent_revision = CodeResourceRevision.check_accessible(pk, creating_user)
    coderesource = parent_revision.coderesource

    if request.method == 'POST':
        # Use forms here, just as in resource_add.  Again note that entries of dep_forms may be None.
        revision_form = CodeResourceRevisionForm(request.POST, request.FILES)
        if not revision_form.is_valid():
            c.update({
                'revision_form': revision_form,
                'parent_revision': parent_revision,
                'coderesource': coderesource
            })
            return HttpResponse(t.render(c, request))

        try:
            _make_crv(request.FILES.get('content_file', None),
                      creating_user,
                      revision_form,
                      parent_revision=parent_revision)
        except ValidationError:
            # The forms have all been updated with the appropriate errors.
            c.update({
                'revision_form': revision_form,
                'parent_revision': parent_revision,
                'coderesource': coderesource
            })
            return HttpResponse(
                t.render(c, request)
            )  # CodeResourceRevision object required for next steps

        # Success; return to the resources page.
        return HttpResponseRedirect('/resources')

    # Having reached here, we know that this CR is being revised.  Return a form pre-populated
    # with default info.
    parent_users_allowed = [
        x.username for x in parent_revision.users_allowed.all()
    ]
    parent_groups_allowed = [
        x.name for x in parent_revision.groups_allowed.all()
    ]
    crv_form = CodeResourceRevisionForm(
        initial={"permissions": [parent_users_allowed, parent_groups_allowed]})

    c.update({
        'revision_form': crv_form,
        'parent_revision': parent_revision,
        'coderesource': coderesource
    })
    return HttpResponse(t.render(c, request))
예제 #2
0
파일: views.py 프로젝트: cfe-lab/Kive
def resource_revision_add(request, pk):
    """
    Add a code resource revision.  The form will initially be populated with values of the last
    revision to this code resource.
    """
    t = loader.get_template('method/resource_revision_add.html')
    c = {}
    creating_user = request.user
    parent_revision = CodeResourceRevision.check_accessible(pk, creating_user)
    coderesource = parent_revision.coderesource

    if request.method == 'POST':
        # Use forms here, just as in resource_add.  Again note that entries of dep_forms may be None.
        revision_form = CodeResourceRevisionForm(request.POST, request.FILES)
        if not revision_form.is_valid():
            c.update({
                'revision_form': revision_form,
                'parent_revision': parent_revision,
                'coderesource': coderesource
            })
            return HttpResponse(t.render(c, request))

        try:
            _make_crv(request.FILES.get('content_file', None), creating_user, revision_form,
                      parent_revision=parent_revision)
        except ValidationError:
            # The forms have all been updated with the appropriate errors.
            c.update(
                {
                    'revision_form': revision_form,
                    'parent_revision': parent_revision,
                    'coderesource': coderesource
                })
            return HttpResponse(t.render(c, request))  # CodeResourceRevision object required for next steps

        # Success; return to the resources page.
        return HttpResponseRedirect('/resources')

    # Having reached here, we know that this CR is being revised.  Return a form pre-populated
    # with default info.
    parent_users_allowed = [x.username for x in parent_revision.users_allowed.all()]
    parent_groups_allowed = [x.name for x in parent_revision.groups_allowed.all()]
    crv_form = CodeResourceRevisionForm(
        initial={
            "permissions": [parent_users_allowed, parent_groups_allowed]
        }
    )

    c.update(
        {
            'revision_form': crv_form,
            'parent_revision': parent_revision,
            'coderesource': coderesource
        }
    )
    return HttpResponse(t.render(c, request))
예제 #3
0
def resource_revision_view(request, pk):
    revision = CodeResourceRevision.check_accessible(pk, request.user)
    addable_users, addable_groups = revision.other_users_groups()

    if request.method == 'POST':
        # We are attempting to update the CodeResourceRevision's metadata/permissions.
        revision_form = CodeResourceRevisionDetailsForm(
            request.POST,
            addable_users=addable_users,
            addable_groups=addable_groups,
            instance=revision)

        if revision_form.is_valid():
            try:
                revision.revision_name = revision_form.cleaned_data[
                    "revision_name"]
                revision.revision_desc = revision_form.cleaned_data[
                    "revision_desc"]
                revision.save()
                revision.grant_from_json(
                    revision_form.cleaned_data["permissions"])
                revision.clean()

                # Success -- go back to the CodeResource page.
                return HttpResponseRedirect('/resource_revisions/{}'.format(
                    revision.coderesource.pk))
            except (AttributeError, ValidationError, ValueError) as e:
                LOGGER.exception(e.message)
                revision_form.add_error(None, e)

    else:
        revision_form = CodeResourceRevisionDetailsForm(
            addable_users=addable_users,
            addable_groups=addable_groups,
            initial={
                "revision_name": revision.revision_name,
                "revision_desc": revision.revision_desc
            })

    t = loader.get_template("method/resource_revision_view.html")
    c = {
        "revision": revision,
        "revision_form": revision_form,
        "is_owner": revision.user == request.user,
        "is_admin": admin_check(request.user)
    }
    return HttpResponse(t.render(c, request))
예제 #4
0
파일: views.py 프로젝트: cfe-lab/Kive
def resource_revision_view(request, pk):
    revision = CodeResourceRevision.check_accessible(pk, request.user)
    addable_users, addable_groups = revision.other_users_groups()

    if request.method == 'POST':
        # We are attempting to update the CodeResourceRevision's metadata/permissions.
        revision_form = CodeResourceRevisionDetailsForm(
            request.POST,
            addable_users=addable_users,
            addable_groups=addable_groups,
            instance=revision
        )

        if revision_form.is_valid():
            try:
                revision.revision_name = revision_form.cleaned_data["revision_name"]
                revision.revision_desc = revision_form.cleaned_data["revision_desc"]
                revision.save()
                revision.grant_from_json(revision_form.cleaned_data["permissions"])
                revision.clean()

                # Success -- go back to the CodeResource page.
                return HttpResponseRedirect('/resource_revisions/{}'.format(revision.coderesource.pk))
            except (AttributeError, ValidationError, ValueError) as e:
                LOGGER.exception(e.message)
                revision_form.add_error(None, e)

    else:
        revision_form = CodeResourceRevisionDetailsForm(
            addable_users=addable_users,
            addable_groups=addable_groups,
            initial={
                "revision_name": revision.revision_name,
                "revision_desc": revision.revision_desc
            }
        )

    t = loader.get_template("method/resource_revision_view.html")
    c = {
        "revision": revision,
        "revision_form": revision_form,
        "is_owner": revision.user == request.user,
        "is_admin": admin_check(request.user)
    }
    return HttpResponse(t.render(c, request))