Code

Ticket #3777: admin-filters-in-session.diff

File admin-filters-in-session.diff, 8.0 KB (added by matt <matt.barry@…>, 7 years ago)

persistent list_filter patch

Line 
1Index: django/contrib/admin/filterspecs.py
2===================================================================
3--- django/contrib/admin/filterspecs.py (revision 4767)
4+++ django/contrib/admin/filterspecs.py (working copy)
5@@ -55,7 +55,7 @@
6         else:
7             self.lookup_title = f.verbose_name
8         self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to._meta.pk.name)
9-        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
10+        self.lookup_val = params.get(self.lookup_kwarg, None)
11         self.lookup_choices = f.rel.to._default_manager.all()
12 
13     def has_output(self):
14@@ -80,7 +80,7 @@
15     def __init__(self, f, request, params, model):
16         super(ChoicesFilterSpec, self).__init__(f, request, params, model)
17         self.lookup_kwarg = '%s__exact' % f.name
18-        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
19+        self.lookup_val = params.get(self.lookup_kwarg, None)
20 
21     def choices(self, cl):
22         yield {'selected': self.lookup_val is None,
23@@ -133,8 +133,8 @@
24         super(BooleanFieldFilterSpec, self).__init__(f, request, params, model)
25         self.lookup_kwarg = '%s__exact' % f.name
26         self.lookup_kwarg2 = '%s__isnull' % f.name
27-        self.lookup_val = request.GET.get(self.lookup_kwarg, None)
28-        self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
29+        self.lookup_val = params.get(self.lookup_kwarg, None)
30+        self.lookup_val2 = params.get(self.lookup_kwarg2, None)
31 
32     def title(self):
33         return self.field.verbose_name
34@@ -157,7 +157,7 @@
35 class AllValuesFilterSpec(FilterSpec):
36     def __init__(self, f, request, params, model):
37         super(AllValuesFilterSpec, self).__init__(f, request, params, model)
38-        self.lookup_val = request.GET.get(f.name, None)
39+        self.lookup_val = params.get(f.name, None)
40         self.lookup_choices = model._meta.admin.manager.distinct().order_by(f.name).values(f.name)
41 
42     def title(self):
43Index: django/contrib/admin/views/main.py
44===================================================================
45--- django/contrib/admin/views/main.py  (revision 4767)
46+++ django/contrib/admin/views/main.py  (working copy)
47@@ -12,8 +12,10 @@
48 from django.http import Http404, HttpResponse, HttpResponseRedirect
49 from django.utils.html import escape
50 from django.utils.text import capfirst, get_text_list
51+from django.conf import settings
52 import operator
53 
54+
55 from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
56 if not LogEntry._meta.installed:
57     raise ImproperlyConfigured, "You'll need to put 'django.contrib.admin' in your INSTALLED_APPS setting before you can use the admin application."
58@@ -33,6 +35,7 @@
59 SEARCH_VAR = 'q'
60 IS_POPUP_VAR = 'pop'
61 ERROR_FLAG = 'e'
62+SESSION_RESTORE_VAR = 'sr'
63 
64 # Text to display within change-list table cells if the value is blank.
65 EMPTY_CHANGELIST_VALUE = '(None)'
66@@ -235,7 +238,7 @@
67     if post_url is None:
68         if request.user.has_perm(app_label + '.' + opts.get_change_permission()):
69             # redirect to list view
70-            post_url = '../'
71+            post_url = '../?sr'
72         else:
73             # Object list will give 'Permission Denied', so go back to admin home
74             post_url = '../../../'
75@@ -357,7 +360,7 @@
76                 return HttpResponseRedirect("../add/")
77             else:
78                 request.user.message_set.create(message=msg)
79-                return HttpResponseRedirect("../")
80+                return HttpResponseRedirect("../?sr")
81     else:
82         # Populate new_data with a "flattened" version of the current data.
83         new_data = manipulator.flatten_data()
84@@ -512,7 +515,7 @@
85         obj.delete()
86         LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, object_id, obj_display, DELETION)
87         request.user.message_set.create(message=_('The %(name)s "%(obj)s" was deleted successfully.') % {'name': opts.verbose_name, 'obj': obj_display})
88-        return HttpResponseRedirect("../../")
89+        return HttpResponseRedirect("../../?sr")
90     extra_context = {
91         "title": _("Are you sure?"),
92         "object_name": opts.verbose_name,
93@@ -554,25 +557,49 @@
94         self.manager = self.opts.admin.manager
95 
96         # Get search parameters from the query string.
97+        self.set_params(request, model)
98+        self.order_field, self.order_type = self.get_ordering()
99+        self.query_set = self.get_query_set()
100+        self.get_results(request)
101+        self.title = (self.is_popup and _('Select %s') % self.opts.verbose_name or _('Select %s to change') % self.opts.verbose_name)
102+        self.filter_specs, self.has_filters = self.get_filters(request)
103+        self.pk_attname = self.lookup_opts.pk.attname
104+
105+    def set_params(self, request, model):
106+        query = request.GET.copy()
107+        restore = SESSION_RESTORE_VAR in request.GET
108+        sessions = 'django.contrib.sessions' in settings.INSTALLED_APPS
109+
110+        # restore from session if requested
111+        if sessions:
112+            saved = request.session.setdefault('change_list_params', {}) \
113+                .setdefault(model._meta.app_label,{}) \
114+                .setdefault(model._meta.object_name, {})
115+            if restore:
116+                for key in saved:
117+                    query.setdefault(key, saved[key])
118         try:
119-            self.page_num = int(request.GET.get(PAGE_VAR, 0))
120+            self.page_num = int(query.get(PAGE_VAR, 0))
121         except ValueError:
122             self.page_num = 0
123-        self.show_all = request.GET.has_key(ALL_VAR)
124-        self.is_popup = request.GET.has_key(IS_POPUP_VAR)
125-        self.params = dict(request.GET.items())
126+        self.show_all = query.has_key(ALL_VAR)
127+        self.is_popup = query.has_key(IS_POPUP_VAR)
128+        self.params = dict(query.items())
129+
130         if self.params.has_key(PAGE_VAR):
131             del self.params[PAGE_VAR]
132         if self.params.has_key(ERROR_FLAG):
133             del self.params[ERROR_FLAG]
134+        if restore:
135+            del self.params[SESSION_RESTORE_VAR]
136+        self.query = query.get(SEARCH_VAR, '')
137 
138-        self.order_field, self.order_type = self.get_ordering()
139-        self.query = request.GET.get(SEARCH_VAR, '')
140-        self.query_set = self.get_query_set()
141-        self.get_results(request)
142-        self.title = (self.is_popup and _('Select %s') % self.opts.verbose_name or _('Select %s to change') % self.opts.verbose_name)
143-        self.filter_specs, self.has_filters = self.get_filters(request)
144-        self.pk_attname = self.lookup_opts.pk.attname
145+        # save session
146+        if sessions:
147+            # make sure session gets saved
148+            request.session['change_list_params_changed'] = 1
149+            for key in self.params:
150+                saved[key] = self.params[key]
151 
152     def get_filters(self, request):
153         filter_specs = []
154Index: django/contrib/sessions/middleware.py
155===================================================================
156--- django/contrib/sessions/middleware.py       (revision 4767)
157+++ django/contrib/sessions/middleware.py       (working copy)
158@@ -36,6 +36,11 @@
159     def get(self, key, default=None):
160         return self._session.get(key, default)
161 
162+    def setdefault(self, key, default):
163+        if not key in self:
164+            self[key] = default
165+        return self[key]
166+
167     def set_test_cookie(self):
168         self[TEST_COOKIE_NAME] = TEST_COOKIE_VALUE
169 
170Index: django/contrib/admin/templates/admin/change_form.html
171===================================================================
172--- django/contrib/admin/templates/admin/change_form.html       (revision 4767)
173+++ django/contrib/admin/templates/admin/change_form.html       (working copy)
174@@ -11,7 +11,7 @@
175 {% block breadcrumbs %}{% if not is_popup %}
176 <div class="breadcrumbs">
177      <a href="../../../">{% trans "Home" %}</a> &rsaquo;
178-     <a href="../">{{ opts.verbose_name_plural|capfirst|escape }}</a> &rsaquo;
179+     <a href="../?sr">{{ opts.verbose_name_plural|capfirst|escape }}</a> &rsaquo;
180      {% if add %}{% trans "Add" %} {{ opts.verbose_name|escape }}{% else %}{{ original|truncatewords:"18"|escape }}{% endif %}
181 </div>
182 {% endif %}{% endblock %}