diff options
Diffstat (limited to 'djangorestframework/utils')
| -rw-r--r-- | djangorestframework/utils/__init__.py | 3 | ||||
| -rw-r--r-- | djangorestframework/utils/mediatypes.py | 79 |
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') |
