Selaa lähdekoodia

Fix snippet IndexView not respecting get_add_url()

Christer Jensen 1 vuosi sitten
vanhempi
commit
b14bc33cdc

+ 1 - 0
CHANGELOG.txt

@@ -9,6 +9,7 @@ Changelog
  * Update settings file in project settings to address Django 4.2 deprecations (Sage Abdullah)
  * Improve layout and accessibility of the image URL generator page, reduce reliance on JavaScript (Temidayo Azeez)
  * Allow `UniqueConstraint` in place of `unique_together` for `TranslatableMixin`'s system check (Temidayo Azeez, Sage Abdullah)
+ * Make use of `IndexView.get_add_url()` in snippets index view template (Christer Jensen, Sage Abdullah)
  * Fix: Update system check for overwriting storage backends to recognise the `STORAGES` setting introduced in Django 4.2 (phijma-leukeleu)
  * Fix: Prevent password change form from raising a validation error when browser autocomplete fills in the "Old password" field (Chiemezuo Akujobi)
  * Fix: Ensure that the legacy dropdown options, when closed, do not get accidentally clicked by other interactions wide viewports (CheesyPhoenix, Christer Jensen)

+ 1 - 0
docs/releases/6.0.md

@@ -19,6 +19,7 @@ depth: 1
  * Update settings file in project settings to address Django 4.2 deprecations (Sage Abdullah)
  * Improve layout and accessibility of the image URL generator page, reduce reliance on JavaScript (Temidayo Azeez)
  * Allow `UniqueConstraint` in place of `unique_together` for {class}`~wagtail.models.TranslatableMixin`'s system check (Temidayo Azeez, Sage Abdullah)
+ * Make use of `IndexView.get_add_url()` in snippets index view template (Christer Jensen, Sage Abdullah)
 
 ### Bug fixes
 

+ 1 - 1
wagtail/admin/views/generic/models.py

