Skip to content

Commit

Permalink
allow variable rules in route_base
Browse files Browse the repository at this point in the history
  • Loading branch information
uniphil committed Mar 26, 2013
1 parent 01b7a50 commit 6aa0248
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 7 additions & 1 deletion flask_classy.py
Expand Up @@ -12,6 +12,7 @@


import functools import functools
import inspect import inspect
from werkzeug.routing import parse_rule
from flask import request, Response, make_response from flask import request, Response, make_response
import re import re


Expand Down Expand Up @@ -210,11 +211,14 @@ def build_rule(cls, rule, method=None):


route_base = cls.get_route_base() route_base = cls.get_route_base()
rule_parts = [route_base, rule] rule_parts = [route_base, rule]
ignored_rule_args = ['self']
if hasattr(cls, 'base_args'):
ignored_rule_args += cls.base_args


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


result = "/%s" % "/".join(rule_parts) result = "/%s" % "/".join(rule_parts)
Expand All @@ -226,6 +230,8 @@ def get_route_base(cls):


if hasattr(cls, "route_base"): if hasattr(cls, "route_base"):
route_base = cls.route_base route_base = cls.route_base
base_rule = parse_rule(route_base)
cls.base_args = [r[2] for r in base_rule]
else: else:
if cls.__name__.endswith("View"): if cls.__name__.endswith("View"):
route_base = cls.__name__[:-4].lower() route_base = cls.__name__[:-4].lower()
Expand Down
2 changes: 1 addition & 1 deletion test_classy/test_endpoints.py
Expand Up @@ -38,4 +38,4 @@ def test_variable_route_popped_base():
def test_variable_route_base(): def test_variable_route_base():
with app.test_request_context(): with app.test_request_context():
url = url_for('VarBaseView:with_base_arg', route='bar') url = url_for('VarBaseView:with_base_arg', route='bar')
eq_('/var-base-route/bar/', url) eq_('/var-base-route/bar/with_base_arg/', url)
4 changes: 2 additions & 2 deletions test_classy/view_classes.py
Expand Up @@ -82,8 +82,8 @@ def before_index(self):
def index(self): def index(self):
return "Custom routed." return "Custom routed."


# def with_base_arg(self, route): def with_base_arg(self, route):
# return "Base route arg: " + route return "Base route arg: " + route


class BeforeRequestView(FlaskView): class BeforeRequestView(FlaskView):


Expand Down

0 comments on commit 6aa0248

Please sign in to comment.