Example #1
0
    def _decorated_view(request, *args, **kwargs):
        # Get locale from Facebook's `signed_request`
        signed_request = utils.unwrap_signed_request(request)
        try:
            facebook_locale = signed_request['user']['locale']
        except KeyError:
            pass
        else:
            # If user's locale isn't supported, get the next best one.
            # Defaults to en-US if no locale in same language as the
            # user's is found.
            best_locale = utils.get_best_locale(facebook_locale)

            prefix = urlresolvers.get_url_prefix()

            # Compare locales in lowercase just in case. Heh.
            # If we aren't using the best locale, redirect to it
            if prefix.locale.lower() != best_locale.lower():
                prefix.locale = best_locale
                locale_url = prefix.fix(request.path_info)
                query_string = urllib.urlencode(request.GET)
                final_url = ('?'.join([locale_url, query_string])
                             if query_string else locale_url)
                return redirect(final_url)

        return view_fn(request, *args, **kwargs)
Example #2
0
    def _pre_setup(self):
        # Add the models to the db.
        self._original_installed_apps = list(settings.INSTALLED_APPS)
        for app in self.apps:
            settings.INSTALLED_APPS.append(app)
        loading.cache.loaded = False
        call_command('syncdb', interactive=False, verbosity=0)
        call_command('update_badges', verbosity=0)
        badger.autodiscover()

        if get_url_prefix:
            # If we're in funfactoryland, make sure a locale prefix is
            # set for urlresolvers
            locale = 'en-US'
            self.old_prefix = get_url_prefix()
            self.old_locale = get_language()
            rf = RequestFactory()
            set_url_prefix(Prefixer(rf.get('/%s/' % (locale, ))))
            activate(locale)

        # Create a default user for tests
        self.user_1 = self._get_user(username="******",
                                     email="*****@*****.**",
                                     password="******")

        # Call the original method that does the fixtures etc.
        super(test.TestCase, self)._pre_setup()
Example #3
0
    def _pre_setup(self):
        # Add the models to the db.
        self._original_installed_apps = list(settings.INSTALLED_APPS)
        for app in self.apps:
            settings.INSTALLED_APPS.append(app)
        loading.cache.loaded = False
        call_command('syncdb', interactive=False, verbosity=0)
        call_command('update_badges', verbosity=0)
        badger.autodiscover()

        if get_url_prefix:
            # If we're in funfactoryland, make sure a locale prefix is 
            # set for urlresolvers
            locale = 'en-US'
            self.old_prefix = get_url_prefix()
            self.old_locale = get_language()
            rf = RequestFactory()
            set_url_prefix(Prefixer(rf.get('/%s/' % (locale,))))
            activate(locale)

        # Create a default user for tests
        self.user_1 = self._get_user(username="******",
                                     email="*****@*****.**",
                                     password="******")

        # Call the original method that does the fixtures etc.
        super(test.TestCase, self)._pre_setup()
Example #4
0
 def activate(self, locale):
     """Context manager that temporarily activates a locale."""
     old_prefix = get_url_prefix()
     old_locale = get_language()
     rf = test_utils.RequestFactory()
     set_url_prefix(Prefixer(rf.get('/%s/' % (locale, ))))
     activate(locale)
     yield
     set_url_prefix(old_prefix)
     activate(old_locale)
Example #5
0
 def activate(self, locale):
     """Context manager that temporarily activates a locale."""
     old_prefix = get_url_prefix()
     old_locale = get_language()
     rf = test_utils.RequestFactory()
     set_url_prefix(Prefixer(rf.get('/%s/' % (locale,))))
     activate(locale)
     yield
     set_url_prefix(old_prefix)
     activate(old_locale)
Example #6
0
    def _pre_setup(self):
        loading.cache.loaded = False
        call_command('update_badges', verbosity=0)
        badger.autodiscover()

        if get_url_prefix:
            # If we're in funfactoryland, make sure a locale prefix is
            # set for urlresolvers
            locale = 'en-US'
            self.old_prefix = get_url_prefix()
            self.old_locale = get_language()
            rf = RequestFactory()
            set_url_prefix(Prefixer(rf.get('/%s/' % (locale,))))

        # Create a default user for tests
        self.user_1 = self._get_user(username="******",
                                     email="*****@*****.**",
                                     password="******")

        # Call the original method that does the fixtures etc.
        super(test.TestCase, self)._pre_setup()
