From 29c68151de9eb65253bbda163cbfe020bbfb91cc Mon Sep 17 00:00:00 2001 From: richard-dds Date: Wed, 1 Aug 2018 11:22:21 -0400 Subject: [PATCH] Convert ui methods into Flask globals --- atst/app.py | 31 +++++++++++++++++++++++++-- atst/routes/__init__.py | 2 +- templates/{base.html.to => base.html} | 22 +++++++++---------- templates/{home.html.to => home.html} | 4 ++-- 4 files changed, 42 insertions(+), 17 deletions(-) rename templates/{base.html.to => base.html} (60%) rename templates/{home.html.to => home.html} (68%) diff --git a/atst/app.py b/atst/app.py index 5128117f..f4cfe183 100644 --- a/atst/app.py +++ b/atst/app.py @@ -1,7 +1,8 @@ import os +import re from configparser import ConfigParser from redis import StrictRedis -from flask import Flask +from flask import Flask, request, g from unipath import Path from atst.api_client import ApiClient @@ -24,6 +25,8 @@ def make_app(config): ) app.config.update(config) + make_flask_callbacks(app) + db.init_app(app) assets.init_app(app) @@ -32,6 +35,21 @@ def make_app(config): return app +def make_flask_callbacks(app): + @app.before_request + def set_globals(): + g.navigationContext = 'workspace' if re.match('\/workspaces\/[A-Za-z0-9]*', request.url) else 'global' + g.dev = os.getenv("TORNADO_ENV", "dev") == "dev" + g.matchesPath = lambda href: re.match('^'+href, request.url) + g.modalOpen = request.args.get("modal", False) + + # TODO: Make me a macro + def modal(self, body): + return self.render_string( + "components/modal.html.to", + body=body) + + # def make_app(config, deps, **kwargs): # routes = [ # url(r"/", Root, {"page": "root"}, name="root"), @@ -180,6 +198,15 @@ def make_deps(config): ), } +def map_config(config): + return { + "ENV": config["default"]["ENVIRONMENT"], + "DEBUG": config["default"]["DEBUG"], + "PORT": int(config["default"]["PORT"]), + "SQLALCHEMY_DATABASE_URI": config["default"]["DATABASE_URI"], + "SQLALCHEMY_TRACK_MODIFICATIONS": False, + **config["default"] + } def make_config(): BASE_CONFIG_FILENAME = os.path.join(os.path.dirname(__file__), "../config/base.ini") @@ -212,4 +239,4 @@ def make_config(): ) config.set("default", "DATABASE_URI", database_uri) - return config["default"] + return map_config(config) diff --git a/atst/routes/__init__.py b/atst/routes/__init__.py index c6b03baf..93fde8e2 100644 --- a/atst/routes/__init__.py +++ b/atst/routes/__init__.py @@ -1,4 +1,4 @@ -from flask import Blueprint, render_template +from flask import Blueprint, render_template, g bp = Blueprint("atst", __name__) diff --git a/templates/base.html.to b/templates/base.html similarity index 60% rename from templates/base.html.to rename to templates/base.html index 3f0d5995..58c804cb 100644 --- a/templates/base.html.to +++ b/templates/base.html @@ -1,21 +1,21 @@ {# TODO: set this context elsewhere #} {# set context='workspace' #} -{% set context=navigationContext() %} +{% set context=g.navigationContext %} - {% block title %}JEDI{% end %} - {% for url in assets['css'].urls() %} - - {% end %} + {% block title %}JEDI{% endblock %} + {% assets "css" %} + + {% endassets %} - + - {% block template_vars %}{% end %} + {% block template_vars %}{% endblock %} {% include 'navigation/topbar.html.to' %} @@ -23,18 +23,16 @@ {% include 'navigation/global_navigation.html.to' %}
- {% block sidenav %}{% end %} + {% block sidenav %}{% endblock %} {% block content %} these are not the droids you are looking for - {% end %} + {% endblock %}
{% include 'footer.html.to' %} - {% block modal %}{% end %} + {% block modal %}{% endblock %} - - diff --git a/templates/home.html.to b/templates/home.html similarity index 68% rename from templates/home.html.to rename to templates/home.html index eb789f28..0df70037 100644 --- a/templates/home.html.to +++ b/templates/home.html @@ -1,4 +1,4 @@ -{% extends "base.html.to" %} +{% extends "base.html" %} {% block content %} @@ -8,7 +8,7 @@ -{% end %} +{% endblock %}