0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Fix 'create one now' link on snippet choosers

Regression introduced in #8422
This commit is contained in:
Matt Westcott 2022-06-20 18:46:40 +01:00 committed by LB (Ben Johnston)
parent 409c840adb
commit 97b3ab2c39
5 changed files with 39 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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 %}

View File

@ -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()

View File

@ -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"},
)