Allow passing a tag model to AdminTagWidget

pull/5837/head
Matt Westcott 2020-02-17 22:39:04 +00:00
rodzic 5d8b343528
commit 0099de92f6
2 zmienionych plików z 31 dodań i 5 usunięć

Wyświetl plik

@ -4,7 +4,7 @@ from django.test.utils import override_settings
from wagtail.admin import widgets
from wagtail.core.models import Page
from wagtail.tests.testapp.forms import AdminStarDateInput
from wagtail.tests.testapp.models import EventPage, SimplePage
from wagtail.tests.testapp.models import EventPage, RestaurantTag, SimplePage
class TestAdminPageChooserWidget(TestCase):
@ -186,7 +186,7 @@ class TestAdminTagWidget(TestCase):
def test_render_js_init_basic(self):
"""Chekcs that the 'initTagField' is correctly added to the inline script for tag widgets"""
"""Checks that the 'initTagField' is correctly added to the inline script for tag widgets"""
widget = widgets.AdminTagWidget()
html = widget.render('tags', None, attrs={'id': 'alpha'})
@ -201,7 +201,7 @@ class TestAdminTagWidget(TestCase):
@override_settings(TAG_SPACES_ALLOWED=False)
def test_render_js_init_no_spaces_allowed(self):
"""Chekcs that the 'initTagField' includes the correct value based on TAG_SPACES_ALLOWED in settings"""
"""Checks that the 'initTagField' includes the correct value based on TAG_SPACES_ALLOWED in settings"""
widget = widgets.AdminTagWidget()
html = widget.render('tags', None, attrs={'id': 'alpha'})
@ -214,7 +214,7 @@ class TestAdminTagWidget(TestCase):
@override_settings(TAG_LIMIT=5)
def test_render_js_init_with_tag_limit(self):
"""Chekcs that the 'initTagField' includes the correct value based on TAG_LIMIT in settings"""
"""Checks that the 'initTagField' includes the correct value based on TAG_LIMIT in settings"""
widget = widgets.AdminTagWidget()
html = widget.render('tags', None, attrs={'id': 'alpha'})
@ -223,3 +223,16 @@ class TestAdminTagWidget(TestCase):
self.assertEqual(len(params), 4)
self.assertEqual(params[2], 'true') # tag_spaces_allowed
self.assertEqual(params[3], '5') # tag_limit
def test_render_js_init_with_tag_model(self):
"""Checks that 'initTagField' is passed the correct autocomplete URL for the custom model"""
widget = widgets.AdminTagWidget(tag_model=RestaurantTag)
html = widget.render('tags', None, attrs={'id': 'alpha'})
params = self.get_js_init_params(html)
self.assertEqual(len(params), 4)
self.assertEqual(params[0], "'alpha'") # id
self.assertEqual(params[1], "'/admin/tag\\u002Dautocomplete/tests/restauranttag/'") # autocomplete url
self.assertEqual(params[2], 'true') # tag_spaces_allowed
self.assertEqual(params[3], 'null') # tag_limit

Wyświetl plik

@ -117,9 +117,22 @@ class AdminDateTimeInput(widgets.DateTimeInput):
class AdminTagWidget(TagWidget):
template_name = 'wagtailadmin/widgets/tag_widget.html'
def __init__(self, *args, **kwargs):
self.tag_model = kwargs.pop('tag_model', None)
super().__init__(*args, **kwargs)
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context['widget']['autocomplete_url'] = reverse('wagtailadmin_tag_autocomplete')
if self.tag_model:
autocomplete_url = reverse(
'wagtailadmin_tag_model_autocomplete',
args=(self.tag_model._meta.app_label, self.tag_model._meta.model_name)
)
else:
autocomplete_url = reverse('wagtailadmin_tag_autocomplete')
context['widget']['autocomplete_url'] = autocomplete_url
context['widget']['tag_spaces_allowed'] = getattr(settings, 'TAG_SPACES_ALLOWED', True)
context['widget']['tag_limit'] = getattr(settings, 'TAG_LIMIT', None)