Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions flask_classy.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FlaskView(object):

@classmethod
def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
trailing_slash=None):
trailing_slash=None, instance=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
Expand All @@ -70,6 +70,8 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
:param route_prefix: A prefix to be applied to all routes registered
for this class. Precedes route_base. Overrides
the class' route_prefix if it has been set.

:param instance: a previous instance of the class
"""

if cls is FlaskView:
Expand Down Expand Up @@ -98,7 +100,7 @@ def register(cls, app, route_base=None, subdomain=None, route_prefix=None,
special_methods = ["get", "put", "patch", "post", "delete", "index"]

for name, value in members:
proxy = cls.make_proxy_method(name)
proxy = cls.make_proxy_method(name, instance)
route_name = cls.build_route_name(name)
try:
if hasattr(value, "_rule_cache") and name in value._rule_cache:
Expand Down Expand Up @@ -163,15 +165,18 @@ def parse_options(cls, options):


@classmethod
def make_proxy_method(cls, name):
def make_proxy_method(cls, name, instance=None):
"""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
:param instance: a previous instance of the class
"""

i = cls()
if instance is None:
i = cls()
else:
i = instance
view = getattr(i, name)

if cls.decorators:
Expand Down