aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Gareth Coles <[email protected]>2018-05-15 14:40:11 +0100
committerGravatar Gareth Coles <[email protected]>2018-05-15 14:40:11 +0100
commitaf412314eaba6a955b364da2ff7e4dbfb8eb7003 (patch)
tree9f9e7a1ac2d0db240c56569de6a964969eb10505
parentUpdate font-awesome to 5.0.13 (diff)
Add TemplateView class for views that only render a template
-rw-r--r--pysite/base_route.py37
-rw-r--r--pysite/route_manager.py3
-rw-r--r--pysite/views/main/about/index.py8
-rw-r--r--pysite/views/main/about/rules.py8
-rw-r--r--pysite/views/main/index.py8
-rw-r--r--pysite/views/main/info/help.py8
-rw-r--r--pysite/views/main/info/index.py8
-rw-r--r--pysite/views/main/info/jams.py8
-rw-r--r--pysite/views/wiki/special/index.py9
9 files changed, 60 insertions, 37 deletions
diff --git a/pysite/base_route.py b/pysite/base_route.py
index e9df7afe..58be7598 100644
--- a/pysite/base_route.py
+++ b/pysite/base_route.py
@@ -206,3 +206,40 @@ class ErrorView(BaseView):
else:
raise RuntimeError(
"Error views must have an `error_code` that is either an `int` or an iterable") # pragma: no cover # noqa: E501
+
+
+class TemplateView(RouteView):
+ """
+ An easy view for routes that simply render a template with no extra information.
+
+ This class is intended to be subclassed - use it as a base class for your own views, and set the class-level
+ attributes as appropriate. For example:
+
+ >>> class MyView(TemplateView):
+ ... name = "my_view" # Flask internal name for this route
+ ... path = "/my_view" # Actual URL path to reach this route
+ ... template = "my_view.html" # Template to use
+
+ Note that this view only handles GET requests. If you need any other verbs, you can implement them yourself
+ or just use one of the more customizable base view classes.
+ """
+
+ template = None # type: str
+
+ @classmethod
+ def setup(cls: "TemplateView", manager: "pysite.route_manager.RouteManager", blueprint: Blueprint):
+ """
+ Set up the view, deferring most setup to the superclasses but checking for the template attribute.
+
+ :param manager: Instance of the current RouteManager
+ :param blueprint: Current Flask blueprint to register the error handler for
+ """
+
+ if hasattr(super(), "setup"):
+ super().setup(manager, blueprint) # pragma: no cover
+
+ if not cls.template:
+ raise RuntimeError("Template views must have `template` defined")
+
+ def get(self, *_):
+ return self.render(self.template)
diff --git a/pysite/route_manager.py b/pysite/route_manager.py
index a666ce09..764049cd 100644
--- a/pysite/route_manager.py
+++ b/pysite/route_manager.py
@@ -8,7 +8,7 @@ from flask_dance.contrib.discord import make_discord_blueprint
from flask_sockets import Sockets
from gunicorn_config import _when_ready as when_ready
-from pysite.base_route import APIView, BaseView, ErrorView, RouteView
+from pysite.base_route import APIView, BaseView, ErrorView, RouteView, TemplateView
from pysite.constants import (
CSRF, DEBUG_MODE, DISCORD_OAUTH_AUTHORIZED, DISCORD_OAUTH_ID, DISCORD_OAUTH_REDIRECT,
DISCORD_OAUTH_SCOPE, DISCORD_OAUTH_SECRET, PREFERRED_URL_SCHEME)
@@ -131,6 +131,7 @@ class RouteManager:
cls is not RouteView and
cls is not APIView and
cls is not WS and
+ cls is not TemplateView and
(
BaseView in cls.__mro__ or
WS in cls.__mro__
diff --git a/pysite/views/main/about/index.py b/pysite/views/main/about/index.py
index 9f211702..6f5ef1c8 100644
--- a/pysite/views/main/about/index.py
+++ b/pysite/views/main/about/index.py
@@ -1,9 +1,7 @@
-from pysite.base_route import RouteView
+from pysite.base_route import TemplateView
-class IndexView(RouteView):
+class IndexView(TemplateView):
path = "/about/"
name = "about.index"
-
- def get(self):
- return self.render("main/about/index.html")
+ template = "main/about/index.html"
diff --git a/pysite/views/main/about/rules.py b/pysite/views/main/about/rules.py
index 22e79afa..a40110a1 100644
--- a/pysite/views/main/about/rules.py
+++ b/pysite/views/main/about/rules.py
@@ -1,9 +1,7 @@
-from pysite.base_route import RouteView
+from pysite.base_route import TemplateView
-class RulesView(RouteView):
+class RulesView(TemplateView):
path = "/about/rules"
name = "about.rules"
-
- def get(self):
- return self.render("main/about/rules.html")
+ template = "main/about/rules.html"
diff --git a/pysite/views/main/index.py b/pysite/views/main/index.py
index ef5ed700..874961bb 100644
--- a/pysite/views/main/index.py
+++ b/pysite/views/main/index.py
@@ -1,9 +1,7 @@
-from pysite.base_route import RouteView
+from pysite.base_route import TemplateView
-class IndexView(RouteView):
+class IndexView(TemplateView):
path = "/"
name = "index"
-
- def get(self):
- return self.render("main/index.html")
+ template = "main/index.html"
diff --git a/pysite/views/main/info/help.py b/pysite/views/main/info/help.py
index a47362fc..6a82a9ed 100644
--- a/pysite/views/main/info/help.py
+++ b/pysite/views/main/info/help.py
@@ -1,9 +1,7 @@
-from pysite.base_route import RouteView
+from pysite.base_route import TemplateView
-class HelpView(RouteView):
+class HelpView(TemplateView):
path = "/info/help"
name = "info.help"
-
- def get(self):
- return self.render("main/info/help.html")
+ template = "main/info/help.html"
diff --git a/pysite/views/main/info/index.py b/pysite/views/main/info/index.py
index f9fab682..97678ee4 100644
--- a/pysite/views/main/info/index.py
+++ b/pysite/views/main/info/index.py
@@ -1,9 +1,7 @@
-from pysite.base_route import RouteView
+from pysite.base_route import TemplateView
-class IndexView(RouteView):
+class IndexView(TemplateView):
path = "/info/"
name = "info.index"
-
- def get(self):
- return self.render("main/info/index.html")
+ template = "main/info/index.html"
diff --git a/pysite/views/main/info/jams.py b/pysite/views/main/info/jams.py
index 9158091f..85b6a6dc 100644
--- a/pysite/views/main/info/jams.py
+++ b/pysite/views/main/info/jams.py
@@ -1,9 +1,7 @@
-from pysite.base_route import RouteView
+from pysite.base_route import TemplateView
-class IndexView(RouteView):
+class IndexView(TemplateView):
path = "/info/jams"
name = "info.jams"
-
- def get(self):
- return self.render("main/info/jams.html")
+ template = "main/info/jams.html"
diff --git a/pysite/views/wiki/special/index.py b/pysite/views/wiki/special/index.py
index 15c0a649..ccfc7a5a 100644
--- a/pysite/views/wiki/special/index.py
+++ b/pysite/views/wiki/special/index.py
@@ -1,10 +1,7 @@
-from pysite.base_route import RouteView
-from pysite.mixins import DBMixin
+from pysite.base_route import TemplateView
-class PageView(RouteView, DBMixin):
+class PageView(TemplateView):
path = "/special"
name = "special"
-
- def get(self):
- return self.render("wiki/special.html")
+ template = "wiki/special.html"