From 24f25eddb6f8ec64d624776cfeddc7b01c657820 Mon Sep 17 00:00:00 2001 From: Diana Whitten Date: Fri, 14 Aug 2015 20:51:21 -0700 Subject: [PATCH] SCSS $webroot should inherit from settings.py There is double maintenance right now when you change the STATIC_URL via settings.py, you must also use a custom theme to have access to also change $webroot within the SCSS to serve font-awesome from a custom location. This patch makes the STATIC_URL available by default in the SCSS namespace through variable $static_url. Also, it was being called $webroot in the SCSS, but it should actually be called $static_url since it should map directly to settings.py's STATIC_URL. Change-Id: I1ee40652f8110b176f45b8811f0e0197789a73da Closes-Bug: #1478794 --- doc/source/topics/settings.rst | 19 +++----- horizon/utils/scss_filter.py | 43 +++++++++++++++++++ openstack_dashboard/settings.py | 2 +- .../static/dashboard/scss/_variables.scss | 10 ++--- .../themes/webroot/_variables.scss | 2 +- 5 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 horizon/utils/scss_filter.py diff --git a/doc/source/topics/settings.rst b/doc/source/topics/settings.rst index 0bc2a27dae..4bcfa7e269 100755 --- a/doc/source/topics/settings.rst +++ b/doc/source/topics/settings.rst @@ -1067,18 +1067,6 @@ the web server. For example, if you're accessing the Dashboard via https:///dashboard, you would set this to ``"/dashboard/"``. -Additionally, setting the ``"$webroot"`` SCSS variable is required. You -can change this directly in -``"openstack_dashboard/static/dashboard/scss/_variables.scss"`` or in the -``"_variables.scss"`` file in your custom theme. For more information on -custom themes, see: ``"CUSTOM_THEME_PATH"``. - -Make sure you run ``python manage.py collectstatic`` and -``python manage.py compress`` after you change the ``_variables.scss`` file. - -For your convenience, a custom theme for only setting the web root has been -provided see: ``"/horizon/openstack_dashboard/themes/webroot"`` - .. note:: Additional settings may be required in the config files of your webserver @@ -1125,6 +1113,13 @@ webserver configuration should be updated to match. The value for STATIC_URL must end in '/'. +This value is also available in the scss namespace with the variable name +$static_url. Make sure you run ``python manage.py collectstatic`` and +``python manage.py compress`` after any changes to this value in settings.py. + +For your convenience, a custom theme for only setting the static url has been +provided see: ``"/horizon/openstack_dashboard/themes/webroot"`` + For more information see: https://docs.djangoproject.com/en/1.7/ref/settings/#static-url diff --git a/horizon/utils/scss_filter.py b/horizon/utils/scss_filter.py new file mode 100644 index 0000000000..8d2be1b5d4 --- /dev/null +++ b/horizon/utils/scss_filter.py @@ -0,0 +1,43 @@ +# (c) Copyright 2015 Hewlett-Packard Development Company, L.P. +# +# 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. + +from django.conf import settings + +from django_pyscss.compressor import DjangoScssFilter +from django_pyscss import DjangoScssCompiler + +from scss.namespace import Namespace +from scss.types import String + +import six + + +class HorizonScssFilter(DjangoScssFilter): + def __init__(self, *args, **kwargs): + super(HorizonScssFilter, self).__init__(*args, **kwargs) + + self.namespace = Namespace() + + # Add variables to the SCSS Global Namespace Here + self.namespace.set_variable( + '$static_url', + String(six.text_type(getattr(settings, 'STATIC_URL', '/static/'))) + ) + + # Create a compiler with the right namespace + @property + def compiler(self): + return DjangoScssCompiler( + namespace=self.namespace + ) diff --git a/openstack_dashboard/settings.py b/openstack_dashboard/settings.py index 5a25797082..dc0cf8e1d5 100644 --- a/openstack_dashboard/settings.py +++ b/openstack_dashboard/settings.py @@ -141,7 +141,7 @@ STATICFILES_FINDERS = ( ) COMPRESS_PRECOMPILERS = ( - ('text/scss', 'django_pyscss.compressor.DjangoScssFilter'), + ('text/scss', 'horizon.utils.scss_filter.HorizonScssFilter'), ) COMPRESS_CSS_FILTERS = ( diff --git a/openstack_dashboard/static/dashboard/scss/_variables.scss b/openstack_dashboard/static/dashboard/scss/_variables.scss index 167ed01039..6cacc99dbd 100644 --- a/openstack_dashboard/static/dashboard/scss/_variables.scss +++ b/openstack_dashboard/static/dashboard/scss/_variables.scss @@ -2,10 +2,10 @@ to our variables */ @import "/custom/variables"; - -/* This variable can be used to change the web root of horizon -default value is already '/' */ -$webroot: "" !default; +/* When used with Horizon via Django, this value is set automatically from + settings.py and is added dynamically to the namespace through + horizon/utils/scss_filter.py */ +$static_url: "/static/" !default; /* Horizon Custom Variables */ @@ -59,7 +59,7 @@ $rbrowser-footer-background-color: #f1f1f1; // .table-striped-datatable styles. // Font-awesome path to the icon fonts -$fa-font-path: $webroot + "/static/horizon/lib/font-awesome/fonts"; +$fa-font-path: $static_url + "horizon/lib/font-awesome/fonts"; /* Charts */ diff --git a/openstack_dashboard/themes/webroot/_variables.scss b/openstack_dashboard/themes/webroot/_variables.scss index 8f2e1d5edd..e4edb9157e 100644 --- a/openstack_dashboard/themes/webroot/_variables.scss +++ b/openstack_dashboard/themes/webroot/_variables.scss @@ -1,5 +1,5 @@ /* This variable changes the web root of horizon to /dashboard rather than the default '/' */ -$webroot: "/dashboard"; +$static_url: "/dashboard/static/"; @import "../themes/default/variables"; \ No newline at end of file