1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # Author: limodou@gmail.com
# version: 0.1
# Url filter middleware
# Update:
# 0.1
from django.conf import settings
from utils.common import get_func
import re
class FilterMiddleware(object):
def process_request(self, request):
filter_items = getattr(settings, 'FILTERS', ())
for v in filter_items:
r, func = v
if not isinstance(r, (list, tuple)):
r = [r]
for p in r:
if isinstance(p, (str, unicode)):
p = re.compile(p)
m = p.match(request.path[1:])
if m:
kwargs = m.groupdict()
if kwargs:
args = ()
else:
args = m.groups()
return get_func(func)(request, *args, **kwargs)
|
More like this
- render_as_template template tag by cogat 4 years, 2 months ago
- Format transition middleware by limodou 6 years, 2 months ago
- Use express.js like url patterns by ekinertac 6 months ago
- CallTag - Just like include, but can pass parameters to it by limodou 6 years, 2 months ago
- integrated jinja2 which could use generic view ,my djangojinja2.py by jasongreen 3 years, 4 months ago
Comments
I guess re.compile(p) stuff should be in
__init__method. Is there is a special need to recompile filters for each request? (;#
Because I don't know if the instance of Middleware will be created per request, if it doesnot, put re.compile(p) in init will have problem. And the url in FILTERS can be not the same as urls.py(of cause they can be the same), and you can pass string or instance of re.compile() to it, if the object is string, this middleware will compile it first.
#
you could just use a new urlconf for that stuff
#
i think this plan is not the best~
如果楼主有心的方法,希望可以发生一份给我,谢谢!
jerry2801@gmail.com
#