Ticket #9749: patch_django_9749.20090507.diff

File patch_django_9749.20090507.diff, 7.0 KB (added by David Larlet, 16 years ago)

Update against r10680

  • django/contrib/admin/validation.py

     
    88from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model, _get_foreign_key
    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
     
    148149            validate_base(inline, inline.model)
    149150            validate_inline(inline, cls, model)
    150151
     152    # changelist_class = ChangeList
     153    if hasattr(cls, 'changelist_class') and not issubclass(cls.changelist_class, ChangeList):
     154        raise ImproperlyConfigured("'%s.changelist_class' does not inherit "
     155                        "from admin.views.main.ChangeList." % cls.__name__)
     156
    151157def validate_inline(cls, parent, parent_model):
    152158    # model is already verified to exist and be a Model
    153159    if cls.fk_name: # default value is None
  • django/contrib/admin/options.py

     
    174174
    175175class ModelAdmin(BaseModelAdmin):
    176176    "Encapsulates all admin options and functionality for a given model."
     177    # Avoid circular import of ChangeList
     178    from django.contrib.admin.views.main import ChangeList
     179   
    177180    __metaclass__ = forms.MediaDefiningClass
    178181
    179182    list_display = ('__str__',)
     
    188191    save_on_top = False
    189192    ordering = None
    190193    inlines = []
    191 
     194    changelist_class = ChangeList
     195   
    192196    # Custom templates (designed to be over-ridden in subclasses)
    193197    change_form_template = None
    194198    change_list_template = None
     
    874878
    875879    def changelist_view(self, request, extra_context=None):
    876880        "The 'change list' admin view for this model."
    877         from django.contrib.admin.views.main import ChangeList, ERROR_FLAG
    878881        opts = self.model._meta
    879882        app_label = opts.app_label
    880883        if not self.has_change_permission(request, None):
     
    892895                pass
    893896
    894897        try:
    895             cl = ChangeList(request, self.model, list_display, self.list_display_links, self.list_filter,
     898            cl = self.changelist_class(request, self.model, list_display, self.list_display_links, self.list_filter,
    896899                self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self)
    897900        except IncorrectLookupParameters:
    898901            # Wacky lookup parameters were given, so redirect to the main
     
    900903            # parameter via the query string. If wacky parameters were given and
    901904            # the 'invalid=1' parameter was already in the query string, something
    902905            # is screwed up with the database, so display an error page.
     906            from django.contrib.admin.views.main import ERROR_FLAG
    903907            if ERROR_FLAG in request.GET.keys():
    904908                return render_to_response('admin/invalid_setup.html', {'title': _('Database error')})
    905909            return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')
  • tests/regressiontests/admin_views/tests.py

     
    10501050        self.assertEquals(len(mail.outbox), 1)
    10511051        self.assertEquals(mail.outbox[0].subject, 'Greetings from a function action')
    10521052
     1053
     1054class AdminCustomChangeListTest(TestCase):
     1055    fixtures = ['admin-views-users.xml']
     1056
     1057    def setUp(self):
     1058        self.client.login(username='super', password='secret')
     1059
     1060    def tearDown(self):
     1061        self.client.logout()
     1062
     1063    def testCustomLinkPresence(self):
     1064        """
     1065        A test to ensure that custom ChangeList class works as expected.
     1066        """
     1067        post_data = {
     1068            "title": u"Lion",
     1069        }
     1070        response = self.client.post('/test_admin/admin/admin_views/animal/add/', post_data)
     1071        self.failUnlessEqual(response.status_code, 302) # redirect somewhere
     1072        response = self.client.get('/test_admin/admin/admin_views/animal/')
     1073        self.failUnlessEqual(response.status_code, 200)
     1074        should_contain = """<a href="custom/1/">Lion</a>"""
     1075        self.assertContains(response, should_contain)
     1076
     1077
    10531078class TestInlineNotEditable(TestCase):
    10541079    fixtures = ['admin-views-users.xml']
    10551080
  • tests/regressiontests/admin_views/models.py

     
    184184    def __unicode__(self):
    185185        return self.name
    186186
     187class Animal(models.Model):
     188    title = models.CharField(max_length=20)
     189    def __unicode__(self):
     190        return self.title
     191
     192class AnimalChangeList(admin.views.main.ChangeList):
     193    def url_for_result(self, result):
     194        return "custom/%s/" % admin.util.quote(getattr(result, self.pk_attname))
     195
    187196class Account(models.Model):
    188197    """
    189198    A simple, generic account encapsulating the information shared by all
     
    218227        BarAccountAdmin
    219228    )
    220229
     230
     231class AnimalAdmin(admin.ModelAdmin):
     232    changelist_class = AnimalChangeList
     233
     234
    221235class Subscriber(models.Model):
    222236    name = models.CharField(blank=False, max_length=80)
    223237    email = models.EmailField(blank=False, max_length=175)
     
    335349admin.site.register(Persona, PersonaAdmin)
    336350admin.site.register(Subscriber, SubscriberAdmin)
    337351admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin)
     352admin.site.register(Animal, AnimalAdmin)
    338353admin.site.register(OldSubscriber, OldSubscriberAdmin)
    339354admin.site.register(Podcast, PodcastAdmin)
    340355admin.site.register(Vodcast, VodcastAdmin)
  • tests/regressiontests/modeladmin/models.py

     
    952952...     inlines = [ValidationTestInline]
    953953>>> validate(ValidationTestModelAdmin, ValidationTestModel)
    954954
     955# changelist_class
     956
     957>>> class ValidationTestModelAdmin(ModelAdmin):
     958...     changelist_class = object
     959>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     960Traceback (most recent call last):
     961...
     962ImproperlyConfigured: 'ValidationTestModelAdmin.changelist_class' does not inherit from admin.views.main.ChangeList.
     963
     964>>> from django.contrib.admin.views.main import ChangeList
     965>>> class ValidationTestChangeList(ChangeList):
     966...     def url_for_result(self, result):
     967...         return "custom/%s/" % quote(getattr(result, self.pk_attname))
     968>>> class ValidationTestModelAdmin(ModelAdmin):
     969...     changelist_class = ValidationTestChangeList
     970>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     971
    955972"""
    956973}
Back to Top