예제 #1
0
    def _retrieve_data(self, request):
        """Return module's data

        Args:
            request:

        Returns:

        """
        data = ""
        self.error = None
        data_xml_entities = XmlEntities()
        if request.method == "GET":
            if "data" in request.GET:
                if len(request.GET["data"]) > 0:
                    data = request.GET["data"]
        elif request.method == "POST":
            if "data" in request.POST:
                url_form = URLForm({"url": request.POST["data"]})
                if url_form.is_valid():
                    data = url_form.data["url"]
                else:
                    self.error = "Enter a valid URL."

        return (data_xml_entities.escape_xml_entities(data)
                if AUTO_ESCAPE_XML_ENTITIES else data)
예제 #2
0
    def render_extension(self, element):
        """Renders an extension

        Args:
            element:

        Returns:

        """
        content = ["", "", ""]

        for child in element.children:
            tmp_content = ["", "", ""]

            if child.tag == "input":
                tmp_content[1] = child.value if child.value is not None else ""
                if AUTO_ESCAPE_XML_ENTITIES:
                    tmp_content[1] = XmlEntities().escape_xml_entities(
                        tmp_content[1])
            elif child.tag == "attribute":
                tmp_content[0] = self.render_attribute(child)
            elif child.tag == "simple_type":
                tmp_content = self.render_simple_type(child)
            elif child.tag == "complex_type":
                tmp_content = self.render_complex_type(child)
            else:
                message = "render_extension: " + child.tag + " not handled"
                self.warnings.append(message)

            content[0] = " ".join([content[0], tmp_content[0]]).strip()
            content[1] += tmp_content[1]
            content[2] += tmp_content[2]

        return content
예제 #3
0
    def render_blob_host_data(data, error):
        """Render blob host data

        Args:
            data:
            error:

        Returns:

        """
        context = {}
        if error is not None:
            context["error"] = error
        else:
            """We have to unescape the string before the graphical render"""
            context["handle"] = XmlEntities.unescape_xml_entities(data)[0]
        """Even if we have unescaped the graphical version of the data
         we have to display the warning message if there are xml predefined entities"""
        data_xml_entities = XmlEntities()
        data_xml_entities.escape_xml_entities(data)
        if (data_xml_entities.number_of_subs_made > 0 or len(
                re.findall(r"((&)|(>)|(<)|(')|("))", data))
                > 0):
            context["xml_entities_warning"] = True

        return AbstractModule.render_template(
            "core_module_blob_host_app/blob_host_display.html", context)
예제 #4
0
    def test_escape_already_escaped_string(self):
        string = "aaa<bbb>ccc&ddd'eee"fff"

        xmlEntities = XmlEntities()

        self.assertTrue(xmlEntities.escape_xml_entities(string) == string)
        self.assertTrue(xmlEntities.unescaped_xml_string == string)
        self.assertTrue(xmlEntities.number_of_subs_made == 0)
예제 #5
0
 def test_text_area_module_render_data_returns_always_empty(self):
     # Arrange
     request = HttpRequest()
     data = "dummy text"
     my_module = TextAreaModule()
     my_module.data_xml_entities = XmlEntities()
     my_module.data = data
     # Act
     result = my_module._render_data(request)
     self.assertEqual(True, result == "")
예제 #6
0
    def test_escape_with_predefined_xml_entities(self):
        string = "aaa<bbb>ccc&ddd'eee\"fff"

        xmlEntities = XmlEntities()

        self.assertTrue(
            xmlEntities.escape_xml_entities(string) ==
            "aaa&lt;bbb&gt;ccc&amp;ddd&apos;eee&quot;fff")
        self.assertTrue(xmlEntities.unescaped_xml_string == string)
        self.assertTrue(xmlEntities.number_of_subs_made == 5)
