Exemplo n.º 1
0
def method_revise(request, pk):
    """
    Add a revision of an existing Method.  revision_parent is defined by the
    previous version.
    """
    t = loader.get_template('method/method.html')
    c = {}
    creating_user = request.user

    # Retrieve the most recent member of this Method's family.
    parent_method = Method.check_accessible(pk, creating_user)
    family = parent_method.family

    # Retrieve the most recent revision of the corresponding CR.
    parent_revision = parent_method.driver
    if not parent_revision:
        this_code_resource = None
        all_revisions = []
    else:
        this_code_resource = parent_revision.coderesource
        # Filter the available revisions by user.
        all_revisions = CodeResourceRevision.filter_by_user(
            creating_user,
            queryset=this_code_resource.revisions.all()).order_by(
                '-revision_DateTime')

    parent_container = parent_method.container
    if not parent_container:
        this_container_family = None
        all_containers = []
    else:
        this_container_family = parent_container.family
        all_containers = Container.filter_by_user(
            creating_user, queryset=this_container_family.containers.all())

    if request.method == 'POST':
        # Because there is no CodeResource specified, the second value is of type MethodReviseForm.
        family_form, method_revise_form,\
            dep_forms, input_form_tuples,\
            output_form_tuples, _ = create_method_forms(request.POST, creating_user, family=family)
        if not _method_forms_check_valid(family_form, method_revise_form,
                                         dep_forms, input_form_tuples,
                                         output_form_tuples):
            # Bail out now if there are any problems.
            c.update({
                'coderesource': this_code_resource,
                'containerfamily': this_container_family,
                'method_revise_form': method_revise_form,
                'dep_forms': dep_forms,
                'input_forms': input_form_tuples,
                'output_forms': output_form_tuples,
                'family': family,
                'family_form': family_form,
                'parent': parent_method,
                'docker_default': DockerImage.DEFAULT_IMAGE
            })
            return HttpResponse(t.render(c, request))

        # Next, attempt to build the Method and add it to family.
        create_method_from_forms(family_form,
                                 method_revise_form,
                                 dep_forms,
                                 input_form_tuples,
                                 output_form_tuples,
                                 creating_user,
                                 family=family,
                                 parent_method=parent_method)
        if _method_forms_check_valid(family_form, method_revise_form,
                                     dep_forms, input_form_tuples,
                                     output_form_tuples):
            # Success!
            return HttpResponseRedirect('/methods/{}'.format(family.pk))

    else:
        # Initialize forms with values of parent Method.
        family_form = MethodFamilyForm({
            "name": family.name,
            "description": family.description
        })
        parent_users_allowed = [
            x.username for x in parent_method.users_allowed.all()
        ]
        parent_groups_allowed = [
            x.name for x in parent_method.groups_allowed.all()
        ]
        method_revise_form = MethodReviseForm(
            initial={
                "revision_desc": parent_method.revision_desc,
                "driver_revisions": parent_revision and parent_revision.pk,
                "docker_image": parent_method.docker_image_id,
                "reusable": parent_method.reusable,
                "threads": parent_method.threads,
                "memory": parent_method.memory,
                "permissions": [parent_users_allowed, parent_groups_allowed]
            })

        dependencies = parent_method.dependencies.all()
        dep_forms = []
        for i, dependency in enumerate(dependencies):
            its_crv = dependency.requirement
            its_cr = its_crv.coderesource
            dep_form = MethodDependencyForm(user=creating_user,
                                            auto_id='id_%s_' + str(i),
                                            initial={
                                                'coderesource': its_cr.pk,
                                                'revisions': its_crv.pk,
                                                'path': dependency.path,
                                                'filename': dependency.filename
                                            })
            dep_forms.append(dep_form)
        # If the parent Method has no dependencies, add a blank form.
        if len(dep_forms) == 0:
            dep_forms.append(
                MethodDependencyForm(user=creating_user, auto_id='id_%s_0'))

        xput_forms = []
        inputs = parent_method.inputs.order_by("dataset_idx")
        outputs = parent_method.outputs.order_by("dataset_idx")
        for xput_type, xputs in (("in", inputs), ("out", outputs)):
            forms = []
            for xput in xputs:
                tx_form = TransformationXputForm(auto_id='id_%s_{}_{}'.format(
                    xput_type, len(forms)),
                                                 initial={
                                                     'dataset_name':
                                                     xput.dataset_name,
                                                     'dataset_idx':
                                                     xput.dataset_idx
                                                 })
                if xput.has_structure:
                    structure = xput.structure
                    xs_form = XputStructureForm(
                        user=creating_user,
                        auto_id='id_%s_{}_{}'.format(xput_type, len(forms)),
                        initial={
                            'compounddatatype': structure.compounddatatype.id,
                            'min_row': structure.min_row,
                            'max_row': structure.max_row
                        })
                else:
                    xs_form = XputStructureForm(
                        user=creating_user,
                        auto_id='id_%s_{}_{}'.format(xput_type, len(forms)),
                        initial={'compounddatatype': '__raw__'})

                forms.append((tx_form, xs_form))
            xput_forms.append(forms)

        input_form_tuples, output_form_tuples = xput_forms
        # if previous Method has no inputs, provide blank forms
        if len(input_form_tuples) == 0:
            tx_form = TransformationXputForm(auto_id='id_%s_in_0')
            xs_form = XputStructureForm(user=creating_user,
                                        auto_id='id_%s_in_0')
            input_form_tuples.append((tx_form, xs_form))

    method_revise_form.fields['driver_revisions'].widget.choices = [
        (str(x.id), '{}: {}'.format(x.revision_number, x.revision_name))
        for x in all_revisions
    ]
    method_revise_form.fields['container'].widget.choices = [
        (str(x.id), x.tag) for x in all_containers
    ]
    c.update({
        'coderesource': this_code_resource,
        'containerfamily': this_container_family,
        'method_form': method_revise_form,
        'dep_forms': dep_forms,
        'input_forms': input_form_tuples,
        'output_forms': output_form_tuples,
        'family': family,
        'family_form': family_form,
        'parent': parent_method,
        'docker_default': DockerImage.DEFAULT_IMAGE
    })
    return HttpResponse(t.render(c, request))
