Ticket #1035: 1035.diff

File 1035.diff, 4.3 KB (added by Chris Beaven, 15 years ago)
  • django/contrib/admin/widgets.py

    ### Eclipse Workspace Patch 1.0
    #P Django trunk
     
    197197    This class is a wrapper to a given widget to add the add icon for the
    198198    admin interface.
    199199    """
    200     def __init__(self, widget, rel, admin_site):
     200    def __init__(self, widget, rel, admin_site, can_add_related=None):
    201201        self.is_hidden = widget.is_hidden
    202202        self.needs_multipart_form = widget.needs_multipart_form
    203203        self.attrs = widget.attrs
    204204        self.choices = widget.choices
    205205        self.widget = widget
    206206        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
    207212        # so we can check if the related object is registered with this AdminSite
    208213        self.admin_site = admin_site
    209214
     
    228233            related_url = '../../../%s/%s/add/' % info
    229234        self.widget.choices = self.choices
    230235        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:
    232237            # TODO: "id_" is hard-coded here. This should instead use the correct
    233238            # API to determine the ID dynamically.
    234239            output.append(u'<a href="%s" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
  • django/contrib/admin/options.py

     
    102102            # rendered output. formfield can be None if it came from a
    103103            # OneToOneField with parent_link=True or a M2M intermediary.
    104104            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)
    106112
    107113            return formfield
    108114
  • tests/regressiontests/admin_views/tests.py

     
    516516                        'Plural error message not found in response to post with multiple errors.')
    517517        self.client.get('/test_admin/admin/logout/')
    518518
     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
    519541    def testCustomModelAdminTemplates(self):
    520542        self.client.get('/test_admin/admin/')
    521543        self.client.post('/test_admin/admin/', self.super_login)
Back to Top