Ticket #17828: 17828-debug-IncorrectLookupParameters.patch

File 17828-debug-IncorrectLookupParameters.patch, 4.7 KB (added by Chris Adams, 13 years ago)

GitHub-generated patch

  • django/contrib/admin/options.py

    From 79334d5e60b1a83dbdd09fce456019ed6609e2e4 Mon Sep 17 00:00:00 2001
    From: Chris Adams <chris@improbable.org>
    Date: Wed, 14 Mar 2012 10:51:52 -0700
    Subject: [PATCH] Standard debugging for changelist (see #17828)
    
    Previously all exceptions in ChangeList initialization were squashed down to an
    IncorrectLookupParameters exception without context. Now when settings.DEBUG is
    defined the original exception will be passed along to make debugging easier and
    avoid a frequently misleading generic error message.
    ---
     django/contrib/admin/options.py                 |    2 +
     django/contrib/admin/views/main.py              |    7 ++++-
     tests/regressiontests/admin_changelist/tests.py |   33 +++++++++++++++++++++++
     3 files changed, 41 insertions(+), 1 deletions(-)
    
    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index f5f6256..8c5b059 100644
    a b def changelist_view(self, request, extra_context=None):  
    11351135            # and the 'invalid=1' parameter was already in the query string,
    11361136            # something is screwed up with the database, so display an error
    11371137            # page.
     1138            if settings.DEBUG:
     1139                raise  # We'll let the standard debugging view handle this
    11381140            if ERROR_FLAG in request.GET.keys():
    11391141                return SimpleTemplateResponse('admin/invalid_setup.html', {
    11401142                    'title': _('Database error'),
  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    index 56f13f8..30f65ec 100644
    a b  
    11import operator
    22
     3from django.conf import settings
    34from django.core.exceptions import SuspiciousOperation, ImproperlyConfigured
    45from django.core.paginator import InvalidPage
    56from django.db import models
    def get_query_set(self, request):  
    319320            # are not in the correct type, so we might get FieldError,
    320321            # ValueError, ValidationError, or ? from a custom field that raises
    321322            # yet something else when handed impossible data.
    322             raise IncorrectLookupParameters(e)
     323            if settings.DEBUG:
     324                # Re-raise the original exception to preserve debug information
     325                raise
     326            else:
     327                raise IncorrectLookupParameters(e)
    323328
    324329        # Use select_related() if one of the list_display options is a field
    325330        # with a relationship and the provided queryset doesn't already have
  • tests/regressiontests/admin_changelist/tests.py

    diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py
    index d2b017b..03b334e 100644
    a b  
    11from __future__ import with_statement, absolute_import
    22
     3from django.conf import settings
    34from django.contrib import admin
    45from django.contrib.admin.options import IncorrectLookupParameters
    56from django.contrib.admin.views.main import ChangeList, SEARCH_VAR, ALL_VAR
    def test_result_list_editable(self):  
    142143                    m.list_filter, m.date_hierarchy, m.search_fields,
    143144                    m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m))
    144145
     146    def test_result_list_editable(self):
     147        """
     148        Regression test for #17828: ensure that settings.DEBUG causes
     149        exceptions in ChangeList setup to be re-raised rather than replaced
     150        with a generic IncorrectLookupParameters instance
     151        """
     152
     153        request = self.factory.get('/child/')
     154        m = ChildAdmin(Child, admin.site)
     155
     156        class BrokenFilter(admin.SimpleListFilter):
     157            title = "Broken Filter"
     158            parameter_name = 'broken_filter'
     159
     160            # Note the absense of the mandatory methods, which will trigger an exception
     161
     162        def broken_changelist_init():
     163            ChangeList(request, Child, m.list_display, m.list_display_links,
     164                        m.list_filter + (BrokenFilter, ), m.date_hierarchy,
     165                        m.search_fields, m.list_select_related,
     166                        m.list_per_page, m.list_max_show_all, m.list_editable,
     167                        m)
     168
     169        ORIG_DEBUG = settings.DEBUG
     170        try:
     171            settings.DEBUG = False
     172            self.assertRaises(IncorrectLookupParameters, broken_changelist_init)
     173            settings.DEBUG = True
     174            self.assertRaises(NotImplementedError, broken_changelist_init)
     175        finally:
     176            settings.DEBUG = ORIG_DEBUG
     177
    145178    def test_custom_paginator(self):
    146179        new_parent = Parent.objects.create(name='parent')
    147180        for i in range(200):
Back to Top