Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network_webui/flask_classy.py
blob: f2a4aa7dab95299f2892cbf295af3c0976f3203d (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
    Flask-Classy
    ------------

    Class based views for the Flask microframework.

    :copyright: (c) 2012 by Freedom Dumlao.
    :license: BSD, see LICENSE for more details.
"""

__version__ = "0.4.3"

import inspect
from flask import Response, make_response

_temp_rule_cache = None

def route(rule, **options):
    """A decorator that is used to define custom routes for methods in
    FlaskView subclasses. The format is exactly the same as Flask's
    `@app.route` decorator.
    """

    def decorator(f):
        global _temp_rule_cache
        if _temp_rule_cache is None:
            _temp_rule_cache = {f.__name__: [(rule, options)]}
        elif not f.__name__ in _temp_rule_cache:
            _temp_rule_cache[f.__name__] = [(rule, options)]
        else:
            _temp_rule_cache[f.__name__].append((rule, options))

        return f

    return decorator

class _FlaskViewMeta(type):

    def __init__(cls, name, bases, dct):
        global _temp_rule_cache
        if _temp_rule_cache:
            cls._rule_cache = _temp_rule_cache
            _temp_rule_cache = None

class FlaskView(object):
    """Base view for any class based views implemented with Flask-Classy. Will
    automatically configure routes when registered with a Flask app instance.
    """

    __metaclass__ = _FlaskViewMeta

    @classmethod
    def register(cls, app, route_base=None, subdomain=None):
        """Registers a FlaskView class for use with a specific instance of a
        Flask app. Any methods not prefixes with an underscore are candidates
        to be routed and will have routes registered when this method is
        called.

        :param app: an instance of a Flask application

        :param route_base: The base path to use for all routes registered for
                           this class. Overrides the route_base attribute if
                           it has been set.

        :param subdomain:  A subdomain that this registration should use when
                           configuring routes.
        """

        if cls is FlaskView:
            raise TypeError("cls must be a subclass of FlaskVew, not FlaskView itself")

        if route_base:
            if hasattr(cls, "route_base"):
                cls.orig_route_base = cls.route_base

            cls.route_base = route_base

        if not subdomain:
            if hasattr(app, "subdomain") and app.subdomain != None:
                subdomain = app.subdomain
            elif hasattr(cls, "subdomain"):
                subdomain = cls.subdomain

        members = cls.find_member_methods()
        special_methods = ["get", "put", "patch", "post", "delete", "index"]
        id_methods = ["get", "put", "patch", "delete"]

        for name, value in members:
            proxy = cls.make_proxy_method(name)
            route_name = cls.build_route_name(name)

            if hasattr(cls, "_rule_cache") and name in cls._rule_cache:
                for idx, cached_rule in enumerate(cls._rule_cache[name]):
                    rule, options = cached_rule
                    rule = cls.build_rule(rule)
                    options = cls.configure_subdomain(options, subdomain)
                    app.add_url_rule(rule, "%s_%d" % (route_name, idx,), proxy, **options)

            elif name in special_methods:
                if name in ["get", "index"]:
                    methods = ["GET"]
                else:
                    methods = [name.upper()]

                if name in id_methods:
                    rule = "/<id>/"
                else:
                    rule = "/"
                rule = cls.build_rule(rule)
                #options = cls.configure_subdomain(dict(methods=methods), subdomain)
                app.add_url_rule(rule, route_name, proxy, methods=methods, subdomain=subdomain)

            else:
                rule = cls.build_rule('/%s/' % name, value,)
                app.add_url_rule(rule, route_name, proxy, subdomain=subdomain)

        if hasattr(cls, "orig_route_base"):
            cls.route_base = cls.orig_route_base
            del cls.orig_route_base



    @classmethod
    def configure_subdomain(cls, options, subdomain):
        if "subdomain" in options: return options

        options["subdomain"] = subdomain
        return options


    @classmethod
    def make_proxy_method(cls, name):
        """Creates a proxy function that can be used by Flasks routing. The
        proxy instantiates the FlaskView subclass and calls the appropriate
        method.

        :param name: the name of the method to create a proxy for
        """

        def proxy(*args, **kwargs):
            i = cls()
            if hasattr(i, "before_request"):
                i.before_request(name, *args, **kwargs)

            before_view_name = "before_" + name
            if hasattr(i, before_view_name):
                before_view = getattr(i, before_view_name)
                before_view(*args, **kwargs)

            view = getattr(i, name)
            response = view(*args, **kwargs)
            if not isinstance(response, Response):
                response = make_response(response)

            after_view_name = "after_" + name
            if hasattr(i, after_view_name):
                after_view = getattr(i, after_view_name)
                response = after_view(response)

            if hasattr(i, "after_request"):
                response = i.after_request(name, response)

            return response

        return proxy

    @classmethod
    def find_member_methods(cls):
        """Returns a list of methods that can be routed to"""

        base_members = dir(FlaskView)
        all_members = inspect.getmembers(cls, predicate=inspect.ismethod)
        return [member for member in all_members
                if not member[0] in base_members
                and not member[0].startswith("_")
                and not member[0].startswith("before_")
                and not member[0].startswith("after_")]

    @classmethod
    def build_rule(cls, rule, method=None):
        """Creates a routing rule based on either the class name (minus the
        'View' suffix) or the defined `route_base` attribute of the class

        :param rule: the path portion that should be appended to the
                     route base

        :param method: if a method's arguments should be considered when
                       constructing the rule, provide a reference to the
                       method here. arguments named "self" will be ignored
        """

        rule_parts = []
        route_base = cls.get_route_base()
        if route_base:
            rule_parts.append(route_base)

        rule = rule.strip("/")
        if rule:
            rule_parts.append(rule)

        if method:
            args = inspect.getargspec(method)[0]
            for arg in args:
                if arg != "self":
                    rule_parts.append("<%s>" % arg)

        return "/%s/" % "/".join(rule_parts)

    @classmethod
    def get_route_base(cls):
        """Returns the route base to use for the current class."""

        if hasattr(cls, "route_base"):
            route_base = cls.route_base
        else:
            if cls.__name__.endswith("View"):
                route_base = cls.__name__[:-4].lower()
            else:
                route_base = cls.__name__.lower()

        return route_base.strip("/")


    @classmethod
    def build_route_name(cls, method_name):
        """Creates a unique route name based on the combination of the class
        name with the method name.

        :param method_name: the method name to use when building a route name
        """
        return cls.__name__ + ":%s" % method_name