def process_response(self, request, response): """Unset the thread-local var we set during `process_request`.""" # This makes mistaken tests (that should use LocalizingClient but # use Client instead) fail loudly and reliably. Otherwise, the set # prefixer bleeds from one test to the next, making tests # order-dependent and causing hard-to-track failures. set_url_prefixer(None) return response
def process_request(self, request): try: urlname = resolve(request.path_info).url_name except Resolver404: urlname = None if settings.OIDC_ENABLE and urlname in settings.OIDC_EXEMPT_URLS: translation.activate(settings.LANGUAGE_CODE) return prefixer = Prefixer(request) set_url_prefixer(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if request.GET.get('lang', '') in settings.SUMO_LANGUAGES: # Blank out the locale so that we can set a new one. Remove lang # 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), v) for k, v in request.GET.iteritems() if k != 'lang') # 'lang' is only used on the language selection page. If this is # present it is safe to set language preference for the current # user. if request.user.is_anonymous(): cookie = settings.LANGUAGE_COOKIE_NAME request.session[cookie] = request.GET['lang'] return HttpResponseRedirect(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 = HttpResponseRedirect(full_path) # Vary on Accept-Language if we changed the locale old_locale = prefixer.locale new_locale, _ = split_path(full_path) if old_locale != new_locale: response['Vary'] = 'Accept-Language' return response request.path_info = '/' + prefixer.shortened_path request.LANGUAGE_CODE = prefixer.locale translation.activate(prefixer.locale)
def process_request(self, request): prefixer = Prefixer(request) set_url_prefixer(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if request.GET.get('lang', '') in settings.SUMO_LANGUAGES: # Blank out the locale so that we can set a new one. Remove lang # 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), v) for k, v in request.GET.iteritems() if k != 'lang') # 'lang' is only used on the language selection page. If this is # present it is safe to set language preference for the current # user. if request.user.is_anonymous(): cookie = settings.LANGUAGE_COOKIE_NAME request.session[cookie] = request.GET['lang'] return HttpResponseRedirect(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 = HttpResponseRedirect(full_path) # Vary on Accept-Language if we changed the locale old_locale = prefixer.locale new_locale, _ = split_path(full_path) if old_locale != new_locale: response['Vary'] = 'Accept-Language' return response request.path_info = '/' + prefixer.shortened_path request.LANGUAGE_CODE = prefixer.locale translation.activate(prefixer.locale)
def process_exception(self, request, exception): set_url_prefixer(None)