Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion wagtaildraftsharing/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
from wagtail.snippets.views.snippets import SnippetViewSet

from wagtaildraftsharing.models import WagtaildraftsharingLink
from wagtail.permission_policies import ModelPermissionPolicy

from .settings import settings as draftsharing_settings

class NoAddPermissionPolicy(ModelPermissionPolicy):
def user_has_permission(self, user, action):
if action == 'add':
return False
return super().user_has_permission(user, action)

class WagtaildraftsharingLinkSnippetViewSet(SnippetViewSet):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Could also add_view_class = None to ensure that code path is not working, but redundant. There's no one way to disable the add view I guess.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that doesnt work on wagtail 5 though i dont think?

model = WagtaildraftsharingLink
Expand All @@ -16,6 +22,9 @@ class WagtaildraftsharingLinkSnippetViewSet(SnippetViewSet):
list_display = ("__str__", "is_active", "created_by", "share_url")
list_filter = ("is_active",)

# Use custom permission policy so adding from this view is disabled
permission_policy = NoAddPermissionPolicy(WagtaildraftsharingLink)

edit_handler = ObjectList([
FieldPanel("revision", read_only=True),
FieldPanel(
Expand All @@ -33,7 +42,6 @@ class WagtaildraftsharingLinkSnippetViewSet(SnippetViewSet):
),
])


def get_queryset(self, request):
return WagtaildraftsharingLink.objects.all().prefetch_related(
"revision", "revision__content_object", "created_by"
Expand Down
25 changes: 25 additions & 0 deletions wagtaildraftsharing/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from wagtail_factories import PageFactory

from wagtaildraftsharing.models import WagtaildraftsharingLink
from wagtaildraftsharing.snippets import WagtaildraftsharingLinkSnippetViewSet
from wagtaildraftsharing.views import CreateSharingLinkView, SharingLinkView

User = get_user_model()
Expand Down Expand Up @@ -197,3 +198,27 @@ def test_sharing_link_includes_robots_header(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(response["X-Robots-Tag"], "noindex, nofollow")


class WagtaildraftsharingLinkSnippetAdminViewTests(TestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.superuser = User.objects.create_superuser(
username="admin",
password="test",
)

def test_snippet_list_does_not_show_add_button_for_superuser(self):
# Superusers would normally be allowed to add snippets, but our
# custom permission policy should disable the "Add" action.
self.client.login(username="admin", password="test")

viewset = WagtaildraftsharingLinkSnippetViewSet()
list_url = reverse(viewset.get_url_name("list"))

response = self.client.get(list_url)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, "Add Draftsharing Link")
# Occasionally the "Add Sharing Link" pops up too...
self.assertNotContains(response, "Add Sharing Link")