Ticket #1035: 1035.diff
File 1035.diff, 4.3 KB (added by , 15 years ago) |
---|
-
django/contrib/admin/widgets.py
### Eclipse Workspace Patch 1.0 #P Django trunk
197 197 This class is a wrapper to a given widget to add the add icon for the 198 198 admin interface. 199 199 """ 200 def __init__(self, widget, rel, admin_site ):200 def __init__(self, widget, rel, admin_site, can_add_related=None): 201 201 self.is_hidden = widget.is_hidden 202 202 self.needs_multipart_form = widget.needs_multipart_form 203 203 self.attrs = widget.attrs 204 204 self.choices = widget.choices 205 205 self.widget = widget 206 206 self.rel = rel 207 # Backwards compatible check for whether a user can add related 208 # objects. 209 if can_add_related is None: 210 can_add_related = rel_to in self.admin_site._registry 211 self.can_add_related = can_add_related 207 212 # so we can check if the related object is registered with this AdminSite 208 213 self.admin_site = admin_site 209 214 … … 228 233 related_url = '../../../%s/%s/add/' % info 229 234 self.widget.choices = self.choices 230 235 output = [self.widget.render(name, value, *args, **kwargs)] 231 if rel_to in self.admin_site._registry: # If the related object has an admin interface:236 if self.can_add_related: 232 237 # TODO: "id_" is hard-coded here. This should instead use the correct 233 238 # API to determine the ID dynamically. 234 239 output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \ -
django/contrib/admin/options.py
102 102 # rendered output. formfield can be None if it came from a 103 103 # OneToOneField with parent_link=True or a M2M intermediary. 104 104 if formfield and db_field.name not in self.raw_id_fields: 105 formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site) 105 related_modeladmin = self.admin_site._registry.get( 106 db_field.rel.to) 107 can_add_related = bool(related_modeladmin and 108 related_modeladmin.has_add_permission(request)) 109 formfield.widget = widgets.RelatedFieldWidgetWrapper( 110 formfield.widget, db_field.rel, self.admin_site, 111 can_add_related=can_add_related) 106 112 107 113 return formfield 108 114 -
tests/regressiontests/admin_views/tests.py
516 516 'Plural error message not found in response to post with multiple errors.') 517 517 self.client.get('/test_admin/admin/logout/') 518 518 519 def testConditionallyShowAddSectionLink(self): 520 """ 521 The foreign key widget should only show the "add related" button if the 522 user has permission to add that related item. 523 """ 524 # Set up and log in user. 525 url = '/test_admin/admin/admin_views/article/add/' 526 add_link_text = ' class="add-another"' 527 self.client.get('/test_admin/admin/') 528 self.client.post('/test_admin/admin/', self.adduser_login) 529 # The add user can't add sections yet, so they shouldn't see the "add 530 # section" link. 531 response = self.client.get(url) 532 self.assertNotContains(response, add_link_text) 533 # Allow the add user to add sections too. Now they can see the "add 534 # section" link. 535 add_user = User.objects.get(username='adduser') 536 perm = get_perm(Section, Section._meta.get_add_permission()) 537 add_user.user_permissions.add(perm) 538 response = self.client.get(url) 539 self.assertContains(response, add_link_text) 540 519 541 def testCustomModelAdminTemplates(self): 520 542 self.client.get('/test_admin/admin/') 521 543 self.client.post('/test_admin/admin/', self.super_login)