@@ -377,7 +377,7 @@ class IndexView(
 
     def get_add_url(self):
         if self.add_url_name:
-            return reverse(self.add_url_name)
+            return self._set_locale_query_param(reverse(self.add_url_name))
 
     def get_page_title(self):
         if not self.page_title and self.model:

+ 1 - 1
wagtail/snippets/templates/wagtailsnippets/snippets/index.html

@@ -14,7 +14,7 @@
 {% block content %}
     {% include 'wagtailadmin/shared/headers/slim_header.html' %}
     {% fragment as base_action_locale %}{% if locale %}{% include 'wagtailadmin/shared/locale_selector.html' with theme="large" %}{% endif %}{% endfragment %}
-    {% fragment as action_url_add_snippet %}{% if can_add_snippet %}{% url view.add_url_name %}{% if locale %}?locale={{ locale.language_code }}{% endif %}{% endif %}{% endfragment %}
+    {% fragment as action_url_add_snippet %}{% if can_add_snippet %}{{ add_url }}{% endif %}{% endfragment %}
     {% fragment as action_text_snippet %}{% blocktrans trimmed with snippet_type_name=model_opts.verbose_name %}Add {{ snippet_type_name }}{% endblocktrans %}{% endfragment %}
     {% fragment as extra_actions %}
         {% if view.list_export %}

+ 1 - 9
wagtail/snippets/templates/wagtailsnippets/snippets/index_results.html

@@ -17,14 +17,6 @@
     {% if is_searching or is_filtering %}
         <p role="alert">{% blocktrans trimmed with snippet_type_name_plural=model_opts.verbose_name_plural %}Sorry, no {{ snippet_type_name_plural }} match your query{% endblocktrans %}</p>
     {% else %}
-        {% url view.add_url_name as wagtailsnippets_create_url %}
-
-        {# NOTE: The two strings in the following messages are exactly the same #}
-        {# The difference is that the first variant appends '?locale={{ locale.language_code}}' to 'wagtailsnippets_create_url' #}
-        {% if locale %}
-            <p class="no-results-message">{% blocktrans trimmed with snippet_type_name_plural=model_opts.verbose_name_plural wagtailsnippets_create_url=wagtailsnippets_create_url|add:'?locale='|add:locale.language_code %}No {{ snippet_type_name_plural }} have been created. Why not <a href="{{ wagtailsnippets_create_url }}">add one</a>?{% endblocktrans %}</p>
-        {% else %}
-            <p class="no-results-message">{% blocktrans trimmed with snippet_type_name_plural=model_opts.verbose_name_plural %}No {{ snippet_type_name_plural }} have been created. Why not <a href="{{ wagtailsnippets_create_url }}">add one</a>?{% endblocktrans %}</p>
-        {% endif %}
+        <p class="no-results-message">{% blocktrans trimmed with snippet_type_name_plural=model_opts.verbose_name_plural wagtailsnippets_create_url=add_url %}No {{ snippet_type_name_plural }} have been created. Why not <a href="{{ wagtailsnippets_create_url }}">add one</a>?{% endblocktrans %}</p>
     {% endif %}
 {% endif %}

+ 26 - 6
wagtail/snippets/tests/test_viewset.py

@@ -9,7 +9,7 @@ from django.contrib.auth.models import Permission
 from django.contrib.contenttypes.models import ContentType
 from django.core.exceptions import ImproperlyConfigured
 from django.template.defaultfilters import date
-from django.test import SimpleTestCase, TestCase, TransactionTestCase
+from django.test import SimpleTestCase, TestCase, TransactionTestCase, override_settings
 from django.urls import NoReverseMatch, resolve, reverse
 from django.utils.timezone import now
 from openpyxl import load_workbook
@@ -435,11 +435,7 @@ class TestFilterSetClass(BaseSnippetViewSetTests):
 
     def test_unfiltered_no_results(self):
         response = self.get()
-        add_url = self.get_url("add")
-        self.assertContains(
-            response,
-            f'No full-featured snippets have been created. Why not <a href="{add_url}">add one</a>',
-        )
+        self.assertContains(response, "No full-featured snippets have been created.")
         self.assertContains(
             response,
             '<label for="id_country_code_0"><input type="radio" name="country_code" value="" id="id_country_code_0" checked>All</label>',
@@ -1493,3 +1489,27 @@ class TestBreadcrumbs(AdminTemplateTestUtils, BaseSnippetViewSetTests):
             {"url": "", "label": "Inspect"},
         ]
         self.assertBreadcrumbsItemsRendered(items, response.content)
+
+
+class TestCustomMethods(BaseSnippetViewSetTests):
+    model = FullFeaturedSnippet
+
+    def test_index_view_get_add_url_is_respected(self):
+        response = self.client.get(self.get_url("list"))
+        add_url = self.get_url("add") + "?customised=param"
+        soup = self.get_soup(response.content)
+        # Should contain the customised add URL in two places:
+        # The main action button, and the "Why not add one?" suggestion
+        links = soup.find_all("a", attrs={"href": add_url})
+        self.assertEqual(len(links), 2)
+
+    @override_settings(WAGTAIL_I18N_ENABLED=True)
+    def test_index_view_get_add_url_is_respected_with_i18n(self):
+        Locale.objects.create(language_code="fr")
+        response = self.client.get(self.get_url("list") + "?locale=fr")
+        add_url = self.get_url("add") + "?locale=fr&customised=param"
+        soup = self.get_soup(response.content)
+        # Should contain the customised add URL in two places:
+        # The main action button, and the "Why not add one?" suggestion
+        links = soup.find_all("a", attrs={"href": add_url})
+        self.assertEqual(len(links), 2)

+ 7 - 0
wagtail/test/testapp/wagtail_hooks.py

@@ -18,6 +18,7 @@ from wagtail.admin.search import SearchArea
 from wagtail.admin.site_summary import SummaryItem
 from wagtail.admin.ui.components import Component
 from wagtail.admin.ui.tables import BooleanColumn, UpdatedAtColumn
+from wagtail.admin.utils import set_query_params
 from wagtail.admin.views.account import BaseSettingsPanel
 from wagtail.admin.widgets import Button
 from wagtail.snippets.bulk_actions.snippet_bulk_action import SnippetBulkAction
@@ -301,6 +302,12 @@ class FullFeaturedSnippetViewSet(SnippetViewSet):
     menu_order = 999999
     inspect_view_enabled = True
 
+    class IndexView(SnippetViewSet.index_view_class):
+        def get_add_url(self):
+            return set_query_params(super().get_add_url(), {"customised": "param"})
+
+    index_view_class = IndexView
+
     # TODO: When specific search fields are supported in SQLite FTS (see #10217),
     # specify search_fields or get_search_fields here