aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/parsers.py
diff options
context:
space:
mode:
authorTom Christie2011-12-11 10:42:37 -0800
committerTom Christie2011-12-11 10:42:37 -0800
commite2f3153b138a728c2dacb16c90f7a46a17f429c4 (patch)
treed95d6755f787c8ab3d8c555315d514c067121bf9 /djangorestframework/parsers.py
parent4f42303035c094a1d0c6128f0ccce1abfa6b72f4 (diff)
parente84bf2140c33b45d1d9d8325eeaf62a97c46495e (diff)
downloaddjango-rest-framework-e2f3153b138a728c2dacb16c90f7a46a17f429c4.tar.bz2
Merge pull request #90 from jakul/xmlparser6
Lovely stuff!
Diffstat (limited to 'djangorestframework/parsers.py')
-rw-r--r--djangorestframework/parsers.py58
1 files changed, 56 insertions, 2 deletions
diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py
index 2ff64bd3..09e7f4d0 100644
--- a/djangorestframework/parsers.py
+++ b/djangorestframework/parsers.py
@@ -19,6 +19,9 @@ from djangorestframework import status
from djangorestframework.compat import yaml
from djangorestframework.response import ErrorResponse
from djangorestframework.utils.mediatypes import media_type_matches
+from xml.etree import ElementTree as ET
+import datetime
+import decimal
__all__ = (
@@ -28,6 +31,7 @@ __all__ = (
'FormParser',
'MultiPartParser',
'YAMLParser',
+ 'XMLParser'
)
@@ -167,10 +171,60 @@ class MultiPartParser(BaseParser):
raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
{'detail': 'multipart parse error - %s' % unicode(exc)})
return django_parser.parse()
+
+
+class XMLParser(BaseParser):
+ """
+ XML parser.
+ """
+
+ media_type = 'application/xml'
+
+ def parse(self, stream):
+ """
+ Returns a 2-tuple of `(data, files)`.
+
+ `data` will simply be a string representing the body of the request.
+ `files` will always be `None`.
+ """
+ data = {}
+ tree = ET.parse(stream)
+ for child in tree.getroot().getchildren():
+ data[child.tag] = self._type_convert(child.text)
+
+ return (data, None)
+
+ def _type_convert(self, value):
+ """
+ Converts the value returned by the XMl parse into the equivalent
+ Python type
+ """
+ if value is None:
+ return value
+
+ try:
+ return datetime.datetime.strptime(value,'%Y-%m-%d %H:%M:%S')
+ except ValueError:
+ pass
+
+ try:
+ return int(value)
+ except ValueError:
+ pass
+
+ try:
+ return decimal.Decimal(value)
+ except decimal.InvalidOperation:
+ pass
+
+ return value
+
DEFAULT_PARSERS = ( JSONParser,
FormParser,
- MultiPartParser )
+ MultiPartParser,
+ XMLParser
+ )
if YAMLParser:
- DEFAULT_PARSERS += ( YAMLParser, )
+ DEFAULT_PARSERS += ( YAMLParser, ) \ No newline at end of file