diff --git a/CHANGELOG.txt b/CHANGELOG.txt index a379e70633..a07b8b6102 100644 --- a/CHANGELOG.txt +++ b/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) diff --git a/docs/releases/4.0.md b/docs/releases/4.0.md index b4fbe52d37..0c79cfb093 100644 --- a/docs/releases/4.0.md +++ b/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 diff --git a/wagtail/snippets/templates/wagtailsnippets/chooser/results.html b/wagtail/snippets/templates/wagtailsnippets/chooser/results.html index 4d50cfc18b..a83dde8946 100644 --- a/wagtail/snippets/templates/wagtailsnippets/chooser/results.html +++ b/wagtail/snippets/templates/wagtailsnippets/chooser/results.html @@ -18,7 +18,7 @@ {% if is_searching %}

{% blocktrans trimmed %}Sorry, no snippets match "{{ query_string }}"{% endblocktrans %}

{% else %} - {% url view.add_url_name as wagtailsnippets_create_snippet_url %} + {% url add_url_name as wagtailsnippets_create_snippet_url %}

{% blocktrans trimmed with snippet_type_name=model_opts.verbose_name %}You haven't created any {{ snippet_type_name }} snippets. Why not create one now?{% endblocktrans %}

{% endif %} {% endif %} diff --git a/wagtail/snippets/tests.py b/wagtail/snippets/tests.py index c534a925f7..212a55cfe7 100644 --- a/wagtail/snippets/tests.py +++ b/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() diff --git a/wagtail/snippets/views/chooser.py b/wagtail/snippets/views/chooser.py index 3ac59890cc..290c6fc87e 100644 --- a/wagtail/snippets/views/chooser.py +++ b/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"}, )