hobo: improve theme handling with full support for overlays (#18426)

This commit is contained in:
Frédéric Péters 2017-09-04 15:33:54 +02:00
parent d5ba994201
commit 22a8be9acf
3 changed files with 78 additions and 8 deletions

View File

@ -249,7 +249,33 @@ def test_update_configuration():
assert pub.cfg['misc']['sitename'] == 'Test wcs'
assert pub.cfg['emails']['footer'] == 'Hello world.'
assert pub.cfg['emails']['from'] == 'noreply@example.net'
def test_update_themes():
pub.cfg['branding'] = {'theme': 'default'}
service = [x for x in HOBO_JSON.get('services', []) if x.get('service-id') == 'wcs'][0]
hobo_cmd.update_configuration(service, pub)
assert pub.cfg['branding']['theme'] == 'default'
service['variables']['theme'] = 'foobar'
hobo_cmd.update_configuration(service, pub)
assert pub.cfg['branding']['theme'] == 'default'
hobo_cmd.THEMES_DIRECTORY = os.path.join(os.path.dirname(__file__), 'themes')
hobo_cmd.update_configuration(service, pub)
assert pub.cfg['branding']['theme'] == 'publik-base'
assert os.readlink(os.path.join(pub.app_dir, 'static')) == \
os.path.join(hobo_cmd.THEMES_DIRECTORY, 'foobar/static')
assert os.readlink(os.path.join(pub.app_dir, 'templates')) == \
os.path.join(hobo_cmd.THEMES_DIRECTORY, 'foobar/templates')
assert os.readlink(os.path.join(pub.app_dir, 'theme')) == \
os.path.join(hobo_cmd.THEMES_DIRECTORY, 'publik-base')
service['variables']['theme'] = 'foobar2'
hobo_cmd.update_configuration(service, pub)
assert not os.path.lexists(os.path.join(pub.app_dir, 'static'))
assert not os.path.lexists(os.path.join(pub.app_dir, 'templates'))
assert os.readlink(os.path.join(pub.app_dir, 'theme')) == \
os.path.join(hobo_cmd.THEMES_DIRECTORY, 'foobar2')
def test_update_profile():
profile = HOBO_JSON.get('profile')

View File

@ -0,0 +1,18 @@
[
{"id": "foobar",
"label": "Foobar",
"module": "publik-base",
"variables": {
"css_variant": "foobar",
"theme_color": "#D8297B"
},
"overlay": "foobar"
},
{"id": "foobar2",
"label": "Foobar2",
"variables": {
"css_variant": "foobar2",
"theme_color": "#D8297B"
}
}
]

View File

@ -32,9 +32,6 @@ from qommon.storage import atomic_write
from wcs.admin.settings import UserFieldsFormDef
from wcs.fields import StringField, EmailField
# TODO: import this from django settings
THEMES_DIRECTORY = os.environ.get('THEMES_DIRECTORY', '/usr/share/publik/themes')
class NoChange(Exception):
pass
@ -50,6 +47,9 @@ def atomic_symlink(src, dst):
class CmdCheckHobos(Command):
name = 'hobo_deploy'
# TODO: import this from django settings
THEMES_DIRECTORY = os.environ.get('THEMES_DIRECTORY', '/usr/share/publik/themes')
def __init__(self):
Command.__init__(self, [
make_option('--ignore-timestamp', action='store_true',
@ -156,16 +156,42 @@ class CmdCheckHobos(Command):
if not pub.cfg.get('emails'):
pub.cfg['emails'] = {}
theme_id = self.all_services.get('variables', {}).get('theme')
if theme_id:
variables = self.all_services.get('variables') or {}
variables.update(service.get('variables') or {})
theme_id = variables.get('theme')
theme_data = None
if theme_id and os.path.exists(self.THEMES_DIRECTORY):
for theme_module in os.listdir(self.THEMES_DIRECTORY):
try:
themes_json = json.load(open(
os.path.join(self.THEMES_DIRECTORY, theme_module, 'themes.json')))
except IOError:
continue
for theme_data in themes_json:
if theme_data.get('id') == theme_id:
if not 'module' in theme_data:
theme_data['module'] = theme_module
break
else:
theme_data = None
continue
break
if theme_data:
pub.cfg['branding'] = {'theme': 'publik-base'}
tenant_dir = pub.app_dir
theme_dir = os.path.join(tenant_dir, 'theme')
target_dir = os.path.join(THEMES_DIRECTORY, 'publik-base')
target_dir = os.path.join(self.THEMES_DIRECTORY, theme_data['module'])
atomic_symlink(target_dir, theme_dir)
for component in ('static', 'templates'):
component_dir = os.path.join(tenant_dir, component)
if os.path.lexists(component_dir):
os.unlink(component_dir)
if theme_data.get('overlay'):
atomic_symlink(
os.path.join(self.THEMES_DIRECTORY, theme_data['overlay'], component),
component_dir)
variables = self.all_services.get('variables') or {}
variables.update(service.get('variables') or {})
if variables.get('default_from_email'):
pub.cfg['emails']['from'] = variables.get('default_from_email').encode('utf-8')
if variables.get('email_signature') is not None: