Extend Subsonic XML renderer to allow explicit XML child tags
Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2695>
This commit is contained in:
parent
d227490f5b
commit
a5ee48818e
|
@ -6,6 +6,10 @@ from rest_framework import renderers
|
|||
import funkwhale_api
|
||||
|
||||
|
||||
class TagValue(str):
|
||||
"""Use this for string values that must be rendered as tags instead of attributes in XML."""
|
||||
|
||||
|
||||
# from https://stackoverflow.com/a/8915039
|
||||
# because I want to avoid a lxml dependency just for outputting cdata properly
|
||||
# in a RSS feed
|
||||
|
@ -83,6 +87,10 @@ def dict_to_xml_tree(root_tag, d, parent=None):
|
|||
el = ET.Element(key)
|
||||
el.text = str(obj)
|
||||
root.append(el)
|
||||
elif isinstance(value, TagValue):
|
||||
el = ET.Element(key)
|
||||
el.text = str(value)
|
||||
root.append(el)
|
||||
else:
|
||||
if key == "value":
|
||||
root.text = str(value)
|
||||
|
|
|
@ -79,9 +79,10 @@ def test_xml_renderer_dict_to_xml():
|
|||
"hello": "world",
|
||||
"item": [{"this": 1, "value": "text"}, {"some": "node"}],
|
||||
"list": [1, 2],
|
||||
"some-tag": renderers.TagValue("foo"),
|
||||
}
|
||||
expected = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
<key hello="world"><item this="1">text</item><item some="node" /><list>1</list><list>2</list></key>"""
|
||||
<key hello="world"><item this="1">text</item><item some="node" /><list>1</list><list>2</list><some-tag>foo</some-tag></key>""" # noqa
|
||||
result = renderers.dict_to_xml_tree("key", payload)
|
||||
exp = ET.fromstring(expected)
|
||||
assert ET.tostring(result) == ET.tostring(exp)
|
||||
|
|
Loading…
Reference in New Issue