Skip to content

Commit

Permalink
[ckan#6817] Fix theme settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Zharktas committed Sep 27, 2022
1 parent ccb2951 commit f9084f9
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 34 deletions.
6 changes: 6 additions & 0 deletions changes/6817.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fix theme settings. Options which were used to specify a CSS file
with a base theme are replaced. Use altenatives below in order
to specify **asset** with a base theme for application:

* `ckan.main_css` replaced by `ckan.theme`
* `ckan.i18n.rtl_css` replaced by `ckan.i18n.rtl_theme`
12 changes: 6 additions & 6 deletions ckan/controllers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ def __before__(self, action, **params):

def _get_config_form_items(self):
# Styles for use in the form.select() macro.
styles = [{'text': 'Default', 'value': '/base/css/main.css'},
{'text': 'Red', 'value': '/base/css/red.css'},
{'text': 'Green', 'value': '/base/css/green.css'},
{'text': 'Maroon', 'value': '/base/css/maroon.css'},
{'text': 'Fuchsia', 'value': '/base/css/fuchsia.css'}]
styles = [{'text': 'Default', 'value': 'css/main'},
{'text': 'Red', 'value': 'css/red'},
{'text': 'Green', 'value': 'css/green'},
{'text': 'Maroon', 'value': 'css/maroon'},
{'text': 'Fuchsia', 'value': 'css/fuchsia'}]

homepages = [{'value': '1', 'text': 'Introductory area, search, featured group and featured organization'},
{'value': '2', 'text': 'Search, stats, introductory area, featured organization and featured group'},
{'value': '3', 'text': 'Search, introductory area and stats'}]

items = [
{'name': 'ckan.site_title', 'control': 'input', 'label': _('Site Title'), 'placeholder': ''},
{'name': 'ckan.main_css', 'control': 'select', 'options': styles, 'label': _('Style'), 'placeholder': ''},
{'name': 'ckan.theme', 'control': 'select', 'options': styles, 'label': _('Style'), 'placeholder': ''},
{'name': 'ckan.site_description', 'control': 'input', 'label': _('Site Tag Line'), 'placeholder': ''},
{'name': 'ckan.site_logo', 'control': 'image_upload', 'label': _('Site Tag Logo'), 'placeholder': '', 'upload_enabled':h.uploads_enabled(),
'field_url': 'ckan.site_logo', 'field_upload': 'logo_upload', 'field_clear': 'clear_logo_upload'},
Expand Down
27 changes: 19 additions & 8 deletions ckan/lib/app_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,23 @@
# A place to store the origional config options of we override them
_CONFIG_CACHE = {}

def set_main_css(css_file):
''' Sets the main_css. The css_file must be of the form file.css '''
assert css_file.endswith('.css')
new_css = css_file
# FIXME we should check the css file exists
app_globals.main_css = str(new_css)
def set_theme(asset):
''' Sets the theme.
The `asset` argument is a name of existing web-asset registered by CKAN
itself or by any enabled extension.
If asset is not registered, use default theme instead.
'''
from ckan.lib.webassets_tools import env

assert env
if asset not in env:
log.error(
"Asset '%s' does not exist. Fallback to '%s'",
asset, 'css/main'
)
asset = 'css/main'

app_globals.theme = asset


def set_app_global(key, value):
Expand Down Expand Up @@ -168,8 +179,8 @@ def get_config_value(key, default=''):
get_config_value(key)

# custom styling
main_css = get_config_value('ckan.main_css', '/base/css/main.css')
set_main_css(main_css)
theme = get_config_value('ckan.theme') or 'css/main'
set_theme(theme)

if app_globals.site_logo:
app_globals.header_class = 'header-image'
Expand Down
4 changes: 2 additions & 2 deletions ckan/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,8 @@ def is_rtl_language():


@core_helper
def get_rtl_css():
return config.get('ckan.i18n.rtl_css', '/base/css/main-rtl.css')
def get_rtl_theme():
return config.get('ckan.i18n.rtl_theme', 'css/main-rtl')


class Message(object):
Expand Down
2 changes: 1 addition & 1 deletion ckan/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ def default_update_configuration_schema(
'ckan.site_about': [ignore_missing, unicode_safe],
'ckan.site_intro_text': [ignore_missing, unicode_safe],
'ckan.site_custom_css': [ignore_missing, unicode_safe],
'ckan.main_css': [ignore_missing, unicode_safe],
'ckan.theme': [ignore_missing, unicode_safe],
'ckan.homepage_style': [ignore_missing, is_positive_integer],
'logo_upload': [ignore_missing, unicode_safe],
'clear_logo_upload': [ignore_missing, unicode_safe],
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/admin/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

{{ form.input('ckan.site_title', id='field-ckan-site-title', label=_('Site Title'), value=data['ckan.site_title'], error=error, classes=['control-medium']) }}

{{ form.select('ckan.main_css', id='field-ckan-main-css', label=_('Style'), options=styles, selected=data['ckan.main_css'], error=error) }}
{{ form.select('ckan.theme', id='field-ckan-main-css', label=_('Style'), options=styles, selected=data['ckan.theme'], error=error) }}

{{ form.input('ckan.site_description', id='field-ckan-site-description', label=_('Site Tag Line'), value=data['ckan.site_description'], error=error, classes=['control-medium']) }}

Expand Down
5 changes: 2 additions & 3 deletions ckan/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@
#}
{%- block styles %}
{# TODO: store just name of asset instead of path to it. #}
{% set main_css = h.get_rtl_css() if h.is_rtl_language() else g.main_css %}
{# strip '/base/' prefix and '.css' suffix #}
{% asset main_css[6:-4] %}
{% set theme = h.get_rtl_theme() if h.is_rtl_language() else g.theme %}
{% asset theme %}
{% endblock %}

{% block head_extras %}
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/macros/autoform.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{% set form_info = [
{'name': 'ckan.site_title', 'control': 'input', 'label': _('Site Title'), 'placeholder': ''},
{'name': 'ckan.main_css', 'control': 'select', 'options': styles, 'label': _('Style'), 'placeholder': ''},
{'name': 'ckan.theme', 'control': 'select', 'options': styles, 'label': _('Style'), 'placeholder': ''},
{'name': 'ckan.site_description', 'control': 'input', 'label': _('Site Tag Line'), 'placeholder': ''},
{'name': 'ckan.site_logo', 'control': 'input', 'label': _('Site Tag Logo'), 'placeholder': ''},
{'name': 'ckan.site_about', 'control': 'markdown', 'label': _('About'), 'placeholder': _('About page text')},
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/controllers/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_main_css_list(self, app, sysadmin_env):
for option in style_select_options:
assert option.string in STYLE_NAMES

def test_main_css(self, app, sysadmin_env):
def test_main_theme(self, app, sysadmin_env):
"""Select a colour style"""

# current style
Expand All @@ -72,7 +72,7 @@ def test_main_css(self, app, sysadmin_env):

url = url_for(u"admin.config")
# set new style css
form = {"ckan.main_css": "/base/css/red.css", "save": ""}
form = {"ckan.theme": "css/red", "save": ""}
resp = app.post(url, data=form, environ_overrides=sysadmin_env)

assert "red.css" in resp or "red.min.css" in resp
Expand Down
12 changes: 6 additions & 6 deletions ckan/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def _get_sysadmins():
def _get_config_options():
styles = [{
u'text': u'Default',
u'value': u'/base/css/main.css'
u'value': u'css/main'
}, {
u'text': u'Red',
u'value': u'/base/css/red.css'
u'value': u'css/red'
}, {
u'text': u'Green',
u'value': u'/base/css/green.css'
u'value': u'css/green'
}, {
u'text': u'Maroon',
u'value': u'/base/css/maroon.css'
u'value': u'css/maroon'
}, {
u'text': u'Fuchsia',
u'value': u'/base/css/fuchsia.css'
u'value': u'css/fuchsia'
}]

homepages = [{
Expand All @@ -62,7 +62,7 @@ def _get_config_options():

def _get_config_items():
return [
u'ckan.site_title', u'ckan.main_css', u'ckan.site_description',
u'ckan.site_title', u'ckan.theme', u'ckan.site_description',
u'ckan.site_logo', u'ckan.site_about', u'ckan.site_intro_text',
u'ckan.site_custom_css', u'ckan.homepage_style'
]
Expand Down
4 changes: 2 additions & 2 deletions doc/contributing/frontend/templating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ values for the (key, value, delete) fields respectively.
classes - An array of classes to apply to the control-group.
attrs - Dictionary of extra tag attributes
is_required - Boolean of whether this input is required for the form to validate


Examples:

Expand Down Expand Up @@ -481,7 +481,7 @@ Example

{% set form_info = [
{'name': 'ckan.site_title', 'control': 'input', 'label': _('Site Title'), 'placeholder': ''},
{'name': 'ckan.main_css', 'control': 'select', 'options': styles, 'label': _('Style'), 'placeholder': ''},
{'name': 'ckan.theme', 'control': 'select', 'options': styles, 'label': _('Style'), 'placeholder': ''},
{'name': 'ckan.site_description', 'control': 'input', 'label': _('Site Tag Line'), 'placeholder': ''},
{'name': 'ckan.site_logo', 'control': 'input', 'label': _('Site Tag Logo'), 'placeholder': ''},
{'name': 'ckan.site_about', 'control': 'markdown', 'label': _('About'), 'placeholder': _('About page text')},
Expand Down
4 changes: 2 additions & 2 deletions doc/extensions/remote-config-update.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ First of all, let's call the :py:func:`~ckan.logic.action.get.config_option_list
"help": "http://localhost:5000/api/3/action/help_show?name=config_option_list",
"result": [
"ckan.site_custom_css",
"ckan.main_css",
"ckan.theme",
"ckan.site_title",
"ckan.site_about",
"ckan.site_url",
Expand Down Expand Up @@ -71,7 +71,7 @@ Restart the web server and do another request to the :py:func:`~ckan.logic.actio
"ckan.datasets_per_page",
"ckanext.example_iconfigurer.test_conf",
"ckan.site_custom_css",
"ckan.main_css",
"ckan.theme",
"ckan.site_title",
"ckan.site_about",
"ckan.site_url",
Expand Down

0 comments on commit f9084f9

Please sign in to comment.