Login

Save Admin ChangeList Filter

Author:
msaron
Posted:
January 11, 2012
Language:
Python
Version:
1.3
Score:
1 (after 1 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 3 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

infinixd (on October 31, 2012):

Works really well, thanks! Not much needed to be done.

#

Please login first before commenting.