Login

Simple View Middleware to allow a Prefilter

Author:
tclineks
Posted:
April 22, 2008
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

This allows you to define a 'prefilter' function in your view modules which will be invoked before any view in that same. This provides an easy place to decorate the request or modify arguments.

For simplicity it doesn't allow configuration of the name of the prefilter function. I also skipped recursing into parent modules since that's somewhat edgecase.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from sys import modules

class ViewModulePrefilter(object):
    """Simple Django View Middleware to allow a prefilter function in view modules"""
    def process_view(self, request, view_func, view_args, view_kwargs):
        module = modules[view_func.__module__]
        prefilter_func_name = 'prefilter'
        if hasattr(module, prefilter_func_name):
            prefilter_func = getattr(module, prefilter_func_name)
            response = prefilter_func(request, view_func, view_args, view_kwargs)
            if response:
                return response

More like this

  1. get_object_or_none by azwdevops 3 months, 2 weeks ago
  2. Mask sensitive data from logger by agusmakmun 5 months, 2 weeks ago
  3. Template tag - list punctuation for a list of items by shapiromatron 1 year, 7 months ago
  4. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 7 months ago
  5. Serializer factory with Django Rest Framework by julio 2 years, 2 months ago

Comments

Please login first before commenting.