deb-horizon/openstack_dashboard/theme_settings.py
Diana Whitten c9de52d6bb Dynamic Themes
Horizon themes are now configurable at a user level, through the use
of cookies. The themes that can be set are configurable at a
deployment level through settings.py. Horizon can be configured to
run with multiple themes, and allow users to choose which themes
they wish to run.

Django Compressor:
In order to support dynamic themes, each theme configuration must
be pre-compiled through the Django compressor. By making use of its
built in COMPRESS_OFFLINE_CONTEXT, we now return a generator to
create each of the theme's necessary offline contexts.

Templates:
Horizon themes allowed template overrides via their 'templates'
subfolder.  In order to maintain this parity, a custom theme template
loader was created.  It is run before the other loads, and simply
looks for a Django template in the current theme (cookie driven)
before diverting to the previous template loaders.

Static Files:
Horizon themes allowed static overrides of the images in
'dashboard/img' folder.  A template tag, 'themable_asset' was created
to maintain this parity. Any asset that is wished to be made themable,
given that it is located in Horizon's 'static/dashboard' folder, can
now be made ot be themable.  By making this a template tag, this
gives the developers more granular control over what branders can
customize.

Angular and Plugins:
By far, the trickiest part of this task, Angular and Plugins are
dynamic in the files that they 'discover'.  SCSS is not flexible in
this manner at ALL.  SCSS disallows the importation of a variable
name.  To get around this, themes.scss was created as a Django
template.  This template is the top level import file for all styles
within Horizon, and therefore, allows ALL the scss files to share a
common namespace and thus, can use shared variables as well as extend
shared styles.

Other:
This change is fundamental, in that it changes the method by which
Horizon ingests its SCSS files.  Many problems existing in the
previous implementation, in an effort to make Horizon flexible, its
SCSS was made very inflexible.  This patch corrects those problems.

Change-Id: Ic48b4b5c1d1a41f1e01a8d52784c9d38d192c8f1
Implements: blueprint horizon-dynamic-theme
Closes-Bug: #1480427
2016-02-25 09:49:43 -08:00

104 lines
3.6 KiB
Python

# Copyright 2016 Hewlett Packard Enterprise Software, LLC
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import os
from django.utils.translation import pgettext_lazy
def get_theme_static_dirs(available_themes, collection_dir, root):
static_dirs = []
# Collect and expose the themes that have been configured
for theme in available_themes:
theme_name, theme_label, theme_path = theme
theme_url = os.path.join(collection_dir, theme_name)
theme_path = os.path.join(root, theme_path)
if os.path.exists(os.path.join(theme_path, 'static')):
# Only expose the subdirectory 'static' if it exists from a custom
# theme, allowing other logic to live with a theme that we might
# not want to expose statically
theme_path = os.path.join(theme_path, 'static')
static_dirs.append(
(theme_url, theme_path),
)
return static_dirs
def get_available_themes(available_themes, custom_path, default_path,
default_theme):
new_theme_list = []
# We can only support one path at a time, because of static file
# collection.
custom_ndx = -1
default_ndx = -1
default_theme_ndx = -1
for ndx, each_theme in enumerate(available_themes):
# Maintain Backward Compatibility for CUSTOM_THEME_PATH
if custom_path:
if each_theme[2] == custom_path:
custom_ndx = ndx
# Maintain Backward Compatibility for DEFAULT_THEME_PATH
if default_path:
if each_theme[0] == 'default':
default_ndx = ndx
each_theme = (
'default',
pgettext_lazy('Default style theme', 'Default'),
default_path
)
# Make sure that DEFAULT_THEME is configured for use
if each_theme[0] == default_theme:
default_theme_ndx = ndx
new_theme_list.append(each_theme)
if custom_ndx != -1:
# If CUSTOM_THEME_PATH is set, then we should set that as the default
# theme to make sure that upgrading Horizon doesn't jostle anyone
default_theme = available_themes[custom_ndx][0]
logging.warning("Your AVAILABLE_THEMES already contains your "
"CUSTOM_THEME_PATH, therefore using configuration in "
"AVAILABLE_THEMES for %s." % custom_path)
elif custom_path is not None:
new_theme_list.append(
('custom',
pgettext_lazy('Custom style theme', 'Custom'),
custom_path)
)
default_theme = 'custom'
# If 'default' isn't present at all, add it with the default_path
if default_ndx == -1 and default_path is not None:
new_theme_list.append(
('default',
pgettext_lazy('Default style theme', 'Default'),
default_path)
)
# If default is not configured, we have to set one,
# just grab the first theme
if default_theme_ndx == -1 and custom_ndx == -1:
default_theme = available_themes[0][0]
return new_theme_list, default_theme