Quellcode durchsuchen

Fix 'create one now' link on snippet choosers

Regression introduced in #8422
Matt Westcott vor 2 Jahren
Ursprung
Commit
97b3ab2c39

+ 1 - 0
CHANGELOG.txt

@@ -61,6 +61,7 @@ Changelog
  * 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)
  * Fix: Ensure that `DecimalBlock` preserves the `Decimal` type when retrieving from the database (Yves Serrano)
+ * Fix: When no snippets are added, ensure the snippet chooser modal would have the correct URL for creating a new snippet (Matt Westcott)
 
 
 3.0.1 (16.06.2022)

+ 1 - 0
docs/releases/4.0.md

@@ -73,6 +73,7 @@ When using a queryset to render a list of images, you can now use the ``prefetch
  * 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)
  * Ensure that `DecimalBlock` preserves the `Decimal` type when retrieving from the database (Yves Serrano)
+ * When no snippets are added, ensure the snippet chooser modal would have the correct URL for creating a new snippet (Matt Westcott)
 
 
 ## Upgrade considerations

+ 1 - 1
wagtail/snippets/templates/wagtailsnippets/chooser/results.html

@@ -18,7 +18,7 @@
     {% if is_searching %}
         <p role="alert">{% blocktrans trimmed %}Sorry, no snippets match "<em>{{ query_string }}</em>"{% endblocktrans %}</p>
     {% else %}
-        {% url view.add_url_name as wagtailsnippets_create_snippet_url %}
+        {% url add_url_name as wagtailsnippets_create_snippet_url %}
         <p>{% blocktrans trimmed with snippet_type_name=model_opts.verbose_name %}You haven't created any {{ snippet_type_name }} snippets. Why not <a href="{{ wagtailsnippets_create_snippet_url }}" target="_blank" rel="noreferrer">create one now</a>?{% endblocktrans %}</p>
     {% endif %}
 {% endif %}

+ 33 - 0
wagtail/snippets/tests.py

@@ -1736,6 +1736,13 @@ class TestSnippetChoose(TestCase, WagtailTestUtils):
             response.json()["html"],
         )
 
+    def test_no_results(self):
+        Advert.objects.all().delete()
+        response = self.get()
+        self.assertTemplateUsed(response, "wagtailsnippets/chooser/choose.html")
+        response_html = response.json()["html"]
+        self.assertIn('href="/admin/snippets/tests/advert/add/"', response_html)
+
     def test_ordering(self):
         """
         Listing should be ordered by PK if no ordering has been set on the model
@@ -1786,6 +1793,32 @@ class TestSnippetChoose(TestCase, WagtailTestUtils):
         self.assertEqual(response.context["items"][0].text, "English snippet")
 
 
+class TestSnippetChooseResults(TestCase, WagtailTestUtils):
+    fixtures = ["test.json"]
+
+    def setUp(self):
+        self.login()
+        self.url_args = ["tests", "advert"]
+
+    def get(self, params=None):
+        return self.client.get(
+            reverse("wagtailsnippets:choose_results", args=self.url_args), params or {}
+        )
+
+    def test_simple(self):
+        response = self.get()
+        self.assertTemplateUsed(response, "wagtailsnippets/chooser/results.html")
+
+    def test_no_results(self):
+        Advert.objects.all().delete()
+        response = self.get()
+        self.assertTemplateUsed(response, "wagtailsnippets/chooser/results.html")
+        self.assertContains(
+            response,
+            'href="/admin/snippets/tests/advert/add/"',
+        )
+
+
 class TestSnippetChooseWithSearchableSnippet(TestCase, WagtailTestUtils):
     def setUp(self):
         self.login()

+ 3 - 0
wagtail/snippets/views/chooser.py

@@ -116,6 +116,8 @@ class BaseChooseView(View):
 class ChooseView(BaseChooseView):
     # Return the choose view as a ModalWorkflow response
     def render_to_response(self):
+        app_label = self.model._meta.app_label
+        model_name = self.model._meta.model_name
         return render_modal_workflow(
             self.request,
             "wagtailsnippets/chooser/choose.html",
@@ -134,6 +136,7 @@ class ChooseView(BaseChooseView):
                 "locale_options": Locale.objects.all()
                 if issubclass(self.model, TranslatableMixin)
                 else [],
+                "add_url_name": f"wagtailsnippets_{app_label}_{model_name}:add",
             },
             json_data={"step": "choose"},
         )