예제 #7
0
    def test_unescaped_and_already_escaped_predefined_xml_entities(self):
        string = "<&lt;&quot;>&&&lt;"

        xmlEntities = XmlEntities()

        self.assertTrue(
            xmlEntities.escape_xml_entities(string) ==
            "&lt;&lt;&quot;&gt;&amp;&amp;&lt;")
        self.assertTrue(xmlEntities.unescaped_xml_string == string)
        self.assertTrue(xmlEntities.number_of_subs_made == 4)
    def _retrieve_data(self, request):
        """Return module display - GET method

        Args:
            request:

        Returns:

        """
        data = ""
        self.error = None
        data_xml_entities = XmlEntities()
        if request.method == "GET":
            if "data" in request.GET:
                if len(request.GET["data"]) > 0:
                    data = request.GET["data"]
        elif request.method == "POST":
            selected_option = request.POST["blob_form"]
            if selected_option == "url":
                url_form = URLForm(request.POST)
                if url_form.is_valid():
                    data = url_form.data["url"]
                else:
                    self.error = "Enter a valid URL."
            elif selected_option == "file":
                try:
                    form = BLOBHostForm(request.POST, request.FILES)
                    if not form.is_valid():
                        self.error = "No file uploaded."
                        return data

                    # get file from request
                    uploaded_file = request.FILES["file"]
                    # get filename from file
                    filename = uploaded_file.name
                    # get user id from request
                    user_id = str(request.user.id)

                    # create blob
                    blob = Blob(filename=filename, user_id=user_id)
                    # set blob file
                    blob.blob = uploaded_file
                    # save blob
                    blob_api.insert(blob)

                    # get download uri
                    data = get_blob_download_uri(blob, request)
                except:
                    self.error = "An error occurred during the upload."

        return (
            data_xml_entities.escape_xml_entities(data)
            if AUTO_ESCAPE_XML_ENTITIES
            else data
        )
예제 #9
0
    def test_escape_full_predefined_xml_entities(self):
        string = "<<<\"\"'''''''\"\"\">>>"

        xmlEntities = XmlEntities()

        self.assertTrue(
            xmlEntities.escape_xml_entities(string) ==
            "&lt;&lt;&lt;&quot;&quot;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&quot;&quot;&quot;&gt;&gt;&gt;"
        )
        self.assertTrue(xmlEntities.unescaped_xml_string == string)
        self.assertTrue(xmlEntities.number_of_subs_made == 18)
예제 #10
0
    def test_escape_full_predefined_xml_entities(self):
        string = '<<<""\'\'\'\'\'\'\'""">>>'

        xmlEntities = XmlEntities()

        self.assertTrue(
            xmlEntities.escape_xml_entities(string) ==
            '&lt;&lt;&lt;&quot;&quot;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&quot;&quot;&quot;&gt;&gt;&gt;'
        )
        self.assertTrue(xmlEntities.unescaped_xml_string == string)
        self.assertTrue(xmlEntities.number_of_subs_made == 18)
예제 #11
0
    def _retrieve_data(self, request):
        """Retrieve module's data

        Args:
            request:

        Returns:

        """
        data = ""
        if request.method == "GET":
            if "data" in request.GET:
                data = request.GET["data"]
        elif request.method == "POST":
            if "elementList" in request.POST:
                element_list = json.loads(request.POST["elementList"])
                if len(element_list) > 0:
                    element_list_xml = ""
                    for element in element_list:
                        element_list_xml += "<constituent>"
                        element_list_xml += "<element>" + element[
                            "name"] + "</element>"
                        element_list_xml += (
                            "<quantity>" +
                            (XmlEntities().escape_xml_entities(element["qty"])
                             if AUTO_ESCAPE_XML_ENTITIES else element["qty"]) +
                            "</quantity>")
                        element_list_xml += (
                            "<purity>" +
                            (XmlEntities().escape_xml_entities(element["pur"])
                             if AUTO_ESCAPE_XML_ENTITIES else element["pur"]) +
                            "</purity>")
                        element_list_xml += (
                            "<error>" +
                            (XmlEntities().escape_xml_entities(element["err"])
                             if AUTO_ESCAPE_XML_ENTITIES else element["err"]) +
                            "</error>")
                        element_list_xml += "</constituent>"
                    # set the data
                    data = element_list_xml
        return data
