aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/utils
diff options
context:
space:
mode:
authorTom Christie2011-05-24 13:29:30 +0100
committerTom Christie2011-05-24 13:29:30 +0100
commitce6e5fdc01b6d820f317bc1d8edc4ede4a946516 (patch)
tree9d3b9dbd2f263ba1d5f99cd3fdce40ae9f54f511 /djangorestframework/utils
parenteafda8550800a98aa37571df0cef78e32521a89b (diff)
downloaddjango-rest-framework-ce6e5fdc01b6d820f317bc1d8edc4ede4a946516.tar.bz2
Renderers can now cope with parameterised args. ResponseMixin gets cleaned up & added Renderer.can_handle_response(), mirroring Parsers.can_handle_request()
Diffstat (limited to 'djangorestframework/utils')
-rw-r--r--djangorestframework/utils/__init__.py3
-rw-r--r--djangorestframework/utils/mediatypes.py79
2 files changed, 51 insertions, 31 deletions
diff --git a/djangorestframework/utils/__init__.py b/djangorestframework/utils/__init__.py
index 67870001..99f9724c 100644
--- a/djangorestframework/utils/__init__.py
+++ b/djangorestframework/utils/__init__.py
@@ -13,6 +13,9 @@ import xml.etree.ElementTree as ET
# """Adds the ADMIN_MEDIA_PREFIX to the request context."""
# return {'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX}
+from mediatypes import media_type_matches, is_form_media_type
+from mediatypes import add_media_type_param, get_media_type_params, order_by_precedence
+
MSIE_USER_AGENT_REGEX = re.compile(r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )')
def as_tuple(obj):
diff --git a/djangorestframework/utils/mediatypes.py b/djangorestframework/utils/mediatypes.py
index 190cdc2d..ae734e62 100644
--- a/djangorestframework/utils/mediatypes.py
+++ b/djangorestframework/utils/mediatypes.py
@@ -51,6 +51,22 @@ def get_media_type_params(media_type):
return _MediaType(media_type).params
+def order_by_precedence(media_type_lst):
+ """
+ Returns a list of lists of media type strings, ordered by precedence.
+ Precedence is determined by how specific a media type is:
+
+ 3. 'type/subtype; param=val'
+ 2. 'type/subtype'
+ 1. 'type/*'
+ 0. '*/*'
+ """
+ ret = [[],[],[],[]]
+ for media_type in media_type_lst:
+ precedence = _MediaType(media_type).precedence
+ ret[3-precedence].append(media_type)
+ return ret
+
class _MediaType(object):
def __init__(self, media_type_str):
@@ -61,53 +77,54 @@ class _MediaType(object):
self.main_type, sep, self.sub_type = self.full_type.partition('/')
def match(self, other):
- """Return true if this MediaType satisfies the constraint of the given MediaType."""
- for key in other.params.keys():
- if key != 'q' and other.params[key] != self.params.get(key, None):
+ """Return true if this MediaType satisfies the given MediaType."""
+ for key in self.params.keys():
+ if key != 'q' and other.params.get(key, None) != self.params.get(key, None):
return False
- if other.sub_type != '*' and other.sub_type != self.sub_type:
+ if self.sub_type != '*' and other.sub_type != '*' and other.sub_type != self.sub_type:
return False
- if other.main_type != '*' and other.main_type != self.main_type:
+ if self.main_type != '*' and other.main_type != '*' and other.main_type != self.main_type:
return False
return True
+ @property
def precedence(self):
"""
- Return a precedence level for the media type given how specific it is.
+ Return a precedence level from 0-3 for the media type given how specific it is.
"""
if self.main_type == '*':
- return 1
+ return 0
elif self.sub_type == '*':
- return 2
+ return 1
elif not self.params or self.params.keys() == ['q']:
- return 3
- return 4
-
- def quality(self):
- """
- Return a quality level for the media type.
- """
- try:
- return Decimal(self.params.get('q', '1.0'))
- except:
- return Decimal(0)
-
- def score(self):
- """
- Return an overall score for a given media type given it's quality and precedence.
- """
- # NB. quality values should only have up to 3 decimal points
- # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9
- return self.quality * 10000 + self.precedence
+ return 2
+ return 3
+
+ #def quality(self):
+ # """
+ # Return a quality level for the media type.
+ # """
+ # try:
+ # return Decimal(self.params.get('q', '1.0'))
+ # except:
+ # return Decimal(0)
+
+ #def score(self):
+ # """
+ # Return an overall score for a given media type given it's quality and precedence.
+ # """
+ # # NB. quality values should only have up to 3 decimal points
+ # # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9
+ # return self.quality * 10000 + self.precedence
- def as_tuple(self):
- return (self.main_type, self.sub_type, self.params)
+ #def as_tuple(self):
+ # return (self.main_type, self.sub_type, self.params)
- def __repr__(self):
- return "<MediaType %s>" % (self.as_tuple(),)
+ #def __repr__(self):
+ # return "<MediaType %s>" % (self.as_tuple(),)
def __str__(self):
return unicode(self).encode('utf-8')