Ticket #9749: patch_django_9749.20090323.diff
File patch_django_9749.20090323.diff, 7.3 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/validation.py
5 5 6 6 from django.core.exceptions import ImproperlyConfigured 7 7 from django.db import models 8 8 from django.forms.models import BaseModelForm, BaseModelFormSet, fields_for_model 9 9 from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin 10 10 from django.contrib.admin.options import HORIZONTAL, VERTICAL 11 from django.contrib.admin.views.main import ChangeList 11 12 12 13 __all__ = ['validate'] 13 14 … … 158 159 raise ImproperlyConfigured("'%s.inlines[%d].model' does not " 159 160 "inherit from models.Model." % (cls.__name__, idx)) 160 161 validate_base(inline, inline.model) 161 validate_inline(inline )162 validate_inline(inline, cls, model) 162 163 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 169 def validate_inline(cls, parent, parent_model): 164 170 # model is already verified to exist and be a Model 165 171 if cls.fk_name: # default value is None 166 172 f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name) -
django/contrib/admin/options.py
172 172 173 173 class ModelAdmin(BaseModelAdmin): 174 174 "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 175 178 __metaclass__ = forms.MediaDefiningClass 176 179 177 180 list_display = ('__str__',) … … 186 189 save_on_top = False 187 190 ordering = None 188 191 inlines = [] 189 192 changelist_class = ChangeList 193 190 194 # Custom templates (designed to be over-ridden in subclasses) 191 195 change_form_template = None 192 196 change_list_template = None … … 876 880 877 881 def changelist_view(self, request, extra_context=None): 878 882 "The 'change list' admin view for this model." 879 from django.contrib.admin.views.main import ChangeList, ERROR_FLAG880 883 opts = self.model._meta 881 884 app_label = opts.app_label 882 885 if not self.has_change_permission(request, None): 883 886 raise PermissionDenied 884 887 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, 886 889 self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self) 887 890 except IncorrectLookupParameters: 888 891 # Wacky lookup parameters were given, so redirect to the main … … 890 893 # parameter via the query string. If wacky parameters were given and 891 894 # the 'invalid=1' parameter was already in the query string, something 892 895 # is screwed up with the database, so display an error page. 896 from django.contrib.admin.views.main import ERROR_FLAG 893 897 if ERROR_FLAG in request.GET.keys(): 894 898 return render_to_response('admin/invalid_setup.html', {'title': _('Database error')}) 895 899 return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1') -
tests/regressiontests/admin_views/tests.py
939 939 } 940 940 response = self.client.post('/test_admin/admin/admin_views/externalsubscriber/', action_data) 941 941 self.failUnlessEqual(response.status_code, 302) 942 943 944 class 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
166 166 def __unicode__(self): 167 167 return self.name 168 168 169 class Animal(models.Model): 170 title = models.CharField(max_length=20) 171 def __unicode__(self): 172 return self.title 173 174 class 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 169 178 class Account(models.Model): 170 179 """ 171 180 A simple, generic account encapsulating the information shared by all … … 200 209 BarAccountAdmin 201 210 ) 202 211 212 213 class AnimalAdmin(admin.ModelAdmin): 214 changelist_class = AnimalChangeList 215 216 203 217 class Subscriber(models.Model): 204 218 name = models.CharField(blank=False, max_length=80) 205 219 email = models.EmailField(blank=False, max_length=175) … … 246 260 admin.site.register(Persona, PersonaAdmin) 247 261 admin.site.register(Subscriber, SubscriberAdmin) 248 262 admin.site.register(ExternalSubscriber, ExternalSubscriberAdmin) 263 admin.site.register(Animal, AnimalAdmin) 249 264 250 265 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. 251 266 # That way we cover all four cases: -
tests/regressiontests/modeladmin/models.py
912 912 ... inlines = [ValidationTestInline] 913 913 >>> validate(ValidationTestModelAdmin, ValidationTestModel) 914 914 915 # changelist_class 916 917 >>> class ValidationTestModelAdmin(ModelAdmin): 918 ... changelist_class = object 919 >>> validate(ValidationTestModelAdmin, ValidationTestModel) 920 Traceback (most recent call last): 921 ... 922 ImproperlyConfigured: '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 915 932 """ 916 933 }