From 61f3cfed38b6a1bdd4789a83575ba4f46789d88d Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Fri, 7 Jul 2023 19:17:29 +0100 Subject: [PATCH] Standardise on passing show_locale_labels as a kwarg on the table constructor instead of plucking it out of the parent template context, or making it a flag on the column definition --- .../wagtailadmin/chooser/_browse_results.html | 2 +- .../chooser/tables/page_title_cell.html | 2 +- .../chooser/tables/parent_page_cell.html | 2 +- .../pages/listing/_page_title_cell.html | 2 +- wagtail/admin/ui/tables/pages.py | 11 ++++- wagtail/admin/views/chooser.py | 41 +++++++++++-------- wagtail/admin/views/pages/listing.py | 41 +++++++++---------- wagtail/admin/views/pages/search.py | 6 ++- 8 files changed, 62 insertions(+), 45 deletions(-) diff --git a/wagtail/admin/templates/wagtailadmin/chooser/_browse_results.html b/wagtail/admin/templates/wagtailadmin/chooser/_browse_results.html index 3a7f00ca54..5f77e85c5c 100644 --- a/wagtail/admin/templates/wagtailadmin/chooser/_browse_results.html +++ b/wagtail/admin/templates/wagtailadmin/chooser/_browse_results.html @@ -1,6 +1,6 @@ {% load i18n wagtailadmin_tags %} -{% if show_locale_labels %} +{% if show_locale_controls %}
{% if locale_options %} {% if selected_locale %} diff --git a/wagtail/admin/templates/wagtailadmin/chooser/tables/page_title_cell.html b/wagtail/admin/templates/wagtailadmin/chooser/tables/page_title_cell.html index 187c00eca9..be083de281 100644 --- a/wagtail/admin/templates/wagtailadmin/chooser/tables/page_title_cell.html +++ b/wagtail/admin/templates/wagtailadmin/chooser/tables/page_title_cell.html @@ -11,7 +11,7 @@ {% else %} {{ value }} {% endif %} - {% if column.show_locale_labels and page.depth == 2 %} + {% if show_locale_labels %} {% status page.locale.get_display_name classname="w-status--label" %} {% endif %} diff --git a/wagtail/admin/templates/wagtailadmin/chooser/tables/parent_page_cell.html b/wagtail/admin/templates/wagtailadmin/chooser/tables/parent_page_cell.html index a1df82367a..c8aa24d937 100644 --- a/wagtail/admin/templates/wagtailadmin/chooser/tables/parent_page_cell.html +++ b/wagtail/admin/templates/wagtailadmin/chooser/tables/parent_page_cell.html @@ -2,6 +2,6 @@ {% if value %} {{ value.get_admin_display_title }} - {% if column.show_locale_labels %}{% status value.locale.get_display_name classname="w-status--label" %}{% endif %} + {% if show_locale_labels %}{% status value.locale.get_display_name classname="w-status--label" %}{% endif %} {% endif %} diff --git a/wagtail/admin/templates/wagtailadmin/pages/listing/_page_title_cell.html b/wagtail/admin/templates/wagtailadmin/pages/listing/_page_title_cell.html index b1e9a02692..d7b1d1e806 100644 --- a/wagtail/admin/templates/wagtailadmin/pages/listing/_page_title_cell.html +++ b/wagtail/admin/templates/wagtailadmin/pages/listing/_page_title_cell.html @@ -1,4 +1,4 @@ {% load l10n %} - {% include "wagtailadmin/pages/listing/_page_title_explore.html" with page=instance %} + {% include "wagtailadmin/pages/listing/_page_title_explore.html" with page=instance show_locale_labels=show_locale_labels %} diff --git a/wagtail/admin/ui/tables/pages.py b/wagtail/admin/ui/tables/pages.py index e15784355e..a60c0619dd 100644 --- a/wagtail/admin/ui/tables/pages.py +++ b/wagtail/admin/ui/tables/pages.py @@ -72,7 +72,12 @@ class NavigateToChildrenColumn(BaseColumn): class PageTable(Table): def __init__( - self, *args, use_row_ordering_attributes=False, parent_page=None, **kwargs + self, + *args, + use_row_ordering_attributes=False, + parent_page=None, + show_locale_labels=False, + **kwargs, ): super().__init__(*args, **kwargs) @@ -95,6 +100,8 @@ class PageTable(Table): "Sort the order of child pages within '%(parent)s' by '%(label)s' in descending order." ) + self.show_locale_labels = show_locale_labels + def get_ascending_title_text(self, column): return self.ascending_title_text_format % { "parent": self.parent_page and self.parent_page.get_admin_display_title(), @@ -122,6 +129,6 @@ class PageTable(Table): def get_context_data(self, parent_context): context = super().get_context_data(parent_context) - context["show_locale_labels"] = parent_context.get("show_locale_labels") + context["show_locale_labels"] = self.show_locale_labels context["perms"] = parent_context.get("perms") return context diff --git a/wagtail/admin/views/chooser.py b/wagtail/admin/views/chooser.py index 727eba392d..bcf9b663a1 100644 --- a/wagtail/admin/views/chooser.py +++ b/wagtail/admin/views/chooser.py @@ -101,6 +101,15 @@ def can_choose_page( class PageChooserTable(Table): classname = "listing chooser" + def __init__(self, *args, show_locale_labels=False, **kwargs): + super().__init__(*args, **kwargs) + self.show_locale_labels = show_locale_labels + + def get_context_data(self, parent_context): + context = super().get_context_data(parent_context) + context["show_locale_labels"] = self.show_locale_labels + return context + def get_row_classname(self, page): classnames = [] if page.is_parent_page: @@ -116,11 +125,8 @@ class PageChooserTable(Table): class PageTitleColumn(Column): cell_template_name = "wagtailadmin/chooser/tables/page_title_cell.html" - def __init__( - self, *args, show_locale_labels=False, is_multiple_choice=False, **kwargs - ): + def __init__(self, *args, is_multiple_choice=False, **kwargs): super().__init__(*args, **kwargs) - self.show_locale_labels = show_locale_labels self.is_multiple_choice = is_multiple_choice def get_value(self, instance): @@ -129,19 +135,24 @@ class PageTitleColumn(Column): def get_cell_context_data(self, instance, parent_context): context = super().get_cell_context_data(instance, parent_context) context["page"] = instance + # only need to show locale labels for top-level pages + context["show_locale_labels"] = ( + parent_context.get("show_locale_labels") and instance.depth == 2 + ) return context class ParentPageColumn(Column): cell_template_name = "wagtailadmin/chooser/tables/parent_page_cell.html" - def __init__(self, *args, show_locale_labels=False, **kwargs): - super().__init__(*args, **kwargs) - self.show_locale_labels = show_locale_labels - def get_value(self, instance): return instance.get_parent() + def get_cell_context_data(self, instance, parent_context): + context = super().get_cell_context_data(instance, parent_context) + context["show_locale_labels"] = parent_context.get("show_locale_labels") + return context + class PageStatusColumn(Column): cell_template_name = "wagtailadmin/chooser/tables/page_status_cell.html" @@ -170,7 +181,6 @@ class BrowseView(View): PageTitleColumn( "title", label=_("Title"), - show_locale_labels=self.i18n_enabled, is_multiple_choice=self.is_multiple_choice, ), DateColumn( @@ -361,6 +371,7 @@ class BrowseView(View): table = PageChooserTable( self.columns, [self.parent_page] + list(pages), + show_locale_labels=self.i18n_enabled, ) # Render @@ -378,7 +389,7 @@ class BrowseView(View): for desired_class in self.desired_classes ], "page_types_restricted": (page_type_string != "wagtailcore.page"), - "show_locale_labels": self.i18n_enabled, + "show_locale_controls": self.i18n_enabled, "locale_options": locale_options, "selected_locale": selected_locale, "is_multiple_choice": self.is_multiple_choice, @@ -398,12 +409,8 @@ class SearchView(View): @property def columns(self): cols = [ - PageTitleColumn( - "title", label=_("Title"), show_locale_labels=self.i18n_enabled - ), - ParentPageColumn( - "parent", label=_("Parent"), show_locale_labels=self.i18n_enabled - ), + PageTitleColumn("title", label=_("Title")), + ParentPageColumn("parent", label=_("Parent")), DateColumn( "updated", label=_("Updated"), @@ -466,6 +473,7 @@ class SearchView(View): table = PageChooserTable( self.columns, pages, + show_locale_labels=self.i18n_enabled, ) return TemplateResponse( @@ -478,7 +486,6 @@ class SearchView(View): "table": table, "pages": pages, "page_type_string": page_type_string, - "show_locale_labels": self.i18n_enabled, }, ), ) diff --git a/wagtail/admin/views/pages/listing.py b/wagtail/admin/views/pages/listing.py index be78f9d6e4..2f5588dd0c 100644 --- a/wagtail/admin/views/pages/listing.py +++ b/wagtail/admin/views/pages/listing.py @@ -169,6 +169,8 @@ class IndexView(PermissionCheckedMixin, BaseListingView): kwargs = super().get_table_kwargs() kwargs["use_row_ordering_attributes"] = self.show_ordering_column kwargs["parent_page"] = self.parent_page + kwargs["show_locale_labels"] = self.i18n_enabled and self.parent_page.is_root() + if self.show_ordering_column: kwargs["attrs"] = { "aria-description": gettext( @@ -182,6 +184,7 @@ class IndexView(PermissionCheckedMixin, BaseListingView): if self.show_ordering_column: self.columns = self.columns.copy() self.columns[0] = OrderingColumn("ordering", width="10px", sort_key="ord") + self.i18n_enabled = getattr(settings, "WAGTAIL_I18N_ENABLED", False) context = super().get_context_data(**kwargs) @@ -203,30 +206,26 @@ class IndexView(PermissionCheckedMixin, BaseListingView): "side_panels": side_panels, "locale": None, "translations": [], - "show_locale_labels": False, "index_url": self.get_index_url(), } ) - if getattr(settings, "WAGTAIL_I18N_ENABLED", False): - if not self.parent_page.is_root(): - context.update( - { - "locale": self.parent_page.locale, - "translations": [ - { - "locale": translation.locale, - "url": reverse( - "wagtailadmin_explore", args=[translation.id] - ), - } - for translation in self.parent_page.get_translations() - .only("id", "locale") - .select_related("locale") - ], - } - ) - else: - context["show_locale_labels"] = True + if self.i18n_enabled and not self.parent_page.is_root(): + context.update( + { + "locale": self.parent_page.locale, + "translations": [ + { + "locale": translation.locale, + "url": reverse( + "wagtailadmin_explore", args=[translation.id] + ), + } + for translation in self.parent_page.get_translations() + .only("id", "locale") + .select_related("locale") + ], + } + ) return context diff --git a/wagtail/admin/views/pages/search.py b/wagtail/admin/views/pages/search.py index 82085ca4f4..56f74c400c 100644 --- a/wagtail/admin/views/pages/search.py +++ b/wagtail/admin/views/pages/search.py @@ -166,6 +166,11 @@ class BaseSearchView(PermissionCheckedMixin, BaseListingView): def get_index_url(self): return reverse("wagtailadmin_pages:search") + def get_table_kwargs(self): + kwargs = super().get_table_kwargs() + kwargs["show_locale_labels"] = self.show_locale_labels + return kwargs + def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: context = super().get_context_data(**kwargs) context.update( @@ -176,7 +181,6 @@ class BaseSearchView(PermissionCheckedMixin, BaseListingView): "selected_content_type": self.selected_content_type, "ordering": self.ordering, "index_url": self.get_index_url(), - "show_locale_labels": self.show_locale_labels, } ) return context