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()
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()
def test_localized_search_plugin(self): """Every locale gets its own plugin.""" response = self.client.get(reverse('search_plugin')) assert '/en-US/search' in response.content # Prefixer and its locale are sticky; clear it before the next request set_url_prefix(None) response = self.client.get(reverse('search_plugin', prefix='/fr/')) assert '/fr/search' in response.content
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)
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)
def _post_teardown(self): # Call the original method. super(test.TestCase, self)._post_teardown() # Restore the settings. settings.INSTALLED_APPS = self._original_installed_apps loading.cache.loaded = False if get_url_prefix: # If we're in funfactoryland, back out of the locale tweaks set_url_prefix(self.old_prefix) activate(self.old_locale)
def _post_teardown(self): # Call the original method. super(test.TestCase, self)._post_teardown() Award.objects.all().delete() Badge.objects.all().delete() loading.cache.loaded = False if get_url_prefix: # If we're in funfactoryland, back out of the locale tweaks set_url_prefix(self.old_prefix)
def _post_teardown(self): # Call the original method. super(test.TestCase, self)._post_teardown() Award.objects.all().delete() Badge.objects.all().delete() # Restore the settings. settings.INSTALLED_APPS = self._original_installed_apps loading.cache.loaded = False if get_url_prefix: # If we're in funfactoryland, back out of the locale tweaks set_url_prefix(self.old_prefix) activate(self.old_locale)
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()
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
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)
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)
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