Example #7
0
    def _pre_setup(self):
        loading.cache.loaded = False
        call_command('update_badges', verbosity=0)
        badger.autodiscover()

        if get_url_prefix:
            # If we're in funfactoryland, make sure a locale prefix is
            # set for urlresolvers
            locale = 'en-US'
            self.old_prefix = get_url_prefix()
            self.old_locale = get_language()
            rf = RequestFactory()
            set_url_prefix(Prefixer(rf.get('/%s/' % (locale,))))

        # Create a default user for tests
        self.user_1 = self._get_user(username="******",
                                     email="*****@*****.**",
                                     password="******")

        # Call the original method that does the fixtures etc.
        super(test.TestCase, self)._pre_setup()
Example #8
0
def well_known_browserid(request):
    # HACK: The following code is terrible. But, I really wanted some
    # locale-free URLs, in order to force a locale redirect later.
    op = get_url_prefix()
    np = Prefixer(request)
    np.locale = 'XXX'
    set_url_prefix(np)
    authentication_url=reverse('whuru.browserid.sign_in').replace('/XXX','')
    provisioning_url=reverse('whuru.browserid.provision').replace('/XXX','')
    set_url_prefix(op)

    settings_root = os.path.dirname(whuru.settings.__file__)
    public_key_fn = os.path.join(settings_root, 'key.publickey')
    public_key = open(public_key_fn, 'rb').read()

    resp = render(request, 'browserid/well-known-browserid.json', dict(
        public_key=public_key,
        authentication_url=authentication_url,
        provisioning_url=provisioning_url,
    ))
    resp['Content-Type'] = 'application/json'
    return resp
Example #9
0
def lang_activate(locale):
    """
    Context manager that temporarily activates a locale.

    This is slightly different from use_lang, as it also sets the global
    prefixer used by reverse to determine the locale in urls. We don't use that
    in non-test code because it creates a test request using Django test code.

    This means that use_lang does not affect reverse. We get around that with a
    hack of using the lang GET argument when necessary, but this really should
    be fixed someday.
    """
    if not locale:
        yield
    else:
        old_prefix = get_url_prefix()
        old_locale = get_language()
        rf = test_utils.RequestFactory()
        set_url_prefix(Prefixer(rf.get('/%s/' % (locale, ))))
        activate(locale)
        yield
        set_url_prefix(old_prefix)
        activate(old_locale)
Example #10
0
def lang_activate(locale):
    """
    Context manager that temporarily activates a locale.

    This is slightly different from use_lang, as it also sets the global
    prefixer used by reverse to determine the locale in urls. We don't use that
    in non-test code because it creates a test request using Django test code.

    This means that use_lang does not affect reverse. We get around that with a
    hack of using the lang GET argument when necessary, but this really should
    be fixed someday.
    """
    if not locale:
        yield
    else:
        old_prefix = get_url_prefix()
        old_locale = get_language()
        rf = test_utils.RequestFactory()
        set_url_prefix(Prefixer(rf.get('/%s/' % (locale,))))
        activate(locale)
        yield
        set_url_prefix(old_prefix)
        activate(old_locale)
Example #11
0
def well_known_browserid(request):
    # HACK: The following code is terrible. But, I really wanted some
    # locale-free URLs, in order to force a locale redirect later.
    op = get_url_prefix()
    np = Prefixer(request)
    np.locale = 'XXX'
    set_url_prefix(np)
    authentication_url = reverse('whuru.browserid.sign_in').replace('/XXX', '')
    provisioning_url = reverse('whuru.browserid.provision').replace('/XXX', '')
    set_url_prefix(op)

    settings_root = os.path.dirname(whuru.settings.__file__)
    public_key_fn = os.path.join(settings_root, 'key.publickey')
    public_key = open(public_key_fn, 'rb').read()

    resp = render(
        request, 'browserid/well-known-browserid.json',
        dict(
            public_key=public_key,
            authentication_url=authentication_url,
            provisioning_url=provisioning_url,
        ))
    resp['Content-Type'] = 'application/json'
    return resp