예제 #12
0
    def render_restriction(self, element):
        """Renders a restriction

        Args:
            element:

        Returns:

        """
        content = ["", "", ""]
        value = element.value

        for child in element.children:
            tmp_content = ["", "", ""]

            if child.tag == "enumeration":
                tmp_content[1] = value if value is not None else ""
                if AUTO_ESCAPE_XML_ENTITIES:
                    tmp_content[1] = XmlEntities().escape_xml_entities(
                        tmp_content[1])
                value = None  # Avoid to copy the value several times
            elif child.tag == "input":
                tmp_content[1] = child.value if child.value is not None else ""
                if AUTO_ESCAPE_XML_ENTITIES:
                    tmp_content[1] = XmlEntities().escape_xml_entities(
                        tmp_content[1])
            elif child.tag == "simple_type":
                tmp_content = self.render_simple_type(child)
            else:
                message = "render_restriction: " + child.tag + " not handled"
                self.warnings.append(message)

            content[0] = " ".join([content[0], tmp_content[0]]).strip()
            content[1] += tmp_content[1]
            content[2] += tmp_content[2]

        return content
예제 #13
0
    def _retrieve_data(self, request):
        """Retrieve module's data

        Args:
            request:

        Returns:

        """
        data = ""
        self.error = None
        data_xml_entities = XmlEntities()
        if request.method == "GET":
            if "data" in request.GET:
                if len(request.GET["data"]) > 0:
                    data = request.GET["data"]
        elif request.method == "POST":
            try:
                form = BLOBHostForm(request.POST, request.FILES)
                if not form.is_valid():
                    self.error = "No file uploaded."
                    return data

                # get file from request
                uploaded_file = request.FILES["file"]
                # get filename from file
                filename = uploaded_file.name
                # get user id from request
                user_id = str(request.user.id)

                # create blob
                blob = Blob(filename=filename, user_id=user_id)
                # set blob file
                blob.blob = uploaded_file
                # save blob
                blob_api.insert(blob)
                # get download uri
                data = get_blob_download_uri(blob, request)
            except:
                self.error = "An unexpected error occurred."

        return (data_xml_entities.escape_xml_entities(data)
                if AUTO_ESCAPE_XML_ENTITIES else data)
예제 #14
0
    def _retrieve_data(self, request):
        """Retrieve module's data

        Args:
            request:

        Returns:

        """
        data = ""
        self.data_xml_entities = XmlEntities()

        if request.method == "GET":
            if "data" in request.GET:
                data = request.GET["data"]
        elif request.method == "POST":
            if "data" in request.POST:
                data = request.POST["data"]

        data = (self.data_xml_entities.escape_xml_entities(data)
                if AUTO_ESCAPE_XML_ENTITIES else data)

        return data
