Django

Code

Changeset 5619

Show
Ignore:
Timestamp:
07/05/07 06:10:27 (1 year ago)
Author:
mtredinnick
Message:

Fixed #1015 -- Fixed decorator_from_middleware to return a real decorator even
when arguments are given. This looks a bit ugly, but it's fully backwards
compatible and all the extra work is done at import time, so it shouldn't have
any real performance impact.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/decorators.py

    r4265 r5619  
    11"Functions that help with dynamically creating decorators for views." 
     2 
     3import types 
    24 
    35def decorator_from_middleware(middleware_class): 
     
    68    lets you use middleware functionality on a per-view basis. 
    79    """ 
    8     def _decorator_from_middleware(view_func, *args, **kwargs): 
     10    def _decorator_from_middleware(*args, **kwargs): 
     11        has_func = True 
     12        try: 
     13            view_func = kwargs.pop('view_func') 
     14        except KeyError: 
     15            if len(args): 
     16                view_func, args = args[0], args[1:] 
     17            else: 
     18                has_func = False 
     19        if not (has_func and isinstance(view_func, types.FunctionType)): 
     20            # For historical reasons, these decorators are also called as 
     21            # dec(func, *args) instead of dec(*args)(func). This branch handles 
     22            # the backwards compatibility. 
     23            if has_func: 
     24                args = (view_func,) + args 
     25            middleware = middleware_class(*args, **kwargs) 
     26 
     27            def decorator_func(fn): 
     28                return _decorator_from_middleware(fn, *args, **kwargs) 
     29            return decorator_func 
     30 
    931        middleware = middleware_class(*args, **kwargs) 
     32 
    1033        def _wrapped_view(request, *args, **kwargs): 
    1134            if hasattr(middleware, 'process_request'):