Skip to content

Commit

Permalink
Merge pull request #68 from wmo-raf/dev
Browse files Browse the repository at this point in the history
Refactor alert xml renderer
  • Loading branch information
erick-otenyo authored Jul 17, 2024
2 parents 8e714cd + a832b67 commit 8cc758f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
67 changes: 23 additions & 44 deletions capeditor/renderers.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,62 @@
import datetime
import xml.etree.ElementTree as ET
from io import StringIO
from xml.etree.ElementTree import Element, ElementTree

import six
from rest_framework_xml.renderers import XMLRenderer


class CapXMLRenderer(XMLRenderer):
format = 'xml'
root_tag_name = 'alert'

def _recursive_serialize_dict(self, value, xml):
def _recursive_serialize_dict(self, value, parent):
for key, value in value.items():
if isinstance(value, list):
# handle MULTIPOLYGONS
if key == 'polygons':
for polygon_coords in value:
element = Element("polygon")
element = ET.SubElement(parent, "cap:polygon")
self._recursive_serialize(polygon_coords, element)
xml.append(element)
else:
for item in value:
element = Element(key)
element = ET.SubElement(parent, f"cap:{key}")
self._recursive_serialize(item, element)
xml.append(element)

else:
element = Element(key)
element = ET.SubElement(parent, f"cap:{key}")
self._recursive_serialize(value, element)
xml.append(element)

def _recursive_serialize(self, value, xml):
def _recursive_serialize(self, value, element):
if isinstance(value, dict):
self._recursive_serialize_dict(value, xml)
self._recursive_serialize_dict(value, element)
elif isinstance(value, datetime.datetime):
xml.text = value.isoformat()
elif isinstance(value, (six.integer_types, float)):
xml.text = six.text_type(value)
elif isinstance(value, six.text_type):
xml.text = value
element.text = value.isoformat()
elif isinstance(value, (int, float)):
element.text = str(value)
elif isinstance(value, str):
element.text = value
elif value is None:
xml.set('nil', 'true')
element.set('nil', 'true')
else:
xml.text = six.text_type(value)
element.text = str(value)

def render(self, data, accepted_media_type=None, renderer_context=None):
"""
Render `data` into XML.
"""

tree = ElementTree()
tree = ET.ElementTree()
stream = StringIO()

if data is None:
return ''
elif isinstance(data, list):
root = Element('feed')

for item in data:
element = Element('alert')
element.set('xmlns', 'urn:oasis:names:tc:emergency:cap:1.2')
self._recursive_serialize(item, element)

root.append(element)

tree._setroot(root)
tree.write(stream, encoding="unicode", xml_declaration=True)
if not isinstance(data, dict):
raise ValueError('Data should be a dictionary')

return stream.getvalue()
root = ET.Element('cap:alert', attrib={'xmlns:cap': 'urn:oasis:names:tc:emergency:cap:1.2'})

elif isinstance(data, dict):
self._recursive_serialize_dict(data, root)
tree._setroot(root)
tree.write(stream, encoding="unicode", xml_declaration=True)

root = Element('alert')
root.set('xmlns', 'urn:oasis:names:tc:emergency:cap:1.2')

self._recursive_serialize_dict(data, root)

tree._setroot(root)
tree.write(stream, encoding="unicode", xml_declaration=True)

return stream.getvalue()
else:
return data
return stream.getvalue()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = capeditor
version = 0.6.0
version = 0.6.1
description = Wagtail based CAP composer
long_description = file:README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit 8cc758f

Please sign in to comment.