def default_prefixer(sender, **kwargs): """Make sure each test starts with a default URL prefixer.""" request = http.HttpRequest() request.META['SCRIPT_NAME'] = '' prefixer = urlresolvers.Prefixer(request) prefixer.locale = settings.LANGUAGE_CODE urlresolvers.set_url_prefix(prefixer)
def process_request(self, request): prefixer = urlresolvers.Prefixer(request) urlresolvers.set_url_prefix(prefixer) full_path = prefixer.fix(prefixer.shortened_path) # Lang is changeable by GET request. if 'lang' in request.GET: # Blank out the prefixer attribute so that we can set a new # one. Remove the parameter from the query params so we don't # have an infinite loop. prefixer.locale = '' new_path = prefixer.fix(prefixer.shortened_path) query = dict((smart_str(k), request.GET[k]) for k in request.GET) query.pop('lang') return HttpResponsePermanentRedirect(urlparams(new_path, **query)) if full_path != request.path: query_string = request.META.get('QUERY_STRING', '') full_path = urllib.quote(full_path.encode('utf-8')) if query_string: full_path = '%s?%s' % (full_path, query_string) response = HttpResponsePermanentRedirect(full_path) # Vary on Accept-Language if we changed the locale old_locale = prefixer.locale new_locale, _ = prefixer.split_path(full_path) if old_locale != new_locale: response['Vary'] = 'Accept-Language' return response request.path_info = '/' + prefixer.shortened_path request.locale = prefixer.locale tower.activate(prefixer.locale)
def test_locale_in_get(self): request = Mock() request.path_info = '/' request.GET = dict(lang='en-US') p = urlresolvers.Prefixer(request) eq_(p.get_language(), 'en-US')
def test_fake_locale(self): r = self.factory.get('/zf/beta') p = urlresolvers.Prefixer(r) eq_(p.locale, '')
def test_almost_locale(self): request = Mock() request.path_info = '/en/' p = urlresolvers.Prefixer(request) eq_(p.locale, 'en-US')
def test_no_locale(self): request = Mock() request.path_info = '/beta' p = urlresolvers.Prefixer(request) eq_(p.locale, '')
def setUp(self): """Set up URL prefixer.""" super(ViewTestCase, self).setUp() urlresolvers.set_url_prefix(urlresolvers.Prefixer(HttpRequest()))