Repository: wagtail/wagtail-generic-chooser (language: python)

Issue:
Version 0.4 is incompatible with wagtail 2.12
Hi,

Version 0.4 is not compatible anymore with wagtail 2.12 since the widgets code use `telepath` module which have been introduced in wagtail 2.13.

So actually, trying to use generic chooser 0.4 with wagtail 2.12 leads to an ImportError. It would be nice to notice this breaking changes in your changelog history.


Discussion / hints:
Thanks for the report @sveetch! Dropping support for 2.12 wasn't intentional - looks like the relevant fallback was removed in #52, and I didn't catch it when cherry-picking just the commits that added Wagtail 3.0 support. Will reinstate the fallback and make a 0.4.1 release.

Fix patch:
diff --git a/generic_chooser/widgets.py b/generic_chooser/widgets.py
index 7ff846a..3757c9d 100644
--- a/generic_chooser/widgets.py
+++ b/generic_chooser/widgets.py
@@ -15,14 +15,22 @@
 try:
     from wagtail.telepath import register
 except ImportError:
-    # Wagtail<3.0
-    from wagtail.core.telepath import register
-    
+    try:
+        # Wagtail<3.0
+        from wagtail.core.telepath import register
+    except ImportError:  # do-nothing fallback for Wagtail <2.13
+        def register(adapter, cls):
+            pass
+
 try:
     from wagtail.widget_adapters import WidgetAdapter
 except ImportError:
-    # Wagtail<3.0
-    from wagtail.core.widget_adapters import WidgetAdapter
+    try:
+        # Wagtail<3.0
+        from wagtail.core.widget_adapters import WidgetAdapter
+    except ImportError:  # do-nothing fallback for Wagtail <2.13
+        class WidgetAdapter:
+            pass
 
 class AdminChooser(WidgetWithScript, widgets.Input):
     input_type = 'hidden'


Test patch:
diff --git a/tests/tests.py b/tests/tests.py
index e4b412e..3a4ef43 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -2,6 +2,7 @@
 from urllib.parse import urlencode, urlparse
 from unittest.mock import patch
 
+from django import forms
 from django.contrib.auth.models import User, Group
 from django.test import TestCase
 
@@ -14,6 +15,7 @@
     from wagtail.core.models import Page, Site
 
 from .models import Person
+from .widgets import SiteChooser
 
 
 class TestChooseView(TestCase):
@@ -569,3 +571,14 @@ def test_post_valid(self):
             response_json['result'],
             {"id": str(person.id), "string": "Gordon Ramsay", "edit_link": None}
         )
+
+
+class TestChooserWidget(TestCase):
+    def test_render(self):
+        class SiteForm(forms.Form):
+            site = forms.ModelChoiceField(queryset=Site.objects.all(), widget=SiteChooser())
+
+        localhost = Site.objects.get(hostname='localhost')
+        form = SiteForm(initial={'site': localhost})
+        html = form.as_p()
+        self.assertIn('<span class="title">localhost [default]</span>', html)
diff --git a/tests/widgets.py b/tests/widgets.py
new file mode 100644
index 0000000..68c46bd
--- /dev/null
+++ b/tests/widgets.py
@@ -0,0 +1,16 @@
+try:
+    from wagtail.models import Site
+except ImportError:
+    # Wagtail<3.0
+    from wagtail.core.models import Site
+
+from generic_chooser.widgets import AdminChooser
+
+
+class SiteChooser(AdminChooser):
+    model = Site
+    icon = "site"
+    choose_one_text = "Choose a site"
+    choose_another_text = "Choose another site"
+    link_to_chosen_text = "Edit this site"
+    edit_item_url_name = "wagtailsites:edit"