Esempio n. 1
0
def get_data(request):
    """ Perform an OAI-PMH request.
    Args:
        request:

    Returns:

    """
    url = request.GET['url']
    args_url = json.loads(request.GET['args_url'])
    # Encode args for the Get request
    encoded_args = urllib.urlencode(args_url)
    # Build the url
    url = url + "?" + encoded_args
    try:
        req = oai_verb_api.get_data(url)
        xml_string = req.data
        request.session['xmlStringOAIPMH'] = str(xml_string.encode("utf8"))
        # loads XSLT
        xslt_path = finders.find(
            join('core_main_app', 'common', 'xsl', 'xml2html.xsl'))
        # reads XSLT
        xslt_string = _read_file_content(xslt_path)
        # transform XML to HTML
        xml_to_html_string = xsl_transform(xml_string, xslt_string)
        content = {'message': xml_to_html_string}

        return HttpResponse(json.dumps(content),
                            content_type="application/javascript")
    except Exception as e:
        return HttpResponseBadRequest(e.message,
                                      content_type="application/javascript")
Esempio n. 2
0
def transform_xsd_to_html_with_modules(xsd_string):
    """Convert xsd string with modules to html.

    Args:
        xsd_string:

    Returns:

    """
    # Get path to XSLT file
    xslt_path = finders.find("core_parser_app/xsl/xsd2html4modules.xsl")

    # get global namespace used in the schema
    global_namespace = get_global_namespace(xsd_string)
    # if a global namespace is present in the schema
    if global_namespace:
        # if the global namespace is the schema namespace
        if global_namespace == SCHEMA_NAMESPACE:
            # a prefix for this namespace is already present in the XSLT
            # FIXME: xsd prefix hardcoded here based on what is in the XSLT file.
            module_tag_name = "xsd:{0}".format(MODULE_TAG_NAME)
        else:
            # the schema is using a global namespace and it's not the XML Schema namespace
            # FIXME: to support this case, we would need to add a namespace prefix to the XSLT
            raise NotImplementedError(
                "The schema is using an unsupported global namespace.")
    else:
        # no global namespace used
        module_tag_name = MODULE_TAG_NAME

    return xsl_transform(
        xsd_string,
        read_and_update_xslt_with_settings(xslt_path, module_tag_name))
def get_data(request):
    """Perform an OAI-PMH request.
    Args:
        request:

    Returns:

    """
    url = request.GET["url"]
    args_url = json.loads(request.GET["args_url"])
    # Encode args for the Get request
    encoded_args = urllib.parse.urlencode(args_url)
    # Build the url
    url = url + "?" + encoded_args
    try:
        xml_string = oai_verb_api.get_data(url, request=request).data
        request.session["xmlStringOAIPMH"] = xml_string
        # loads XSLT
        xslt_path = finders.find(
            join("core_main_app", "common", "xsl", "xml2html.xsl"))
        # reads XSLT
        xslt_string = _read_file_content(xslt_path)
        # transform XML to HTML
        xml_to_html_string = xsl_transform(xml_string, xslt_string)
        content = {"message": xml_to_html_string}

        return HttpResponse(json.dumps(content),
                            content_type="application/javascript")
    except Exception as e:
        return HttpResponseBadRequest(escape(str(e)),
                                      content_type="application/javascript")
Esempio n. 4
0
def _render_xml_as_html(
    xml_string,
    template_id=None,
    template_hash=None,
    xslt_type=XSLType.type_list,
    xsl_transform_id=None,
):
    """Render an XML to HTML according to an xslt type (list or detail).
    Args:
        xml_string:
        template_id:
        template_hash:

        xslt_type:

    Returns:
        HTML

    """
    try:
        try:
            if xslt_type not in (XSLType.type_list, XSLType.type_detail):
                raise Exception("XSLT Type unknown. Default xslt will be used.")
            if xsl_transform_id:
                xsl_transformation = xsl_transformation_api.get_by_id(xsl_transform_id)
            elif template_id or template_hash:
                if template_id:
                    template_xsl_rendering = (
                        template_xsl_rendering_api.get_by_template_id(template_id)
                    )
                else:
                    template_xsl_rendering = (
                        template_xsl_rendering_api.get_by_template_hash(template_hash)
                    )

                if xslt_type == XSLType.type_list:
                    xsl_transformation = template_xsl_rendering.list_xslt
                else:
                    xsl_transformation = template_xsl_rendering.default_detail_xslt
            else:
                raise Exception(
                    "No template information provided. Default xslt will be used."
                )

            xslt_string = xsl_transformation.content

        except (Exception, exceptions.DoesNotExist):
            default_xslt_path = finders.find(DEFAULT_DATA_RENDERING_XSLT)
            xslt_string = read_file_content(default_xslt_path)

        return xsl_transform(xml_string, xslt_string)
    except Exception:
        return xml_string
