add admin.FilterPersistMiddleware
This commit is contained in:
parent
2fd4dcf548
commit
7a35745c84
4 changed files with 44 additions and 0 deletions
0
middleware/__init__.py
Normal file
0
middleware/__init__.py
Normal file
43
middleware/admin.py
Normal file
43
middleware/admin.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
# see http://code.djangoproject.com/ticket/3777
|
||||
|
||||
from django import http
|
||||
|
||||
class FilterPersistMiddleware(object):
|
||||
def process_request(self, request):
|
||||
if request.method != 'GET':
|
||||
return None
|
||||
|
||||
path = request.path
|
||||
if path.find('/admin/') != -1: # Dont waste time if we are not in admin
|
||||
query_string = request.META['QUERY_STRING']
|
||||
key = 'key' + path.replace('/','_')
|
||||
if not request.META.has_key('HTTP_REFERER'):
|
||||
return None
|
||||
if query_string: # Don't modify but save new queries
|
||||
request.session[key] = query_string
|
||||
return None
|
||||
|
||||
session = request.session
|
||||
if session.get('redirected', False): # so that we dont loop once redirected
|
||||
del session['redirected']
|
||||
return None
|
||||
|
||||
referrer = request.META['HTTP_REFERER'].split('?')[0]
|
||||
referrer = referrer[referrer.find('/admin'):len(referrer)]
|
||||
|
||||
if path == referrer: # We are in same page as before
|
||||
if query_string == '': # Filter is empty, delete it
|
||||
if session.get(key,False):
|
||||
del session[key]
|
||||
return None
|
||||
request.session[key] = query_string
|
||||
else: # We are are coming from another page, restore filter if available
|
||||
if session.get(key, False):
|
||||
query_string=request.session.get(key)
|
||||
redirect_to = path+'?'+query_string
|
||||
request.session['redirected'] = True
|
||||
return http.HttpResponseRedirect(redirect_to)
|
||||
else:
|
||||
return None
|
||||
else:
|
||||
return None
|
|
@ -70,6 +70,7 @@ MIDDLEWARE_CLASSES = (
|
|||
'django.middleware.common.CommonMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'texts.middleware.admin.FilterPersistMiddleware',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'texts.urls'
|
||||
|
|
BIN
settings.pyc
BIN
settings.pyc
Binary file not shown.
Loading…
Reference in a new issue