From 5cac1a194435e7b92159c343211dad1a05086b59 Mon Sep 17 00:00:00 2001 From: Ivan Kleshnin Date: Tue, 11 Jun 2013 18:47:57 +0300 Subject: [PATCH] Fix Issue #22 (rule lookup does not count inheritance) Does pass tests --- flask_classy.py | 56 +++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/flask_classy.py b/flask_classy.py index 1d18994..46f04f5 100644 --- a/flask_classy.py +++ b/flask_classy.py @@ -94,37 +94,39 @@ def register(cls, app, route_base=None, subdomain=None): proxy = cls.make_proxy_method(name) route_name = cls.build_route_name(name) try: - 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) - sub, ep, options = cls.parse_options(options) - - if not subdomain and sub: - subdomain = sub - - if ep: - endpoint = ep - elif len(cls._rule_cache[name]) == 1: - endpoint = route_name + for _cls in cls.mro()[:-1]: + 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) + sub, ep, options = _cls.parse_options(options) + + if not subdomain and sub: + subdomain = sub + + if ep: + endpoint = ep + elif len(_cls._rule_cache[name]) == 1: + endpoint = route_name + else: + endpoint = "%s_%d" % (route_name, idx,) + + app.add_url_rule(rule, endpoint, proxy, subdomain=subdomain, **options) + break + else: + if name in special_methods: + if name in ["get", "index"]: + methods = ["GET"] else: - endpoint = "%s_%d" % (route_name, idx,) - - app.add_url_rule(rule, endpoint, proxy, subdomain=subdomain, **options) - - elif name in special_methods: - if name in ["get", "index"]: - methods = ["GET"] - else: - methods = [name.upper()] + methods = [name.upper()] - rule = cls.build_rule("/", value) + rule = cls.build_rule("/", value) - app.add_url_rule(rule, route_name, proxy, methods=methods, subdomain=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) + else: + rule = cls.build_rule('/%s/' % name, value,) + app.add_url_rule(rule, route_name, proxy, subdomain=subdomain) except DecoratorCompatibilityError: raise DecoratorCompatibilityError("Incompatible decorator detected on %s in class %s" % (name, cls.__name__))