diff options
author | Tihomir Trifonov <t.trifonov@gmail.com> | 2014-06-17 00:06:51 +0300 |
---|---|---|
committer | Tihomir Trifonov <t.trifonov@gmail.com> | 2014-06-17 00:09:10 +0300 |
commit | 12d4a34ca4710b991805ae39a1f107594c0168ac (patch) | |
tree | 2e9f15e26d893de865c1c7eb01b06a12518e3112 /pecan/hooks.py | |
parent | 9bc09ea9c6d0cf311c508e6e0a4dce8df9855384 (diff) | |
download | pecan-12d4a34ca4710b991805ae39a1f107594c0168ac.tar.gz |
Added inheritance for hooks from parent classes
also added hook inheritance from mixins, and adding
hooks to child controllers, added as sub-controllers.
Fixes bug 1330673
Change-Id: I709cece7bcce26943b254b15dc8ddac5613b1202
Diffstat (limited to 'pecan/hooks.py')
-rw-r--r-- | pecan/hooks.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/pecan/hooks.py b/pecan/hooks.py index 4ceeb42..27dc872 100644 --- a/pecan/hooks.py +++ b/pecan/hooks.py @@ -14,6 +14,10 @@ __all__ = [ def walk_controller(root_class, controller, hooks): if not isinstance(controller, (int, dict)): + for hook in getattr(controller, '__hooks__', []): + # Append hooks from controller class definition + hooks.add(hook) + for name, value in getmembers(controller): if name == 'controller': continue @@ -22,7 +26,7 @@ def walk_controller(root_class, controller, hooks): if iscontroller(value): for hook in hooks: - value._pecan.setdefault('hooks', []).append(hook) + value._pecan.setdefault('hooks', set()).add(hook) elif hasattr(value, '__class__'): if name.startswith('__') and name.endswith('__'): continue @@ -37,7 +41,12 @@ class HookControllerMeta(type): ''' def __init__(cls, name, bases, dict_): - walk_controller(cls, cls, dict_.get('__hooks__', [])) + hooks = set(dict_.get('__hooks__', [])) + for base in bases: + # Add hooks from parent class and mixins + for hook in getattr(base, '__hooks__', []): + hooks.add(hook) + walk_controller(cls, cls, hooks) HookController = HookControllerMeta( |