Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

accept-language:

* add prefix matching http://trac.pythonpaste.org/pythonpaste/ticket/393
* best_match(fallback) should add fallback as it was passed, not how it was specified in the request
  • Loading branch information...
commit c73cd681b7bb97914f873ace39f70bd59df2289c 1 parent bc6db62
@maluke maluke authored
View
4 tests/test_request.py
@@ -47,10 +47,10 @@ def test_language_parsing():
assert "The languages are: ['en-US']" in res
res = app.get('/', headers={'Accept-Language':'da, en-gb;q=0.8, en;q=0.7'})
- assert "languages are: ['da', 'en-gb', 'en', 'en-US']" in res
+ assert "languages are: ['da', 'en-gb', 'en-US']" in res
res = app.get('/', headers={'Accept-Language':'en-gb;q=0.8, da, en;q=0.7'})
- assert "languages are: ['da', 'en-gb', 'en', 'en-US']" in res
+ assert "languages are: ['da', 'en-gb', 'en-US']" in res
def test_mime_parsing():
app = TestApp(simpleapp)
View
6 tests/test_request.txt
@@ -267,7 +267,11 @@ mime types are not supported.
>>> req.accept_language.best_matches('es')
['es']
>>> req.accept_language.best_matches('ES')
- ['es']
+ ['ES']
+
+ >>> req = Request.blank('/', accept_language='en;q=0.5')
+ >>> req.accept_language.best_match(['en-gb'])
+ 'en-gb'
>>> req = Request.blank('/', accept_charset='utf-8;q=0.5')
>>> req.accept_charset.best_match(['iso-8859-1', 'utf-8'])
View
13 webob/acceptparse.py
@@ -56,6 +56,8 @@ def __init__(self, header_name, header_value):
break
else:
self._parsed.append(('iso-8859-1', 1))
+ elif header_name == 'Accept-Language':
+ self._match = self._match_lang
def __repr__(self):
@@ -185,12 +187,11 @@ def best_matches(self, fallback=None):
Return all the matches in order of quality, with fallback (if
given) at the end.
"""
- items = [i for i, q
- in sorted(self._parsed, key=lambda iq: -iq[1])]
+ items = [i for i, q in sorted(self._parsed, key=lambda iq: -iq[1])]
if fallback:
for index, item in enumerate(items):
if self._match(item, fallback):
- items[index+1:] = []
+ items[index:] = [fallback]
break
else:
items.append(fallback)
@@ -199,6 +200,12 @@ def best_matches(self, fallback=None):
def _match(self, mask, item):
return mask == '*' or item.lower() == mask.lower()
+ def _match_lang(self, mask, item):
+ return (mask == '*'
+ or item.lower() == mask.lower()
+ or item.lower().split('-')[0] == mask.lower()
+ )
+
class NilAccept(object):
Please sign in to comment.
Something went wrong with that request. Please try again.