From dba35ef679ceee8de8432674e60f203b55d435ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20L=C3=B3pez=20Padial?= Date: Mon, 3 Jun 2019 14:46:02 +0200 Subject: [PATCH 1/4] Handle/parse XML attributes: https://github.com/jpadilla/django-rest-framework-xml/issues/16 Based on this issue: https://github.com/jpadilla/django-rest-framework-xml/issues/16 Changes to handle/parse XML attributes. --- rest_framework_xml/parsers.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rest_framework_xml/parsers.py b/rest_framework_xml/parsers.py index 5454356..1682752 100644 --- a/rest_framework_xml/parsers.py +++ b/rest_framework_xml/parsers.py @@ -55,7 +55,13 @@ def _xml_convert(self, element): else: data = {} for child in children: - data[child.tag] = self._xml_convert(child) + if child.attrib: + data[child.tag] = { + 'value': self._xml_convert(child), + 'attributes': child.attrib + } + else: + data[child.tag] = self._xml_convert(child) return data From 5870ed54871e25a699cd883df2d89b5164eb9d2d Mon Sep 17 00:00:00 2001 From: Dario Lopez Padial Date: Tue, 4 Jun 2019 10:17:35 +0200 Subject: [PATCH 2/4] adding some tests for 'attribute parser': https://github.com/jpadilla/django-rest-framework-xml/pull/39#issuecomment-498361606 --- requirements-test.txt | 1 + tests/test_parsers.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/requirements-test.txt b/requirements-test.txt index c32ff38..fa15ff1 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -4,3 +4,4 @@ pytest-django==3.1.2 pytest==3.1.3 pytest-cov==2.5.1 flake8==3.3.0 +defusedxml>=0.3 diff --git a/tests/test_parsers.py b/tests/test_parsers.py index 38618a8..7922ca2 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -52,6 +52,32 @@ def setUp(self): } ] } + self._attribute_data_input = StringIO( + '' + '' + '121.0' + 'dasd' + '' + '2011-12-25 12:45:00' + '' + ) + self._attribute_data = { + 'field_a': { + 'value': 121, + 'attributes': { + 'name': 'attribute_a' + } + }, + 'field_b': { + 'value': 'dasd', + 'attributes': { + 'id': '0001', + 'name': 'attribute_b' + } + }, + 'field_c': None, + 'field_d': datetime.datetime(2011, 12, 25, 12, 45, 00) + } @skipUnless(etree, 'defusedxml not installed') def test_parse(self): @@ -64,3 +90,9 @@ def test_complex_data_parse(self): parser = XMLParser() data = parser.parse(self._complex_data_input) self.assertEqual(data, self._complex_data) + + @skipUnless(etree, 'defusedxml not installed') + def test_complex_data_parse(self): + parser = XMLParser() + data = parser.parse(self._attribute_data_input) + self.assertEqual(data, self._attribute_data) From a4fcdac777f02f02fa04c97c17b4c1046d899402 Mon Sep 17 00:00:00 2001 From: Dario Lopez Padial Date: Tue, 4 Jun 2019 10:43:03 +0200 Subject: [PATCH 3/4] fixing flake8 --- rest_framework_xml/parsers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework_xml/parsers.py b/rest_framework_xml/parsers.py index 1682752..2951e2a 100644 --- a/rest_framework_xml/parsers.py +++ b/rest_framework_xml/parsers.py @@ -47,7 +47,7 @@ def _xml_convert(self, element): if len(children) == 0: return self._type_convert(element.text) else: - # if the fist child tag is list-item means all children are list-item + # if first child tag is list-item means all children are list-item if children[0].tag == "list-item": data = [] for child in children: From 232cf0c8b56192ccc020184aa81cafd5630383a8 Mon Sep 17 00:00:00 2001 From: Dario Lopez Padial Date: Tue, 4 Jun 2019 11:35:45 +0200 Subject: [PATCH 4/4] fixing travis ci for python2.7 --- requirements-test.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements-test.txt b/requirements-test.txt index fa15ff1..c32ff38 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -4,4 +4,3 @@ pytest-django==3.1.2 pytest==3.1.3 pytest-cov==2.5.1 flake8==3.3.0 -defusedxml>=0.3