Переглянути джерело

Added new get_primary_button_for_obj() to ButtonHelper

- This method is now used to add links to the IndexView's rows, where
possible.
- If the user does not have permissions to edit, the inspect view will be used (if enabled), otherwise the item till not be clickable
- fixes #8261
Stefan Hammer 2 роки тому
батько
коміт
82ff0f935a

+ 1 - 0
CHANGELOG.txt

@@ -55,6 +55,7 @@ Changelog
  * Fix: Show alternative message when no page types are available to be created (Jaspreet Singh)
  * Fix: Prevent error on sending notifications for the legacy moderation process when no user was specified (Yves Serrano)
  * Fix: Ensure `aria-label` is not set on locale selection dropdown within page chooser modal as it was a duplicate of the button contents (LB (Ben Johnston))
+ * Fix: Revise the `ModelAdmin` title column behaviour to only link to 'edit' if the user has the correct permissions, fallback to the 'inspect' view or a non-clickable title if needed (Stefan Hammer)
 
 
 3.0.1 (16.06.2022)

+ 1 - 0
docs/releases/4.0.md

@@ -68,6 +68,7 @@ When using a queryset to render a list of images, you can now use the ``prefetch
  * Show alternative message when no page types are available to be created (Jaspreet Singh)
  * Prevent error on sending notifications for the legacy moderation process when no user was specified (Yves Serrano)
  * Ensure `aria-label` is not set on locale selection dropdown within page chooser modal as it was a duplicate of the button contents (LB (Ben Johnston))
+ * Revise the `ModelAdmin` title column behaviour to only link to 'edit' if the user has the correct permissions, fallback to the 'inspect' view or a non-clickable title if needed (Stefan Hammer)
 
 
 ## Upgrade considerations

+ 9 - 0
wagtail/contrib/modeladmin/helpers/button.py

@@ -107,6 +107,15 @@ class ButtonHelper:
             btns.append(self.delete_button(pk, classnames_add, classnames_exclude))
         return btns
 
+    def get_primary_button(self, obj):
+        ph = self.permission_helper
+        usr = self.request.user
+        pk = getattr(obj, self.opts.pk.attname)
+        if ph.user_can_edit_obj(usr, obj):
+            return self.edit_button(pk)
+        if ph.user_can_inspect_obj(usr, obj):
+            return self.inspect_button(pk)
+
 
 class PageButtonHelper(ButtonHelper):
 

+ 5 - 3
wagtail/contrib/modeladmin/templatetags/modeladmin_tags.py

@@ -71,13 +71,15 @@ def items_for_result(view, result, request):
         row_attrs = modeladmin.get_extra_attrs_for_field_col(result, field_name)
         row_attrs["class"] = " ".join(row_classes)
         row_attrs_flat = flatatt(row_attrs)
+        primary_button = None
         if field_name == modeladmin.get_list_display_add_buttons(request):
-            edit_url = view.url_helper.get_action_url("edit", result.pk)
+            primary_button = view.button_helper.get_primary_button(result)
+        if primary_button is not None and primary_button.get("url"):
             yield format_html(
                 '<td{}><div class="title-wrapper"><a href="{}" title="{}">{}</a></div></td>',
                 row_attrs_flat,
-                edit_url,
-                _("Edit this %s") % view.verbose_name,
+                primary_button["url"],
+                primary_button.get("title", ""),
                 result_repr,
             )
         else:

+ 33 - 0
wagtail/contrib/modeladmin/tests/test_simple_modeladmin.py

@@ -265,6 +265,39 @@ class TestAuthorIndexView(TestCase, WagtailTestUtils):
         """
         self.assertContains(response, test_html, html=True)
 
+    def test_title_column_links_to_edit_view_by_default(self):
+        response = self.get()
+        self.assertEqual(response.status_code, 200)
+        test_html = """
+            <div class="title-wrapper"><a href="/admin/modeladmintest/author/edit/1/" title="Edit this author">J. R. R. Tolkien</a></div>
+        """
+        self.assertContains(response, test_html, html=True)
+
+    @mock.patch(
+        "wagtail.contrib.modeladmin.helpers.permission.PermissionHelper.user_can_edit_obj",
+        return_value=False,
+    )
+    def test_title_column_links_to_inspect_view_when_user_cannot_edit(self, *mocks):
+        response = self.get()
+        self.assertEqual(response.status_code, 200)
+        test_html = """
+            <div class="title-wrapper"><a href="/admin/modeladmintest/author/inspect/1/" title="Inspect this author">J. R. R. Tolkien</a></div>
+        """
+        self.assertContains(response, test_html, html=True)
+
+    @mock.patch(
+        "wagtail.contrib.modeladmin.helpers.permission.PermissionHelper.user_can_inspect_obj",
+        return_value=False,
+    )
+    @mock.patch(
+        "wagtail.contrib.modeladmin.helpers.permission.PermissionHelper.user_can_edit_obj",
+        return_value=False,
+    )
+    def test_title_column_is_not_linked_when_user_cannot_edit_or_inspect(self, *mocks):
+        response = self.get()
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, '<td class="field-name title">J. R. R. Tolkien')
+
 
 @override_settings(WAGTAIL_I18N_ENABLED=True)
 class TestTranslatableBookIndexView(TestCase, WagtailTestUtils):