horizon/openstack_dashboard/templatetags/themes.py
Hervé Beraud 17d8ab6103 Stop to use the __future__ module.
The __future__ module [1] was used in this context to ensure compatibility
between python 2 and python 3.

We previously dropped the support of python 2.7 [2] and now we only support
python 3 so we don't need to continue to use this module and the imports
listed below.

Imports commonly used and their related PEPs:
- `division` is related to PEP 238 [3]
- `print_function` is related to PEP 3105 [4]
- `unicode_literals` is related to PEP 3112 [5]
- `with_statement` is related to PEP 343 [6]
- `absolute_import` is related to PEP 328 [7]

[1] https://docs.python.org/3/library/__future__.html
[2] https://governance.openstack.org/tc/goals/selected/ussuri/drop-py27.html
[3] https://www.python.org/dev/peps/pep-0238
[4] https://www.python.org/dev/peps/pep-3105
[5] https://www.python.org/dev/peps/pep-3112
[6] https://www.python.org/dev/peps/pep-0343
[7] https://www.python.org/dev/peps/pep-0328

Change-Id: Ic03754dcaaa4f1c0018294aa52bb05d956aa5d10
2020-06-03 10:45:18 +02:00

91 lines
2.4 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 os
from urllib.request import pathname2url
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
from django import template
from horizon import themes as hz_themes
register = template.Library()
def get_theme(request):
this_theme = hz_themes.get_default_theme()
try:
theme = request.COOKIES[hz_themes.get_theme_cookie_name()]
for each_theme in hz_themes.get_themes():
if theme == each_theme[0]:
this_theme = each_theme[0]
except KeyError:
pass
return this_theme
def find_asset(theme, asset):
theme_path = ''
for name, label, path in hz_themes.get_themes():
if theme == name:
theme_path = path
theme_path = os.path.join(settings.ROOT_PATH, theme_path)
# If there is a 'static' subdir of the theme, then use
# that as the theme's asset root path
static_path = os.path.join(theme_path, 'static')
if os.path.exists(static_path):
theme_path = static_path
# The full path to the asset requested
asset_path = os.path.join(theme_path, asset)
if os.path.exists(asset_path):
return_path = os.path.join(hz_themes.get_theme_dir(), theme, asset)
else:
return_path = os.path.join('dashboard', asset)
return staticfiles_storage.url(pathname2url(return_path))
@register.simple_tag()
def themes():
return hz_themes.get_selectable_themes()
@register.simple_tag()
def theme_cookie():
return hz_themes.get_theme_cookie_name()
@register.simple_tag()
def theme_dir():
return hz_themes.get_theme_dir()
@register.simple_tag(takes_context=True)
def current_theme(context):
return get_theme(context.request)
@register.simple_tag(takes_context=True)
def themable_asset(context, asset):
return find_asset(get_theme(context.request), asset)