Exemplo n.º 2
0
def method_revise(request, pk):
    """
    Add a revision of an existing Method.  revision_parent is defined by the
    previous version.
    """
    t = loader.get_template('method/method.html')
    c = {}
    creating_user = request.user

    # Retrieve the most recent member of this Method's family.
    parent_method = Method.check_accessible(pk, creating_user)
    family = parent_method.family

    # Retrieve the most recent revision of the corresponding CR.
    parent_revision = parent_method.driver
    if not parent_revision:
        this_code_resource = None
        all_revisions = []
    else:
        this_code_resource = parent_revision.coderesource
        # Filter the available revisions by user.
        all_revisions = CodeResourceRevision.filter_by_user(
            creating_user,
            queryset=this_code_resource.revisions.all()).order_by('-revision_DateTime')

    parent_container = parent_method.container
    if not parent_container:
        this_container_family = None
        all_containers = []
    else:
        this_container_family = parent_container.family
        all_containers = Container.filter_by_user(
            creating_user,
            queryset=this_container_family.containers.all())

    if request.method == 'POST':
        # Because there is no CodeResource specified, the second value is of type MethodReviseForm.
        family_form, method_revise_form,\
            dep_forms, input_form_tuples,\
            output_form_tuples, _ = create_method_forms(request.POST, creating_user, family=family)
        if not _method_forms_check_valid(family_form, method_revise_form, dep_forms,
                                         input_form_tuples, output_form_tuples):
            # Bail out now if there are any problems.
            c.update(
                {
                    'coderesource': this_code_resource,
                    'containerfamily': this_container_family,
                    'method_revise_form': method_revise_form,
                    'dep_forms': dep_forms,
                    'input_forms': input_form_tuples,
                    'output_forms': output_form_tuples,
                    'family': family,
                    'family_form': family_form,
                    'parent': parent_method,
                    'docker_default': DockerImage.DEFAULT_IMAGE
                })
            return HttpResponse(t.render(c, request))

        # Next, attempt to build the Method and add it to family.
        create_method_from_forms(
            family_form, method_revise_form, dep_forms, input_form_tuples, output_form_tuples, creating_user,
            family=family, parent_method=parent_method
        )
        if _method_forms_check_valid(family_form, method_revise_form, dep_forms,
                                     input_form_tuples, output_form_tuples):
            # Success!
            return HttpResponseRedirect('/methods/{}'.format(family.pk))

    else:
        # Initialize forms with values of parent Method.
        family_form = MethodFamilyForm({"name": family.name, "description": family.description})
        parent_users_allowed = [x.username for x in parent_method.users_allowed.all()]
        parent_groups_allowed = [x.name for x in parent_method.groups_allowed.all()]
        method_revise_form = MethodReviseForm(
            initial={
                "revision_desc": parent_method.revision_desc,
                "driver_revisions": parent_revision and parent_revision.pk,
                "docker_image": parent_method.docker_image_id,
                "reusable": parent_method.reusable,
                "threads": parent_method.threads,
                "memory": parent_method.memory,
                "permissions": [parent_users_allowed, parent_groups_allowed]
            })

        dependencies = parent_method.dependencies.all()
        dep_forms = []
        for i, dependency in enumerate(dependencies):
            its_crv = dependency.requirement
            its_cr = its_crv.coderesource
            dep_form = MethodDependencyForm(
                user=creating_user,
                auto_id='id_%s_'+str(i),
                initial={
                    'coderesource': its_cr.pk,
                    'revisions': its_crv.pk,
                    'path': dependency.path,
                    'filename': dependency.filename
                }
            )
            dep_forms.append(dep_form)
        # If the parent Method has no dependencies, add a blank form.
        if len(dep_forms) == 0:
            dep_forms.append(MethodDependencyForm(user=creating_user, auto_id='id_%s_0'))

        xput_forms = []
        inputs = parent_method.inputs.order_by("dataset_idx")
        outputs = parent_method.outputs.order_by("dataset_idx")
        for xput_type, xputs in (("in", inputs), ("out", outputs)):
            forms = []
            for xput in xputs:
                tx_form = TransformationXputForm(auto_id='id_%s_{}_{}'.format(xput_type, len(forms)),
                                                 initial={'dataset_name': xput.dataset_name,
                                                          'dataset_idx': xput.dataset_idx})
                if xput.has_structure:
                    structure = xput.structure
                    xs_form = XputStructureForm(user=creating_user,
                                                auto_id='id_%s_{}_{}'.format(xput_type, len(forms)),
                                                initial={'compounddatatype': structure.compounddatatype.id,
                                                         'min_row': structure.min_row,
                                                         'max_row': structure.max_row})
                else:
                    xs_form = XputStructureForm(user=creating_user,
                                                auto_id='id_%s_{}_{}'.format(xput_type, len(forms)),
                                                initial={'compounddatatype': '__raw__'})

                forms.append((tx_form, xs_form))
            xput_forms.append(forms)

        input_form_tuples, output_form_tuples = xput_forms
        # if previous Method has no inputs, provide blank forms
        if len(input_form_tuples) == 0:
            tx_form = TransformationXputForm(auto_id='id_%s_in_0')
            xs_form = XputStructureForm(user=creating_user, auto_id='id_%s_in_0')
            input_form_tuples.append((tx_form, xs_form))

    method_revise_form.fields['driver_revisions'].widget.choices = [
        (str(x.id), '{}: {}'.format(x.revision_number, x.revision_name)) for x in all_revisions
    ]
    method_revise_form.fields['container'].widget.choices = [
        (str(x.id), x.tag) for x in all_containers]
    c.update(
        {
            'coderesource': this_code_resource,
            'containerfamily': this_container_family,
            'method_form': method_revise_form,
            'dep_forms': dep_forms,
            'input_forms': input_form_tuples,
            'output_forms': output_form_tuples,
            'family': family,
            'family_form': family_form,
            'parent': parent_method,
            'docker_default': DockerImage.DEFAULT_IMAGE
        }
    )
    return HttpResponse(t.render(c, request))
