Ejemplo n.º 1
0
def api_get_existing_edit_form_section(request, project_name, section_key, model_id, **kwargs):

    # check the arguments to the view,
    # and parse the section key
    (validity, version, model_proxy, vocabulary, component_proxy, property_type, category_proxy, property_proxy, model_customizer, vocabularies, msg) = \
        validate_edit_view_arguments(project_name, section_key)
    if not validity:
        return questionnaire_error(request, msg)

    # and check requested model(s)...
    # (even though this view may return a small section for a single model,
    # the full set of models is needed to re-create the forms properly)
    try:
        root_model = MetadataModel.objects.get(pk=model_id)
    except MetadataModel.DoesNotExist:
        msg = "Cannot find the specified model.  Please try again."
        return questionnaire_error(request, msg)
    if not root_model.is_root:
        # TODO: DEAL W/ THIS USE-CASE
        msg = "Currently only root models can be viewed.  Please try again."
        return questionnaire_error(request, msg)

    # get (or set) models from the cache...
    session_key = get_key_from_request(request)
    cached_customization_set_key = "{0}_customization_set".format(session_key)
    cached_proxy_set_key = "{0}_proxy_set".format(session_key)
    cached_realization_set_key = "{0}_realization_set".format(session_key)
    customization_set = get_or_create_cached_object(request.session, cached_customization_set_key,
        get_existing_customization_set,
        **{
            "project": model_customizer.project,
            "ontology": version,
            "proxy": model_customizer.proxy,
            "customization_name": model_customizer.name,
        }
    )
    customization_set = convert_customization_set(customization_set)
    customization_set["scientific_category_customizers"] = get_joined_keys_dict(customization_set["scientific_category_customizers"])
    customization_set["scientific_property_customizers"] = get_joined_keys_dict(customization_set["scientific_property_customizers"])
    proxy_set = get_or_create_cached_object(request.session, cached_proxy_set_key,
        get_existing_proxy_set,
        **{
            "ontology": version,
            "proxy": model_proxy,
            "vocabularies": vocabularies,
        }
    )
    proxy_set = convert_proxy_set(proxy_set)
    realization_set = get_or_create_cached_object(request.session, cached_realization_set_key,
        get_existing_realization_set,
        **{
            "models": root_model.get_descendants(include_self=True),
            "model_customizer": customization_set["model_customizer"],
            "vocabularies": vocabularies,
        }
    )
    inheritance_data = get_cached_inheritance_data(session_key)

    # now create the formsets...
    (model_formset, standard_properties_formsets, scientific_properties_formsets) = \
        create_existing_edit_forms_from_models(
            realization_set["models"], customization_set["model_customizer"],
            realization_set["standard_properties"], customization_set["standard_property_customizers"],
            realization_set["scientific_properties"], customization_set["scientific_property_customizers"],
            inheritance_data=inheritance_data
        )

    # now get some things that were previously computed in the master template
    # or in loops that I need to recreate for the individual sections
    section_keys = section_key.split('|')
    model_key = u"%s_%s" % (section_keys[2], section_keys[3])
    _dict = {
        "vocabularies": vocabularies,
        "model_customizer": model_customizer,
        "model_formset": model_formset,
        "standard_properties_formsets": standard_properties_formsets,
        "scientific_properties_formsets": scientific_properties_formsets,
        "section_parameters": {
            "model_form": get_form_by_prefix(model_formset, model_key),
            "standard_property_formset": standard_properties_formsets[model_key],
            "scientific_property_formset": scientific_properties_formsets[model_key],
            "active_standard_categories_and_properties": get_active_standard_categories_and_properties(customization_set["model_customizer"]),
            "active_scientific_categories_and_properties": get_active_scientific_categories_and_properties_by_key(customization_set["model_customizer"], model_key),
        },
    }

    template = get_edit_section_template_path(property_proxy, property_type, category_proxy)
    return render_to_response(template, _dict, context_instance=RequestContext(request))
Ejemplo n.º 2
0
def api_get_new_edit_form_section(request, project_name, section_key, **kwargs):

    # check the arguments to the view,
    # and parse the section key
    (validity, version, model_proxy, vocabulary, component_proxy, property_type, category_proxy, property_proxy, model_customizer, vocabularies, msg) = \
        validate_edit_view_arguments(project_name, section_key)
    if not validity:
        return questionnaire_error(request, msg)

    # get (or set) models from the cache...
    session_key = get_key_from_request(request)
    cached_customization_set_key = "{0}_customization_set".format(session_key)
    cached_proxy_set_key = "{0}_proxy_set".format(session_key)
    cached_realization_set_key = "{0}_realization_set".format(session_key)
    customization_set = get_or_create_cached_object(request.session, cached_customization_set_key,
        get_existing_customization_set,
        **{
            "project": model_customizer.project,
            "ontology": version,
            "proxy": model_customizer.proxy,
            "customization_name": model_customizer.name,
        }
    )
    customization_set = convert_customization_set(customization_set)
    customization_set["scientific_category_customizers"] = get_joined_keys_dict(customization_set["scientific_category_customizers"])
    customization_set["scientific_property_customizers"] = get_joined_keys_dict(customization_set["scientific_property_customizers"])
    proxy_set = get_or_create_cached_object(request.session, cached_proxy_set_key,
        get_existing_proxy_set,
        **{
            "ontology": version,
            "proxy": model_proxy,
            "vocabularies": vocabularies,
        }
    )
    proxy_set = convert_proxy_set(proxy_set)
    realization_set = get_or_create_cached_object(request.session, cached_realization_set_key,
        get_new_realization_set,
        **{
            "project": model_customizer.project,
            "ontology": version,
            "model_proxy": proxy_set["model_proxy"],
            "standard_property_proxies": proxy_set["standard_property_proxies"],
            "scientific_property_proxies": proxy_set["scientific_property_proxies"],
            "model_customizer": customization_set["model_customizer"],
            "vocabularies": vocabularies,
        }
    )
    inheritance_data = get_cached_inheritance_data(session_key)

    # now create the formsets...
    (model_formset, standard_properties_formsets, scientific_properties_formsets) = \
        create_new_edit_forms_from_models(
            realization_set["models"], customization_set["model_customizer"],
            realization_set["standard_properties"], customization_set["standard_property_customizers"],
            realization_set["scientific_properties"], customization_set["scientific_property_customizers"],
            inheritance_data=inheritance_data,
        )

    # now get some things that were previously computed in the master template
    # or in loops that I need to recreate for the individual sections
    section_keys = section_key.split('|')
    model_key = u"%s_%s" % (section_keys[2], section_keys[3])
    _dict = {
        "vocabularies": vocabularies,
        "model_customizer": model_customizer,
        "model_formset": model_formset,
        "standard_properties_formsets": standard_properties_formsets,
        "scientific_properties_formsets": scientific_properties_formsets,
        "section_parameters": {
            "model_form": get_form_by_prefix(model_formset, model_key),
            "standard_property_formset": standard_properties_formsets[model_key],
            "scientific_property_formset": scientific_properties_formsets[model_key],
            "active_standard_categories_and_properties": get_active_standard_categories_and_properties(customization_set["model_customizer"]),
            "active_scientific_categories_and_properties": get_active_scientific_categories_and_properties_by_key(customization_set["model_customizer"], model_key),
        },
    }

    template = get_edit_section_template_path(property_proxy, property_type, category_proxy)
    return render_to_response(template, _dict, context_instance=RequestContext(request))