From bca2c10f326cd60f4357fad2d073130c5806a177 Mon Sep 17 00:00:00 2001 From: Roberto Aguilar Date: Wed, 31 Jul 2013 16:06:45 -0700 Subject: [PATCH] Added ability to add decorators to view. the `decorate_with` tuple can be set on a view subclass or be passed into the patterns method. Any items in the tuple will be applied to the view in the order they are given. --- resourceful/views.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/resourceful/views.py b/resourceful/views.py index 84e3b2d..cd64d12 100644 --- a/resourceful/views.py +++ b/resourceful/views.py @@ -33,6 +33,7 @@ class ResourceView(View): url_prefix = None template_dir = None serialize_fields = None # When None default fields are serialized + decorate_with = () # Decorators for the view query_map = {} def __init__(self, **kwargs): @@ -430,11 +431,12 @@ def patterns_for(cls, model_class=None, template_dir=None, url_prefix=None, **kw cls.patterns(model_class=model_class, template_dir=template_dir, url_prefix=url_prefix, **kwargs) @classmethod - def patterns(cls, model_class=None, template_dir=None, url_prefix=None, **kwargs): + def patterns(cls, model_class=None, template_dir=None, url_prefix=None, decorate_with=None, **kwargs): # in case the model_class is defined in a subclass, but the parameter takes precedence anyway. model_class = model_class or cls.model_class url_prefix = url_prefix or cls.url_prefix template_dir = template_dir or cls.template_dir + decorate_with = decorate_with or cls.decorate_with if isinstance(model_class, six.string_types): t_app_label, t_model_name = model_class.split('.', 1) @@ -467,6 +469,10 @@ def patterns(cls, model_class=None, template_dir=None, url_prefix=None, **kwargs **kwargs ) + # apply all decroators to the view + for decorator in decorate_with: + view = decorator(view) + urlpatterns = patterns( '',