Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvements to FlaskResource #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
22 changes: 19 additions & 3 deletions restless/fl.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ def _wrapper(*args, **kwargs):

return _wrapper

@classmethod
def as_view(cls, route, *init_args, **init_kwargs):
# Overridden here, because Flask uses a global ``request`` object
# rather than passing it to each view.
def _wrapper(*args, **kwargs):
# Make a new instance so that no state potentially leaks between
# instances.
inst = cls(*init_args, **init_kwargs)
inst.request = request
return inst.handle(route, *args, **kwargs)

return _wrapper

def request_body(self):
return self.request.data

Expand Down Expand Up @@ -76,7 +89,7 @@ def build_endpoint_name(cls, name, endpoint_prefix=None):
return '_'.join([endpoint_prefix, name])

@classmethod
def add_url_rules(cls, app, rule_prefix, endpoint_prefix=None):
def add_url_rules(cls, app, rule_prefix, endpoint_prefix=None, pk='pk', pk_type='int'):
"""
A convenience method for hooking up the URLs.

Expand All @@ -98,14 +111,17 @@ def add_url_rules(cls, app, rule_prefix, endpoint_prefix=None):
"""
methods = ['GET', 'POST', 'PUT', 'DELETE']

collection_rule = rule_prefix
app.add_url_rule(
rule_prefix,
collection_rule,
endpoint=cls.build_endpoint_name('list', endpoint_prefix),
view_func=cls.as_list(),
methods=methods
)

instance_rule = '{}<{}:{}>/'.format(rule_prefix, pk_type, pk)
app.add_url_rule(
rule_prefix + '<pk>/',
instance_rule,
endpoint=cls.build_endpoint_name('detail', endpoint_prefix),
view_func=cls.as_detail(),
methods=methods
Expand Down