-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from wmo-raf/dev
Refactor alert xml renderer
- Loading branch information
Showing
2 changed files
with
24 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters