From 5c6bf7d2abee96e6baf6c2e851db503925fcc72a Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Tue, 6 Feb 2018 11:47:19 +0000 Subject: Template rendering --- pysite/base_route.py | 12 +++++++++--- pysite/route_manager.py | 5 +++-- pysite/views/healthcheck.py | 4 ++-- pysite/views/index.py | 6 +++--- pysite/views/invite.py | 4 ++-- templates/.gitkeep | 0 templates/index.html | 10 ++++++++++ 7 files changed, 29 insertions(+), 12 deletions(-) delete mode 100644 templates/.gitkeep create mode 100644 templates/index.html diff --git a/pysite/base_route.py b/pysite/base_route.py index 17e9aee3..16bf0984 100644 --- a/pysite/base_route.py +++ b/pysite/base_route.py @@ -1,23 +1,29 @@ # coding=utf-8 -from flask import Flask +from flask import Flask, render_template from flask.views import MethodView __author__ = "Gareth Coles" class BaseView(MethodView): + def render(self, *template_names, **context): + # thin wrapper here in case it needs to be modified later + return render_template(template_names, **context) + + +class RouteView(BaseView): path = None #: str name = None #: str @classmethod - def setup(cls: "BaseView", app: Flask): + def setup(cls: "RouteView", app: Flask): if not cls.path or not cls.name: raise RuntimeError("Route views must have both `path` and `name` defined") app.add_url_rule(cls.path, view_func=cls.as_view(cls.name)) -class ErrorView(MethodView): +class ErrorView(BaseView): name = None #: str error_code = None #: int diff --git a/pysite/route_manager.py b/pysite/route_manager.py index 501076b7..f922be6a 100644 --- a/pysite/route_manager.py +++ b/pysite/route_manager.py @@ -5,7 +5,7 @@ import os from flask import Flask -from pysite.base_route import BaseView, ErrorView +from pysite.base_route import BaseView, ErrorView, RouteView __author__ = "Gareth Coles" @@ -35,7 +35,8 @@ class RouteManager: inspect.isclass(cls) and cls is not BaseView and cls is not ErrorView and - (BaseView in cls.__mro__ or ErrorView in cls.__mro__) + cls is not RouteView and + BaseView in cls.__mro__ ): cls.setup(self.app) print(f"View loaded: {cls.name: <25} ({module.__name__}.{cls_name})") diff --git a/pysite/views/healthcheck.py b/pysite/views/healthcheck.py index 660c8a96..088bc7b3 100644 --- a/pysite/views/healthcheck.py +++ b/pysite/views/healthcheck.py @@ -1,13 +1,13 @@ # coding=utf-8 from flask import jsonify -from pysite.base_route import BaseView +from pysite.base_route import RouteView __author__ = "Gareth Coles" -class IndexView(BaseView): +class IndexView(RouteView): path = "/healthcheck" name = "healthcheck" diff --git a/pysite/views/index.py b/pysite/views/index.py index 2e779003..040332cc 100644 --- a/pysite/views/index.py +++ b/pysite/views/index.py @@ -1,12 +1,12 @@ # coding=utf-8 -from pysite.base_route import BaseView +from pysite.base_route import RouteView __author__ = "Gareth Coles" -class IndexView(BaseView): +class IndexView(RouteView): path = "/" name = "index" def get(self): - return "Coming soon:tm:" + return self.render("index.html") diff --git a/pysite/views/invite.py b/pysite/views/invite.py index d035fc99..07ddc830 100644 --- a/pysite/views/invite.py +++ b/pysite/views/invite.py @@ -1,13 +1,13 @@ # coding=utf-8 from flask import redirect -from pysite.base_route import BaseView +from pysite.base_route import RouteView __author__ = "Gareth Coles" -class InviteView(BaseView): +class InviteView(RouteView): path = "/invite" name = "invite" diff --git a/templates/.gitkeep b/templates/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 00000000..07bd4541 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,10 @@ + + + + + Python | Home + + +

Coming soon:tm:

+ + \ No newline at end of file -- cgit v1.2.3