Esempio n. 1
0
    def process_request(self, request):
        prefixer = Prefixer(request)
        set_url_prefixer(prefixer)
        full_path = prefixer.fix(prefixer.shortened_path)

        if 'lang' in request.GET:
            # 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')
            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, _ = 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)
Esempio n. 2
0
    def process_request(self, request):
        prefixer = Prefixer(request)
        set_url_prefixer(prefixer)
        full_path = prefixer.fix(prefixer.shortened_path)

        if 'lang' in request.GET:
            # 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')
            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, _ = 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)
Esempio n. 3
0
    def process_request(self, request):
        prefixer = Prefixer(request)
        set_url_prefixer(prefixer)
        full_path = prefixer.fix(prefixer.shortened_path)

        if "lang" in request.GET:
            # 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")
            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
        tower.activate(prefixer.locale)
Esempio n. 4
0
 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
Esempio n. 5
0
 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
Esempio n. 6
0
    def process_request(self, request):
        prefixer = Prefixer(request)
        set_url_prefixer(prefixer)
        full_path = prefixer.fix(prefixer.shortened_path)

        if 'lang' in request.GET:
            # 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
        tower.activate(prefixer.locale)
Esempio n. 7
0
 def process_exception(self, request, exception):
     set_url_prefixer(None)
Esempio n. 8
0
 def process_exception(self, request, exception):
     set_url_prefixer(None)