Save Admin ChangeList Filter

 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. Return to a filtered changelist on change form save by richardbolt 3 years, 8 months ago
  2. Accordion changelist admin by rodnsi 4 years, 4 months ago
  3. Action that respects the filters in changeview_list by grillermo 1 year, 6 months ago
  4. Changelist filter by ForeignKey by overclocked 3 years, 5 months ago
  5. Accordion changelist admin by djm 3 years, 4 months ago

Comments

infinixd (on October 31, 2012):

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

#

(Forgotten your password?)