Skip to content

Commit

Permalink
Merge pull request #8 from mwallraf/maarten
Browse files Browse the repository at this point in the history
Add basic support for themes. Theme can be configured in config file …
  • Loading branch information
afourmy authored Nov 4, 2018
2 parents 7d12e52 + d5acd51 commit b8d8c39
Show file tree
Hide file tree
Showing 4 changed files with 4,302 additions and 1 deletion.
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

0 comments on commit b8d8c39

Please sign in to comment.