aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/utils
diff options
context:
space:
mode:
Diffstat (limited to 'djangorestframework/utils')
-rw-r--r--djangorestframework/utils/__init__.py14
-rw-r--r--djangorestframework/utils/mediatypes.py29
2 files changed, 37 insertions, 6 deletions
diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py
index 9dc769be..67870001 100644
--- a/djangorestframework/utils/__init__.py
+++ b/djangorestframework/utils/__init__.py
@@ -16,7 +16,15 @@ import xml.etree.ElementTree as ET
MSIE_USER_AGENT_REGEX = re.compile(r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )')
def as_tuple(obj):
- """Given obj return a tuple"""
+ """
+ Given an object which may be a list/tuple, another object, or None,
+ return that object in list form.
+
+ IE:
+ If the object is already a list/tuple just return it.
+ If the object is not None, return it in a list with a single element.
+ If the object is None return an empty list.
+ """
if obj is None:
return ()
elif isinstance(obj, list):
@@ -27,7 +35,9 @@ def as_tuple(obj):
def url_resolves(url):
- """Return True if the given URL is mapped to a view in the urlconf, False otherwise."""
+ """
+ Return True if the given URL is mapped to a view in the urlconf, False otherwise.
+ """
try:
resolve(url)
except:
diff --git a/djangorestframework/utils/mediatypes.py b/djangorestframework/utils/mediatypes.py
index 3bf914e4..62a5e6f3 100644
--- a/djangorestframework/utils/mediatypes.py
+++ b/djangorestframework/utils/mediatypes.py
@@ -15,7 +15,7 @@ def media_type_matches(lhs, rhs):
Valid media type strings include:
- 'application/json indent=4'
+ 'application/json; indent=4'
'application/json'
'text/*'
'*/*'
@@ -33,10 +33,28 @@ def is_form_media_type(media_type):
media_type = _MediaType(media_type)
return media_type.full_type == 'application/x-www-form-urlencoded' or \
media_type.full_type == 'multipart/form-data'
-
-
+
+
+def add_media_type_param(media_type, key, val):
+ """
+ Add a key, value parameter to a media type string, and return the new media type string.
+ """
+ media_type = _MediaType(media_type)
+ media_type.params[key] = val
+ return str(media_type)
+
+def get_media_type_params(media_type):
+ """
+ Return a dictionary of the parameters on the given media type.
+ """
+ return _MediaType(media_type).params
+
+
+
class _MediaType(object):
def __init__(self, media_type_str):
+ if media_type_str is None:
+ media_type_str = ''
self.orig = media_type_str
self.full_type, self.params = parse_header(media_type_str)
self.main_type, sep, self.sub_type = self.full_type.partition('/')
@@ -94,5 +112,8 @@ class _MediaType(object):
return unicode(self).encode('utf-8')
def __unicode__(self):
- return self.orig
+ ret = "%s/%s" % (self.main_type, self.sub_type)
+ for key, val in self.params.items():
+ ret += "; %s=%s" % (key, val)
+ return ret