Please provide suggestions on refining this code.
Thanks.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | from django import http
from django.http import Http404
from django.core import urlresolvers
from urlparse import urlparse
class AdminSessionMiddleware(object):
"""
The purpose of this middleware is to automatically save the values of all filters for each
admin changelist page so that when a user leaves a changelist page request and then comes
back to it, the filter is still there.
This middleware must come after the session middleware.
Add this middleware to MIDDLEWARE_CLASSES in your settings.py
This middleware will then automatically save the values of all filters in the admin changelist
for all models.
"""
def process_request(self, request):
"""
"""
try:
current_url = urlresolvers.resolve(request.path)
except Http404:
return
if not current_url.namespace == 'admin' or \
not current_url.view_name.endswith('_changelist'):
return
referer_parsed = urlparse(unicode(request.META.get("HTTP_REFERER",None)))
if request.path == referer_parsed.path:
# save session and return if the same page has been submitted.
request.session[current_url.view_name] = unicode(request.META.get('QUERY_STRING',None))
return
else:
admin_session = request.session.get(current_url.view_name,None)
q = unicode(request.META.get('QUERY_STRING',None))
if admin_session and admin_session == q:
return
if admin_session:
new_url = request.path + '?' + admin_session
return http.HttpResponsePermanentRedirect(new_url)
return
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Works really well, thanks! Not much needed to be done.
#
Please login first before commenting.