aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/base_route.py
blob: 17e9aee3f2585ca6a662fd47574b47938d7062c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# coding=utf-8
from flask import Flask
from flask.views import MethodView

__author__ = "Gareth Coles"


class BaseView(MethodView):
    path = None  #: str
    name = None  #: str

    @classmethod
    def setup(cls: "BaseView", 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):
    name = None  #: str
    error_code = None  #: int

    @classmethod
    def setup(cls: "ErrorView", app: Flask):
        if not cls.name or not cls.error_code:
            raise RuntimeError("Error views must have both `name` and `error_code` defined")

        app._register_error_handler(None, cls.error_code, cls.as_view(cls.name))