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

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

persistent list_filter patch

  • django/contrib/admin/filterspecs.py

     
    5555        else:
    5656            self.lookup_title = f.verbose_name
    5757        self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to._meta.pk.name)
    58         self.lookup_val = request.GET.get(self.lookup_kwarg, None)
     58        self.lookup_val = params.get(self.lookup_kwarg, None)
    5959        self.lookup_choices = f.rel.to._default_manager.all()
    6060
    6161    def has_output(self):
     
    8080    def __init__(self, f, request, params, model):
    8181        super(ChoicesFilterSpec, self).__init__(f, request, params, model)
    8282        self.lookup_kwarg = '%s__exact' % f.name
    83         self.lookup_val = request.GET.get(self.lookup_kwarg, None)
     83        self.lookup_val = params.get(self.lookup_kwarg, None)
    8484
    8585    def choices(self, cl):
    8686        yield {'selected': self.lookup_val is None,
     
    133133        super(BooleanFieldFilterSpec, self).__init__(f, request, params, model)
    134134        self.lookup_kwarg = '%s__exact' % f.name
    135135        self.lookup_kwarg2 = '%s__isnull' % f.name
    136         self.lookup_val = request.GET.get(self.lookup_kwarg, None)
    137         self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
     136        self.lookup_val = params.get(self.lookup_kwarg, None)
     137        self.lookup_val2 = params.get(self.lookup_kwarg2, None)
    138138
    139139    def title(self):
    140140        return self.field.verbose_name
     
    157157class AllValuesFilterSpec(FilterSpec):
    158158    def __init__(self, f, request, params, model):
    159159        super(AllValuesFilterSpec, self).__init__(f, request, params, model)
    160         self.lookup_val = request.GET.get(f.name, None)
     160        self.lookup_val = params.get(f.name, None)
    161161        self.lookup_choices = model._meta.admin.manager.distinct().order_by(f.name).values(f.name)
    162162
    163163    def title(self):
  • django/contrib/admin/views/main.py

     
    1212from django.http import Http404, HttpResponse, HttpResponseRedirect
    1313from django.utils.html import escape
    1414from django.utils.text import capfirst, get_text_list
     15from django.conf import settings
    1516import operator
    1617
     18
    1719from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
    1820if not LogEntry._meta.installed:
    1921    raise ImproperlyConfigured, "You'll need to put 'django.contrib.admin' in your INSTALLED_APPS setting before you can use the admin application."
     
    3335SEARCH_VAR = 'q'
    3436IS_POPUP_VAR = 'pop'
    3537ERROR_FLAG = 'e'
     38SESSION_RESTORE_VAR = 'sr'
    3639
    3740# Text to display within change-list table cells if the value is blank.
    3841EMPTY_CHANGELIST_VALUE = '(None)'
     
    235238    if post_url is None:
    236239        if request.user.has_perm(app_label + '.' + opts.get_change_permission()):
    237240            # redirect to list view
    238             post_url = '../'
     241            post_url = '../?sr'
    239242        else:
    240243            # Object list will give 'Permission Denied', so go back to admin home
    241244            post_url = '../../../'
     
    357360                return HttpResponseRedirect("../add/")
    358361            else:
    359362                request.user.message_set.create(message=msg)
    360                 return HttpResponseRedirect("../")
     363                return HttpResponseRedirect("../?sr")
    361364    else:
    362365        # Populate new_data with a "flattened" version of the current data.
    363366        new_data = manipulator.flatten_data()
     
    512515        obj.delete()
    513516        LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, object_id, obj_display, DELETION)
    514517        request.user.message_set.create(message=_('The %(name)s "%(obj)s" was deleted successfully.') % {'name': opts.verbose_name, 'obj': obj_display})
    515         return HttpResponseRedirect("../../")
     518        return HttpResponseRedirect("../../?sr")
    516519    extra_context = {
    517520        "title": _("Are you sure?"),
    518521        "object_name": opts.verbose_name,
     
    554557        self.manager = self.opts.admin.manager
    555558
    556559        # Get search parameters from the query string.
     560        self.set_params(request, model)
     561        self.order_field, self.order_type = self.get_ordering()
     562        self.query_set = self.get_query_set()
     563        self.get_results(request)
     564        self.title = (self.is_popup and _('Select %s') % self.opts.verbose_name or _('Select %s to change') % self.opts.verbose_name)
     565        self.filter_specs, self.has_filters = self.get_filters(request)
     566        self.pk_attname = self.lookup_opts.pk.attname
     567
     568    def set_params(self, request, model):
     569        query = request.GET.copy()
     570        restore = SESSION_RESTORE_VAR in request.GET
     571        sessions = 'django.contrib.sessions' in settings.INSTALLED_APPS
     572
     573        # restore from session if requested
     574        if sessions:
     575            saved = request.session.setdefault('change_list_params', {}) \
     576                .setdefault(model._meta.app_label,{}) \
     577                .setdefault(model._meta.object_name, {})
     578            if restore:
     579                for key in saved:
     580                    query.setdefault(key, saved[key])
    557581        try:
    558             self.page_num = int(request.GET.get(PAGE_VAR, 0))
     582            self.page_num = int(query.get(PAGE_VAR, 0))
    559583        except ValueError:
    560584            self.page_num = 0
    561         self.show_all = request.GET.has_key(ALL_VAR)
    562         self.is_popup = request.GET.has_key(IS_POPUP_VAR)
    563         self.params = dict(request.GET.items())
     585        self.show_all = query.has_key(ALL_VAR)
     586        self.is_popup = query.has_key(IS_POPUP_VAR)
     587        self.params = dict(query.items())
     588
    564589        if self.params.has_key(PAGE_VAR):
    565590            del self.params[PAGE_VAR]
    566591        if self.params.has_key(ERROR_FLAG):
    567592            del self.params[ERROR_FLAG]
     593        if restore:
     594            del self.params[SESSION_RESTORE_VAR]
     595        self.query = query.get(SEARCH_VAR, '')
    568596
    569         self.order_field, self.order_type = self.get_ordering()
    570         self.query = request.GET.get(SEARCH_VAR, '')
    571         self.query_set = self.get_query_set()
    572         self.get_results(request)
    573         self.title = (self.is_popup and _('Select %s') % self.opts.verbose_name or _('Select %s to change') % self.opts.verbose_name)
    574         self.filter_specs, self.has_filters = self.get_filters(request)
    575         self.pk_attname = self.lookup_opts.pk.attname
     597        # save session
     598        if sessions:
     599            # make sure session gets saved
     600            request.session['change_list_params_changed'] = 1
     601            for key in self.params:
     602                saved[key] = self.params[key]
    576603
    577604    def get_filters(self, request):
    578605        filter_specs = []
  • django/contrib/sessions/middleware.py

     
    3636    def get(self, key, default=None):
    3737        return self._session.get(key, default)
    3838
     39    def setdefault(self, key, default):
     40        if not key in self:
     41            self[key] = default
     42        return self[key]
     43
    3944    def set_test_cookie(self):
    4045        self[TEST_COOKIE_NAME] = TEST_COOKIE_VALUE
    4146
  • django/contrib/admin/templates/admin/change_form.html

     
    1111{% block breadcrumbs %}{% if not is_popup %}
    1212<div class="breadcrumbs">
    1313     <a href="../../../">{% trans "Home" %}</a> &rsaquo;
    14      <a href="../">{{ opts.verbose_name_plural|capfirst|escape }}</a> &rsaquo;
     14     <a href="../?sr">{{ opts.verbose_name_plural|capfirst|escape }}</a> &rsaquo;
    1515     {% if add %}{% trans "Add" %} {{ opts.verbose_name|escape }}{% else %}{{ original|truncatewords:"18"|escape }}{% endif %}
    1616</div>
    1717{% endif %}{% endblock %}
Back to Top