From 6aa02481bc9176de951a5b07573043a4d8e6616f Mon Sep 17 00:00:00 2001 From: Philip Schleihauf Date: Mon, 25 Mar 2013 23:49:42 -0400 Subject: [PATCH] allow variable rules in route_base --- flask_classy.py | 8 +++++++- test_classy/test_endpoints.py | 2 +- test_classy/view_classes.py | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/flask_classy.py b/flask_classy.py index 36568e8..0ab01d8 100644 --- a/flask_classy.py +++ b/flask_classy.py @@ -12,6 +12,7 @@ import functools import inspect +from werkzeug.routing import parse_rule from flask import request, Response, make_response import re @@ -210,11 +211,14 @@ def build_rule(cls, rule, method=None): route_base = cls.get_route_base() rule_parts = [route_base, rule] + ignored_rule_args = ['self'] + if hasattr(cls, 'base_args'): + ignored_rule_args += cls.base_args if method: args = inspect.getargspec(method)[0] for arg in args: - if arg != "self": + if arg not in ignored_rule_args: rule_parts.append("<%s>" % arg) result = "/%s" % "/".join(rule_parts) @@ -226,6 +230,8 @@ def get_route_base(cls): if hasattr(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: if cls.__name__.endswith("View"): route_base = cls.__name__[:-4].lower() diff --git a/test_classy/test_endpoints.py b/test_classy/test_endpoints.py index 99de697..c196dc4 100644 --- a/test_classy/test_endpoints.py +++ b/test_classy/test_endpoints.py @@ -38,4 +38,4 @@ def test_variable_route_popped_base(): def test_variable_route_base(): with app.test_request_context(): url = url_for('VarBaseView:with_base_arg', route='bar') - eq_('/var-base-route/bar/', url) + eq_('/var-base-route/bar/with_base_arg/', url) diff --git a/test_classy/view_classes.py b/test_classy/view_classes.py index 4db2c49..077262b 100644 --- a/test_classy/view_classes.py +++ b/test_classy/view_classes.py @@ -82,8 +82,8 @@ def before_index(self): def index(self): return "Custom routed." - # def with_base_arg(self, route): - # return "Base route arg: " + route + def with_base_arg(self, route): + return "Base route arg: " + route class BeforeRequestView(FlaskView):