Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for themes. Theme can be configured in config file … #8

Merged
merged 3 commits into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from flask import Flask
from flask import Flask, url_for
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from importlib import import_module
from logging import basicConfig, DEBUG, getLogger, StreamHandler
from os import path

db = SQLAlchemy()
login_manager = LoginManager()
Expand Down Expand Up @@ -36,6 +37,36 @@ def configure_logs(app):
logger.addHandler(StreamHandler())


def apply_themes(app):
"""
Add support for themes.

If DEFAULT_THEME is set then all calls to
url_for('static', filename='')
will modfify the url to include the theme name

The theme parameter can be set directly in url_for as well:
ex. url_for('static', filename='', theme='')

If the file cannot be found in the /static/<theme>/ lcation then
the url will not be modified and the file is expected to be
in the default /static/ location
"""
@app.context_processor
def override_url_for():
return dict(url_for=_generate_url_for_theme)

def _generate_url_for_theme(endpoint, **values):
if endpoint.endswith('static'):
themename = values.get('theme', None) or \
app.config.get('DEFAULT_THEME', None)
if themename:
theme_file = "{}/{}".format(themename, values.get('filename', ''))
if path.isfile(path.join(app.static_folder, theme_file)):
values['filename'] = theme_file
return url_for(endpoint, **values)


def create_app(config, selenium=False):
app = Flask(__name__, static_folder='base/static')
app.config.from_object(config)
Expand All @@ -45,4 +76,5 @@ def create_app(config, selenium=False):
register_blueprints(app)
configure_database(app)
configure_logs(app)
apply_themes(app)
return app
Loading