Esempio n. 1
0
    def from_xml(cls, source):
        """Import a page information. @source must be an XML exported
        valid page.
        """
        # page *must* be validated first
        etree = ElementTree.fromstring(source)  # parse XML

        page_info = etree[0].attrib  # page info in first child

        path = page_info.get("path")
        locale = page_info.get("locale", "")
        path, created = Path.objects.get_or_create(path=path, locale=locale)

        # save page data, image is saved later if any
        data = {
            "meta_title": page_info.get("meta_title", ""),
            "meta_description": page_info.get("meta_description", ""),
            "meta_keywords": page_info.get("meta_keywords", ""),
            "search_text": page_info.get("search_text", ""),
            "description": page_info.get("description", ""),
            "template": page_info.get("template"),
            "path": path,
        }
        page = Page(**data)
        page.save()

        # save search image if any
        save_b64_image(page_info.get("search_image"), page_info.get("search_image_name"), page.search_image, save=True)

        # values are from second child to end
        for node in etree[1:]:
            attr = node.attrib
            name, value_type, value = attr["name"], attr["type"], attr["value"]
            if value_type in TYPES_MAP:
                value = TYPES_MAP[value_type]().from_xml(attr)
            page.values.add(Value(name=name, value=value, type=value_type))
        return page
Esempio n. 2
0
 def from_xml(self, data):
     """Reads image stored in a XML file, value must be an base64 enconded
     image and name must be present."""
     if 'value' in data and 'file_name' in data:
         return save_b64_image(data['value'], data['file_name'],
                               self._model().value)