Ticket #9749: patch_django_9749.20090323.diff

File patch_django_9749.20090323.diff, 7.3 KB (added by David Larlet, 16 years ago)

Update against r10123 with feature freeze it will be more simple to maintain now

  • django/contrib/admin/validation.py

     
    55
    66from django.core.exceptions import ImproperlyConfigured
    77from django.db import models
    88from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model
    99from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin
    1010from django.contrib.admin.options import HORIZONTAL, VERTICAL
     11from django.contrib.admin.views.main import ChangeList
    1112
    1213__all__ = ['validate']
    1314
     
    158159                raise ImproperlyConfigured("'%s.inlines[%d].model' does not "
    159160                        "inherit from models.Model." % (cls.__name__, idx))
    160161            validate_base(inline, inline.model)
    161             validate_inline(inline)
     162            validate_inline(inline, cls, model)
    162163
    163 def validate_inline(cls):
     164    # changelist_class = ChangeList
     165    if hasattr(cls, 'changelist_class') and not issubclass(cls.changelist_class, ChangeList):
     166        raise ImproperlyConfigured("'%s.changelist_class' does not inherit "
     167                        "from admin.views.main.ChangeList." % cls.__name__)
     168
     169def validate_inline(cls, parent, parent_model):
    164170    # model is already verified to exist and be a Model
    165171    if cls.fk_name: # default value is None
    166172        f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name)
  • django/contrib/admin/options.py

     
     
    172172
    173173class ModelAdmin(BaseModelAdmin):
    174174    "Encapsulates all admin options and functionality for a given model."
     175    # Avoid circular import of ChangeList
     176    from django.contrib.admin.views.main import ChangeList
     177   
    175178    __metaclass__ = forms.MediaDefiningClass
    176179
    177180    list_display = ('__str__',)
     
    186189    save_on_top = False
    187190    ordering = None
    188191    inlines = []
    189 
     192    changelist_class = ChangeList
     193   
    190194    # Custom templates (designed to be over-ridden in subclasses)
    191195    change_form_template = None
    192196    change_list_template = None
     
    876880
    877881    def changelist_view(self, request, extra_context=None):
    878882        "The 'change list' admin view for this model."
    879         from django.contrib.admin.views.main import ChangeList, ERROR_FLAG
    880883        opts = self.model._meta
    881884        app_label = opts.app_label
    882885        if not self.has_change_permission(request, None):
    883886            raise PermissionDenied
    884887        try:
    885             cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter,
     888            cl = self.changelist_class(request, self.model, self.list_display, self.list_display_links, self.list_filter,
    886889                self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self)
    887890        except IncorrectLookupParameters:
    888891            # Wacky lookup parameters were given, so redirect to the main
     
    890893            # parameter via the query string. If wacky parameters were given and
    891894            # the 'invalid=1' parameter was already in the query string, something
    892895            # is screwed up with the database, so display an error page.
     896            from django.contrib.admin.views.main import ERROR_FLAG
    893897            if ERROR_FLAG in request.GET.keys():
    894898                return render_to_response('admin/invalid_setup.html', {'title': _('Database error')})
    895899            return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')
  • tests/regressiontests/admin_views/tests.py

     
    939939        }
    940940        response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data)
    941941        self.failUnlessEqual(response.status_code, 302)
     942
     943
     944class AdminCustomChangeListTest(TestCase):
     945    fixtures = ['admin-views-users.xml']
     946
     947    def setUp(self):
     948        self.client.login(username='super', password='secret')
     949
     950    def tearDown(self):
     951        self.client.logout()
     952
     953    def testCustomLinkPresence(self):
     954        """
     955        A test to ensure that custom ChangeList class works as expected.
     956        """
     957        post_data = {
     958            "title": u"Lion",
     959        }
     960        response = self.client.post('/test_admin/admin/admin_views/animal/add/', post_data)
     961        self.failUnlessEqual(response.status_code, 302) # redirect somewhere
     962        response = self.client.get('/test_admin/admin/admin_views/animal/')
     963        self.failUnlessEqual(response.status_code, 200)
     964        should_contain = """<a href="custom/1/">Lion</a>"""
     965        self.assertContains(response, should_contain)
     966
  • tests/regressiontests/admin_views/models.py

     
    166166    def __unicode__(self):
    167167        return self.name
    168168
     169class Animal(models.Model):
     170    title = models.CharField(max_length=20)
     171    def __unicode__(self):
     172        return self.title
     173
     174class AnimalChangeList(admin.views.main.ChangeList):
     175    def url_for_result(self, result):
     176        return "custom/%s/" % admin.util.quote(getattr(result, self.pk_attname))
     177
    169178class Account(models.Model):
    170179    """
    171180    A simple, generic account encapsulating the information shared by all
     
    200209        BarAccountAdmin
    201210    )
    202211
     212
     213class AnimalAdmin(admin.ModelAdmin):
     214    changelist_class = AnimalChangeList
     215
     216
    203217class Subscriber(models.Model):
    204218    name = models.CharField(blank=False, max_length=80)
    205219    email = models.EmailField(blank=False, max_length=175)
     
    246260admin.site.register(Persona, PersonaAdmin)
    247261admin.site.register(Subscriber, SubscriberAdmin)
    248262admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
     263admin.site.register(Animal, AnimalAdmin)
    249264
    250265# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    251266# That way we cover all four cases:
  • tests/regressiontests/modeladmin/models.py

     
    912912...     inlines = [ValidationTestInline]
    913913>>> validate(ValidationTestModelAdmin, ValidationTestModel)
    914914
     915# changelist_class
     916
     917>>> class ValidationTestModelAdmin(ModelAdmin):
     918...     changelist_class = object
     919>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     920Traceback (most recent call last):
     921...
     922ImproperlyConfigured: 'ValidationTestModelAdmin.changelist_class' does not inherit from admin.views.main.ChangeList.
     923
     924>>> from django.contrib.admin.views.main import ChangeList
     925>>> class ValidationTestChangeList(ChangeList):
     926...     def url_for_result(self, result):
     927...         return "custom/%s/" % quote(getattr(result, self.pk_attname))
     928>>> class ValidationTestModelAdmin(ModelAdmin):
     929...     changelist_class = ValidationTestChangeList
     930>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     931
    915932"""
    916933}
Back to Top