Esempio n. 5
0
def xsl_transform(xml_content, xslt_name):
    """ Transform an XML file using an XSL transformation.

    Args:
        xml_content (str): XML document content, encoded in UTF-8
        xslt_name (str): Name of an XslTransformation document

    Returns:
        str: Transformed XML string
    """
    xslt_object = get_by_name(xslt_name)

    try:
        return xml.xsl_transform(xml_content, xslt_object.content)
    except Exception:
        raise exceptions.ApiError(
            "An unexpected exception happened while transforming the XML")
Esempio n. 6
0
def build_template(request, template_id):
    """View that allows to build the Template.

    Args:
        request:
        template_id:

    Returns:

    """
    if template_id == "new":
        base_template_path = finders.find(join('core_composer_app', 'user', 'xsd', 'new_base_template.xsd'))
        xsd_string = read_file_content(base_template_path)
    else:
        template = template_api.get(template_id)
        xsd_string = template.content

    request.session['newXmlTemplateCompose'] = xsd_string
    request.session['includedTypesCompose'] = []

    # store the current includes/imports
    xsd_tree = XSDTree.build_tree(xsd_string)
    includes = xsd_tree.findall("{}include".format(LXML_SCHEMA_NAMESPACE))
    for el_include in includes:
        if 'schemaLocation' in el_include.attrib:
            request.session['includedTypesCompose'].append(el_include.attrib['schemaLocation'])
    imports = xsd_tree.findall("{}import".format(LXML_SCHEMA_NAMESPACE))
    for el_import in imports:
        if 'schemaLocation' in el_import.attrib:
            request.session['includedTypesCompose'].append(el_import.attrib['schemaLocation'])

    # remove annotations from the tree
    remove_annotations(xsd_tree)
    xsd_string = XSDTree.tostring(xsd_tree)

    # loads XSLT
    xslt_path = finders.find(join('core_composer_app', 'user', 'xsl', 'xsd2html.xsl'))
    # reads XSLT
    xslt_string = read_file_content(xslt_path)
    # transform XML to HTML
    xsd_to_html_string = xsl_transform(xsd_string, xslt_string)

    # 1) Get user defined types
    user_types = type_version_manager_api.get_version_managers_by_user(str(request.user.id))
    # 2) Get buckets
    buckets = bucket_api.get_all()

    # 3) no_buckets_types: list of types that are not assigned to a specific bucket
    no_buckets_types = type_version_manager_api.get_no_buckets_types()

    # 4) Build list of built-in types
    built_in_types = []
    for built_in_type in get_xsd_types():
        built_in_types.append({'current': 'built_in_type', 'title': built_in_type})

    assets = {
        "js": [
            {
                "path": 'core_composer_app/user/js/build_template.js',
                "is_raw": False
            },
            {
                "path": 'core_composer_app/user/js/build_template.raw.js',
                "is_raw": True
            },
            {
                "path": 'core_composer_app/user/js/xpath.js',
                "is_raw": False
            },
            {
                "path": 'core_composer_app/user/js/menus.js',
                "is_raw": False
            },
            {
                "path": 'core_composer_app/user/js/xsd_tree.js',
                "is_raw": False
            },
        ],
        "css": ['core_main_app/common/css/XMLTree.css',
                'core_composer_app/common/css/bucket.css',
                'core_composer_app/user/css/menu.css',
                'core_composer_app/user/css/style.css',
                'core_composer_app/user/css/xsd_tree.css']
    }
    context = {
        'buckets': buckets,
        'built_in_types': built_in_types,
        'no_buckets_types': no_buckets_types,
        'user_types': user_types,
        'xsd_form': xsd_to_html_string,
        'template_id': template_id,
    }

    modals = [
        'core_composer_app/user/builder/menus/sequence.html',
        'core_composer_app/user/builder/menus/element.html',
        'core_composer_app/user/builder/menus/element_root.html',

        'core_composer_app/user/builder/modals/root_type_name.html',
        'core_composer_app/user/builder/modals/element_name.html',
        'core_composer_app/user/builder/modals/insert_element.html',
        'core_composer_app/user/builder/modals/delete_element.html',
        'core_composer_app/user/builder/modals/change_type.html',
        'core_composer_app/user/builder/modals/save_template.html',
        'core_composer_app/user/builder/modals/save_type.html',
        'core_composer_app/user/builder/modals/save_success.html',
        'core_composer_app/user/builder/modals/occurrences.html',
        'core_composer_app/user/builder/modals/errors.html',
    ]

    return render(request,
                  'core_composer_app/user/build_template.html',
                  assets=assets,
                  context=context,
                  modals=modals)