예제 #15
0
    def render_attribute(self, element):
        """Renders an attribute

        Args:
            element:

        Returns:

        """
        attr_key = element.options["name"]
        attr_list = []
        children = []

        for child in element.children:
            if child.tag == "elem-iter":
                children += child.children
            else:
                message = "render_attribute (iteration): " + child.tag + " not handled"
                self.warnings.append(message)

        for child in children:
            attr_value = ""

            if child.tag == "simple_type":
                content = self.render_simple_type(child)
                attr_value = content[1]
            elif child.tag == "input":
                attr_value = child.value if child.value is not None else ""
                if AUTO_ESCAPE_XML_ENTITIES:
                    attr_value = XmlEntities().escape_xml_entities(attr_value)
            elif child.tag == "module":
                attr_value = self.render_module(child)[1]
            else:
                message = "render_attribute: " + child.tag + " not handled"
                self.warnings.append(message)

            # namespaces
            if "xmlns" in element.options and element.options[
                    "xmlns"] is not None:
                # check that element isn't declaring the same namespace xmlns=""
                parent = self._get_parent_element(element)
                xmlns = ""
                if parent is not None:
                    if ("xmlns" in parent.options
                            and parent.options["xmlns"] is not None
                            and parent.options["xmlns"]
                            == element.options["xmlns"]):
                        xmlns = ""
                    else:  # parent element is in a different namespace
                        if element.options["xmlns"] != "":
                            # TODO: test ns0 not taken and increment if needed
                            ns_prefix = (element.options["ns_prefix"]
                                         if element.options["ns_prefix"]
                                         is not None else "ns0")
                            if ns_prefix != "":
                                xmlns = ' xmlns{0}="{1}"'.format(
                                    ":" + ns_prefix, element.options["xmlns"])
                                attr_key = "{0}:{1}".format(
                                    ns_prefix, attr_key)
                            else:
                                xmlns = ' xmlns="{0}"'.format(
                                    element.options["xmlns"])
                        else:
                            xmlns = ""

                if isinstance(attr_value, numbers.Number):
                    attr_value = str(attr_value)

                attr_list.append(xmlns + " " + attr_key + "='" + attr_value +
                                 "'")

                # TODO: check that sibling attributes are not declaring the
                #  same namespaces
            else:
                attr_list.append(attr_key + '="' + attr_value + '"')

        return " ".join(attr_list)
예제 #16
0
    def render_element(self, element):
        """Renders an element

        Args:
            element:

        Returns:

        """
        xml_string = ""
        children = {}
        child_keys = []
        children_number = 0

        for child in element.children:
            if child.tag == "elem-iter":
                children[child.pk] = child.children
                child_keys.append(child.pk)

                if len(child.children) > 0:
                    children_number += 1
            else:
                message = "render_element (iteration): " + child.tag + " not handled"
                self.warnings.append(message)

        element_name = element.options["name"]

        for child_key in child_keys:
            for child in children[child_key]:
                content = ["", "", ""]

                # add XML Schema instance prefix if root
                if self.isRoot:
                    xsi = ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
                    content[0] += xsi
                    self.isRoot = False

                if child.tag == "complex_type":
                    tmp_content = self.render_complex_type(child)
                    content[0] += tmp_content[0]
                    content[1] += tmp_content[1]
                    content[2] += tmp_content[2]
                elif child.tag == "input":
                    tmp_content = child.value if child.value is not None else ""
                    content[1] += (
                        XmlEntities().escape_xml_entities(tmp_content)
                        if AUTO_ESCAPE_XML_ENTITIES else tmp_content)
                elif child.tag == "simple_type":
                    tmp_content = self.render_simple_type(child)
                    content[0] += tmp_content[0]
                    content[1] += tmp_content[1]
                    content[2] += tmp_content[2]
                elif child.tag == "module":
                    tmp_content = self.render_module(child)

                    if child.options["multiple"]:
                        content[2] += tmp_content[1]
                    else:
                        content[1] += tmp_content[1]
                else:
                    message = "render_element: " + child.tag + " not handled"
                    self.warnings.append(message)

                # namespaces
                parent = self._get_parent_element(element)
                if parent is not None:
                    if ("xmlns" in element.options
                            and element.options["xmlns"] is not None):
                        if ("xmlns" in parent.options
                                and element.options["xmlns"] !=
                                parent.options["xmlns"]):
                            xmlns = ' xmlns="{}"'.format(
                                element.options["xmlns"])
                            content[0] += xmlns
                else:
                    if ("xmlns" in element.options
                            and element.options["xmlns"] is not None):
                        xmlns = ' xmlns="{}"'.format(element.options["xmlns"])
                        content[0] += xmlns

                # content[2] has the value returned by a module (the entire
                # tag, when multiple is True)
                if content[2] != "":
                    if content[1] != "":
                        raise RendererError(
                            "ERROR: More values than expected were returned "
                            "(Module multiple).")
                    xml_string += content[2]
                else:
                    xml_string += self._render_xml(element_name, content[0],
                                                   content[1])

        return xml_string