Exemplo n.º 3
0
def method_view(request, pk):
    """
    View a Method or edit its metadata/permissions.
    """
    method = Method.check_accessible(pk, request.user)
    addable_users, addable_groups = method.other_users_groups()
    addable_users, addable_groups = method.family.intersect_permissions(
        addable_users, addable_groups)
    if method.revision_parent is not None:
        addable_users, addable_groups = (
            method.revision_parent.intersect_permissions(
                addable_users, addable_groups))
    if method.driver is not None:
        addable_users, addable_groups = method.driver.intersect_permissions(
            addable_users, addable_groups)
    if method.docker_image is not None:
        addable_users, addable_groups = (
            method.docker_image.intersect_permissions(addable_users,
                                                      addable_groups))
    for dep in method.dependencies.all():
        addable_users, addable_groups = dep.requirement.intersect_permissions(
            addable_users, addable_groups)
    for xput in itertools.chain(method.inputs.all(), method.outputs.all()):
        xput_cdt = xput.get_cdt()
        if xput_cdt is not None:
            addable_users, addable_groups = xput_cdt.intersect_permissions(
                addable_users, addable_groups)

    if request.method == 'POST':
        # We are attempting to update the Method's metadata/permissions.
        method_form = MethodDetailsForm(request.POST,
                                        addable_users=addable_users,
                                        addable_groups=addable_groups,
                                        instance=method)

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

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

    else:
        method_form = MethodDetailsForm(addable_users=addable_users,
                                        addable_groups=addable_groups,
                                        initial={
                                            "revision_name":
                                            method.revision_name,
                                            "revision_desc":
                                            method.revision_desc
                                        })

    t = loader.get_template("method/method_view.html")
    c = {
        "method": method,
        "method_form": method_form,
        "is_owner": method.user == request.user,
        "is_admin": admin_check(request.user)
    }
    return HttpResponse(t.render(c, request))
