aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/route_manager.py
diff options
context:
space:
mode:
authorGravatar Gareth Coles <[email protected]>2018-02-09 12:19:55 +0000
committerGravatar GitHub <[email protected]>2018-02-09 12:19:55 +0000
commitb38a9f56e54eb21eec37f40075399961ba82906f (patch)
tree3255dc43a11734726509a1ed79d7dfa1da02cb63 /pysite/route_manager.py
parentWeird, PyCharm didn't commit all my changes last push (diff)
Move from straight app registration to Blueprints (#6)
Diffstat (limited to 'pysite/route_manager.py')
-rw-r--r--pysite/route_manager.py24
1 files changed, 18 insertions, 6 deletions
diff --git a/pysite/route_manager.py b/pysite/route_manager.py
index ed0947f8..6ed597b1 100644
--- a/pysite/route_manager.py
+++ b/pysite/route_manager.py
@@ -3,7 +3,7 @@ import importlib
import inspect
import os
-from flask import Flask, abort, g
+from flask import Blueprint, Flask, abort, g
import rethinkdb
@@ -25,18 +25,30 @@ class RouteManager:
)
self.app.secret_key = os.environ.get("WEBPAGE_SECRET_KEY")
- self.load_views()
+ self.main_blueprint = Blueprint("main", __name__)
+
+ print(f"Loading Blueprint: {self.main_blueprint.name}")
+ self.load_views(self.main_blueprint, "pysite/views/main")
+ self.app.register_blueprint(self.main_blueprint)
+ print("")
+
+ self.api_blueprint = Blueprint("api", __name__, subdomain="api")
+
+ print(f"Loading Blueprint: {self.api_blueprint.name}")
+ self.load_views(self.api_blueprint, "pysite/views/api")
+ self.app.register_blueprint(self.api_blueprint)
+ print("")
def run(self):
self.app.run(
port=int(os.environ.get("WEBPAGE_PORT")), debug="FLASK_DEBUG" in os.environ
)
- def load_views(self, location="pysite/views"):
+ def load_views(self, blueprint, location="pysite/views"):
for filename in os.listdir(location):
if os.path.isdir(f"{location}/{filename}"):
# Recurse if it's a directory; load ALL the views!
- self.load_views(location=f"{location}/{filename}")
+ self.load_views(blueprint, location=f"{location}/{filename}")
continue
if filename.endswith(".py") and not filename.startswith("__init__"):
@@ -50,8 +62,8 @@ class RouteManager:
cls is not RouteView and
BaseView in cls.__mro__
):
- cls.setup(self.app)
- print(f"View loaded: {cls.name: <25} ({module.__name__}.{cls_name})")
+ cls.setup(blueprint)
+ print(f">> View loaded: {cls.name: <15} ({module.__name__}.{cls_name})")
def setup_db(self):
connection = self.get_db_connection(connect_database=False)