Ver código fonte

Remove the edit link from edit bird in previews to avoid confusion

Sævar Öfjörð Magnússon 2 anos atrás
pai
commit
34734d252f

+ 1 - 0
CHANGELOG.txt

@@ -53,6 +53,7 @@ Changelog
  * Add legacy and new status tags to the pattern library (Steven Steinwand)
  * Added `WAGTAILADMIN_USER_PASSWORD_RESET_FORM` setting for overriding the admin password reset form (Michael Karamuth)
  * Prefetch workflow states in edit page view to to avoid queries in other parts of the view/templates that need it (Tidiane Dia)
+ * Remove the edit link from edit bird in previews to avoid confusion (Sævar Öfjörð Magnússon)
  * Fix: Typo in `ResumeWorkflowActionFormatter` message (Stefan Hammer)
  * Fix: Throw a meaningful error when saving an image to an unrecognised image format (Christian Franke)
  * Fix: Remove extra padding for headers with breadcrumbs on mobile viewport (Steven Steinwand)

+ 1 - 0
docs/releases/4.0.md

@@ -60,6 +60,7 @@ When using a queryset to render a list of images, you can now use the `prefetch_
  * Add legacy and new status tags to the pattern library (Steven Steinwand)
  * Added `WAGTAILADMIN_USER_PASSWORD_RESET_FORM` setting for overriding the admin password reset form (Michael Karamuth)
  * Prefetch workflow states in edit page view to to avoid queries in other parts of the view/templates that need it (Tidiane Dia)
+ * Remove the edit link from edit bird in previews to avoid confusion (Sævar Öfjörð Magnússon)
 
 ### Bug fixes
 

+ 29 - 1
wagtail/admin/tests/test_userbar.py

@@ -16,9 +16,11 @@ class TestUserbarTag(TestCase, WagtailTestUtils):
         )
         self.homepage = Page.objects.get(id=2)
 
-    def dummy_request(self, user=None):
+    def dummy_request(self, user=None, is_preview=False):
         request = RequestFactory().get("/")
         request.user = user or AnonymousUser()
+        if is_preview:
+            request.is_preview = True
         return request
 
     def test_userbar_tag(self):
@@ -82,6 +84,32 @@ class TestUserbarTag(TestCase, WagtailTestUtils):
 
         self.assertIn("<!-- Wagtail user bar embed code -->", content)
 
+    def test_edit_link(self):
+        template = Template("{% load wagtailuserbar %}{% wagtailuserbar %}")
+        content = template.render(
+            Context(
+                {
+                    PAGE_TEMPLATE_VAR: self.homepage,
+                    "request": self.dummy_request(self.user, is_preview=False),
+                }
+            )
+        )
+        self.assertIn("<!-- Wagtail user bar embed code -->", content)
+        self.assertIn("Edit this page", content)
+
+    def test_userbar_not_in_preview(self):
+        template = Template("{% load wagtailuserbar %}{% wagtailuserbar %}")
+        content = template.render(
+            Context(
+                {
+                    PAGE_TEMPLATE_VAR: self.homepage,
+                    "request": self.dummy_request(self.user, is_preview=True),
+                }
+            )
+        )
+        self.assertIn("<!-- Wagtail user bar embed code -->", content)
+        self.assertNotIn("Edit this page", content)
+
 
 class TestUserbarFrontend(TestCase, WagtailTestUtils):
     def setUp(self):

+ 8 - 0
wagtail/admin/userbar.py

@@ -84,6 +84,14 @@ class EditPageItem(BaseItem):
         if not self.page.id:
             return ""
 
+        # Don't render if request is a preview. This is to avoid confusion that
+        # might arise when the user clicks edit on a preview.
+        try:
+            if request.is_preview:
+                return ""
+        except AttributeError:
+            pass
+
         # Don't render if user doesn't have permission to access the admin area
         if not request.user.has_perm("wagtailadmin.access_admin"):
             return ""