3
0
Fork 0

feat: add basic theme system

This is intended to allow override of look/feel without overriding the
logic/structure of templates.  In practice the main goal internally is
to allow testing of Vue 3 + Oruga, to eventually replace Vue 2 + Buefy
as the default theme.
This commit is contained in:
Lance Edgar 2025-06-29 09:16:44 -05:00
parent 749aca560a
commit 796e793547
20 changed files with 1604 additions and 52 deletions

View file

@ -11,9 +11,12 @@ from fanstatic import Library, Resource
from pyramid import testing
from wuttjamaican.conf import WuttaConfig
from wuttjamaican.testing import ConfigTestCase
from wuttjamaican.testing import ConfigTestCase, DataTestCase
from wuttjamaican.util import resource_path
from wuttaweb import util as mod
from wuttaweb.app import establish_theme
from wuttaweb.testing import WebTestCase
class TestFieldList(TestCase):
@ -621,3 +624,89 @@ class TestMakeJsonSafe(TestCase):
'bar',
"Betty Boop",
])
class TestGetAvailableThemes(TestCase):
def setUp(self):
self.config = WuttaConfig()
self.app = self.config.get_app()
def test_defaults(self):
themes = mod.get_available_themes(self.config)
self.assertEqual(themes, ['default', 'butterfly'])
def test_sorting(self):
self.config.setdefault('wuttaweb.themes.keys', 'default, foo2, foo4, foo1')
themes = mod.get_available_themes(self.config)
self.assertEqual(themes, ['default', 'foo1', 'foo2', 'foo4'])
def test_default_omitted(self):
self.config.setdefault('wuttaweb.themes.keys', 'butterfly, foo')
themes = mod.get_available_themes(self.config)
self.assertEqual(themes, ['default', 'butterfly', 'foo'])
def test_default_notfirst(self):
self.config.setdefault('wuttaweb.themes.keys', 'butterfly, foo, default')
themes = mod.get_available_themes(self.config)
self.assertEqual(themes, ['default', 'butterfly', 'foo'])
class TestGetEffectiveTheme(DataTestCase):
def test_default(self):
theme = mod.get_effective_theme(self.config)
self.assertEqual(theme, 'default')
def test_override_config(self):
self.app.save_setting(self.session, 'wuttaweb.theme', 'butterfly')
self.session.commit()
theme = mod.get_effective_theme(self.config)
self.assertEqual(theme, 'butterfly')
def test_override_param(self):
theme = mod.get_effective_theme(self.config, theme='butterfly')
self.assertEqual(theme, 'butterfly')
def test_invalid(self):
self.assertRaises(ValueError, mod.get_effective_theme, self.config, theme='invalid')
class TestThemeTemplatePath(DataTestCase):
def test_default(self):
path = mod.get_theme_template_path(self.config, theme='default')
# nb. even though the path does not exist, we still want to
# pretend like it does, hence prev call should return this:
expected = resource_path('wuttaweb:templates/themes/default')
self.assertEqual(path, expected)
def test_default(self):
path = mod.get_theme_template_path(self.config, theme='butterfly')
expected = resource_path('wuttaweb:templates/themes/butterfly')
self.assertEqual(path, expected)
def test_custom(self):
self.config.setdefault('wuttaweb.themes.keys', 'default, butterfly, poser')
self.config.setdefault('wuttaweb.theme.poser', '/tmp/poser-theme')
path = mod.get_theme_template_path(self.config, theme='poser')
self.assertEqual(path, '/tmp/poser-theme')
class TestSetAppTheme(WebTestCase):
def test_basic(self):
# establish default
settings = self.request.registry.settings
self.assertNotIn('wuttaweb.theme', settings)
establish_theme(settings)
self.assertEqual(settings['wuttaweb.theme'], 'default')
# set to butterfly
mod.set_app_theme(self.request, 'butterfly', session=self.session)
self.assertEqual(settings['wuttaweb.theme'], 'butterfly')
# set back to default
mod.set_app_theme(self.request, 'default', session=self.session)
self.assertEqual(settings['wuttaweb.theme'], 'default')