Exemplo n.º 4
0
def method_view(request, pk):
    """
    View a Method or edit its metadata/permissions.
    """
    method = Method.check_accessible(pk, request.user)
    addable_users, addable_groups = method.other_users_groups()
    addable_users, addable_groups = method.family.intersect_permissions(
        addable_users,
        addable_groups)
    if method.revision_parent is not None:
        addable_users, addable_groups = (
            method.revision_parent.intersect_permissions(addable_users,
                                                         addable_groups))
    if method.driver is not None:
        addable_users, addable_groups = method.driver.intersect_permissions(
            addable_users,
            addable_groups)
    if method.docker_image is not None:
        addable_users, addable_groups = (
            method.docker_image.intersect_permissions(addable_users,
                                                      addable_groups))
    for dep in method.dependencies.all():
        addable_users, addable_groups = dep.requirement.intersect_permissions(addable_users, addable_groups)
    for xput in itertools.chain(method.inputs.all(), method.outputs.all()):
        xput_cdt = xput.get_cdt()
        if xput_cdt is not None:
            addable_users, addable_groups = xput_cdt.intersect_permissions(addable_users, addable_groups)

    if request.method == 'POST':
        # We are attempting to update the Method's metadata/permissions.
        method_form = MethodDetailsForm(
            request.POST,
            addable_users=addable_users,
            addable_groups=addable_groups,
            instance=method
        )

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

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

    else:
        method_form = MethodDetailsForm(
            addable_users=addable_users,
            addable_groups=addable_groups,
            initial={
                "revision_name": method.revision_name,
                "revision_desc": method.revision_desc
            }
        )

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