From 78dfb10da6529272a572168c4c142640aa26b6ca Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Mon, 18 Jul 2022 11:44:29 +0100 Subject: [PATCH] Update snippet chooser URL routing for Wagtail 4 as per https://docs.wagtail.org/en/latest/releases/4.0.html#url-route-names-for-document-and-snippet-apps-have-changed --- wagtail_localize/views/edit_translation.py | 49 +++++++++++++++------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/wagtail_localize/views/edit_translation.py b/wagtail_localize/views/edit_translation.py index a2669d94..366dbb05 100644 --- a/wagtail_localize/views/edit_translation.py +++ b/wagtail_localize/views/edit_translation.py @@ -381,6 +381,23 @@ def widget_from_field(field): return {"type": "image_chooser"} elif issubclass(field.related_model, tuple(get_snippet_models())): + if WAGTAIL_VERSION >= (4, 0, 0): + chooser_url = reverse( + "wagtailsnippetchoosers_%s_%s:choose" + % ( + field.related_model._meta.app_label, + field.related_model._meta.model_name, + ) + ) + else: + chooser_url = reverse( + "wagtailsnippets:choose", + args=[ + field.related_model._meta.app_label, + field.related_model._meta.model_name, + ], + ) + return { "type": "snippet_chooser", "snippet_model": { @@ -389,13 +406,7 @@ def widget_from_field(field): "verbose_name": field.related_model._meta.verbose_name, "verbose_name_plural": field.related_model._meta.verbose_name_plural, }, - "chooser_url": reverse( - "wagtailsnippets:choose", - args=[ - field.related_model._meta.app_label, - field.related_model._meta.model_name, - ], - ), + "chooser_url": chooser_url, } elif isinstance( @@ -428,6 +439,22 @@ def widget_from_block(block, content_components=None): return {"type": "image_chooser"} elif isinstance(block, SnippetChooserBlock): + if WAGTAIL_VERSION >= (4, 0, 0): + chooser_url = reverse( + "wagtailsnippetchoosers_%s_%s:choose" + % ( + block.target_model._meta.app_label, + block.target_model._meta.model_name, + ) + ) + else: + chooser_url = reverse( + "wagtailsnippets:choose", + args=[ + block.target_model._meta.app_label, + block.target_model._meta.model_name, + ], + ) return { "type": "snippet_chooser", "snippet_model": { @@ -436,13 +463,7 @@ def widget_from_block(block, content_components=None): "verbose_name": block.target_model._meta.verbose_name, "verbose_name_plural": block.target_model._meta.verbose_name_plural, }, - "chooser_url": reverse( - "wagtailsnippets:choose", - args=[ - block.target_model._meta.app_label, - block.target_model._meta.model_name, - ], - ), + "chooser_url": chooser_url, } elif isinstance(