def test_current_theme_path(self): """ Tests get_current_theme returns Theme with correct directory. """ theme = get_current_theme() self.assertEqual(theme.path, settings.DJANGO_ROOT + "/tests/themes/test-theme") self.assertIn(theme.path, unicode(theme))
def test_get_current_theme_with_theming_disabled(self): """ Tests get_current_theme returns None if theming is disabled. """ with override_settings(ENABLE_COMPREHENSIVE_THEMING=False): theme = get_current_theme() self.assertIsNone(theme)
def url(self, name): """ Returns url of the asset, themed url will be returned if the asset is themed otherwise default asset url will be returned. Args: name: name of the asset, e.g. 'images/logo.png' Returns: url of the asset, e.g. '/static/red-theme/images/logo.png' if current theme is red-theme and logo is provided by red-theme otherwise '/static/images/logo.png' """ prefix = '' theme = get_current_theme() # get theme prefix from site address if if asset is accessed via a url if theme: prefix = theme.theme_dir_name # get theme prefix from storage class, if asset is accessed during collectstatic run elif self.prefix: prefix = self.prefix # join theme prefix with asset name if theme is applied and themed asset exists if prefix and self.themed(name, prefix): name = os.path.join(prefix, name) return super(ThemeStorage, self).url(name)
def test_get_current_site_theme_raises_no_error_when_accessed_in_commands(self): """ Tests current site theme returns None and does not errors out if it is accessed inside management commands and request object is not present. """ with patch("ecommerce.theming.helpers.get_current_request", return_value=None): theme = get_current_theme() self.assertIsNone(theme)
def test_get_current_theme_template_dirs(self): """ Tests get_current_theme().template_dirs returns correct template dirs for the current theme. """ themes_dir = settings.COMPREHENSIVE_THEME_DIRS[0] expected_theme_dirs = [ themes_dir / "test-theme" / "templates", themes_dir / "test-theme" / "templates" / "oscar", ] actual_theme_dirs = get_current_theme().template_dirs self.assertItemsEqual(expected_theme_dirs, actual_theme_dirs)
def get_theme_template_sources(): """ Return template sources for the given theme and if request object is None (this would be the case for management commands) return template sources for all themes. """ if get_current_request(): # template is being accessed by a view, so return templates sources for current theme theme = get_current_theme() return theme and theme.template_dirs else: # if request object is not present, then this method is being called inside a management # command and return all theme template sources for compression return get_all_theme_template_dirs()
def get_dirs(self): dirs = super(ThemeTemplateLoader, self).get_dirs() theme_dirs = [] if get_current_request(): # If the template is being loaded in a request, prepend the current theme's template directories # so the theme's templates take precedence. theme = get_current_theme() if theme: theme_dirs = theme.template_dirs else: # If we are outside of a request, we are most likely running the compress management command, in which # case we should load all directories for all themes. theme_dirs = get_all_theme_template_dirs() return theme_dirs + dirs
def test_get_current_theme_value_error(self): """ Tests get current theme method returns None if the theme dir is not present in any of the theme dirs. """ theme = get_current_theme() self.assertIsNone(theme)
def test_get_current_theme(self): """ Tests current site theme name. """ theme = get_current_theme() self.assertEqual(theme.theme_dir_name, 'test-theme')