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 and channel are changeable by GET request. # Note that some paths might not exist across all channels, so handle # with care. # Format is (GET parameter, prefixer attribute) for param, attr in (('lang', 'locale'), ('channel', 'channel')): if param 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. setattr(prefixer, attr, '') new_path = prefixer.fix(prefixer.shortened_path) query = dict((smart_str(k), request.GET[k]) for k in request.GET) query.pop(param) 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, new_channel, _ = 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 request.channel = prefixer.channel tower.activate(prefixer.locale)
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 setUp(self): """Set up URL prefixer.""" super(ViewTestCase, self).setUp() urlresolvers.set_url_prefix(urlresolvers.Prefixer(HttpRequest()))