Add a setting to apply limit on number of tags that can be added to any tagged model.

pull/5056/head
Mani 2019-03-04 15:17:48 -05:00 zatwierdzone przez LB Johnston
rodzic 3fe442ff4d
commit 87bbc31d88
7 zmienionych plików z 79 dodań i 3 usunięć

Wyświetl plik

@ -16,6 +16,7 @@ Changelog
* Renamed `target_model` argument on `PageChooserBlock` to `page_type` (Loic Teixeira)
* `edit_handler` and `panels` can now be defined on a `ModelAdmin` definition (Thomas Kremmel)
* Add Learn Wagtail to third-party tutorials in documentation (Matt Westcott)
* Add a Django setting `TAG_LIMIT` to limit number of tags that can be added to any taggit model (Mani)
* Fix: Set `SERVER_PORT` to 443 in `Page.dummy_request()` for HTTPS sites (Sergey Fedoseev)
* Fix: Include port number in `Host` header of `Page.dummy_request()` (Sergey Fedoseev)
* Fix: Validation error messages in `InlinePanel` no longer count towards `max_num` when disabling the 'add' button (Todd Dembrey, Thibaud Colas)

Wyświetl plik

@ -384,6 +384,15 @@ Multi-word tags
Tags can only consist of a single word, no spaces allowed. The default setting is ``True`` (spaces in tags are allowed).
Tag limit
---------
.. code-block:: python
TAG_LIMIT = 5
Limit the number of tags that can be added to (django-taggit) Tag model. Default setting is ``None``, meaning no limit on tags.
Unicode Page Slugs
------------------

Wyświetl plik

@ -26,6 +26,7 @@ Other features
* Renamed ``target_model`` argument on ``PageChooserBlock`` to ``page_type`` (Loic Teixeira)
* ``edit_handler`` and ``panels`` can now be defined on a ``ModelAdmin`` definition (Thomas Kremmel)
* Add Learn Wagtail to third-party tutorials in documentation (Matt Westcott)
* Add a Django setting ``TAG_LIMIT`` to limit number of tags that can be added to any taggit model (Mani)
Bug fixes

Wyświetl plik

@ -21,7 +21,7 @@ function escapeHtml(text) {
});
}
function initTagField(id, autocompleteUrl, allowSpaces) {
function initTagField(id, autocompleteUrl, allowSpaces, tagLimit) {
$('#' + id).tagit({
autocomplete: {source: autocompleteUrl},
preprocessTag: function(val) {
@ -34,7 +34,8 @@ function initTagField(id, autocompleteUrl, allowSpaces) {
return val;
},
allowSpaces: allowSpaces
allowSpaces: allowSpaces,
tagLimit: tagLimit,
});
}

Wyświetl plik

@ -1 +1,9 @@
{% include 'django/forms/widgets/text.html' %}<script>initTagField('{{ widget.attrs.id|escapejs }}', '{{ widget.autocomplete_url|escapejs }}', {% if widget.tag_spaces_allowed %}true{% else %}false{% endif %});</script>
{% include 'django/forms/widgets/text.html' %}
<script>
initTagField(
'{{ widget.attrs.id|escapejs }}',
'{{ widget.autocomplete_url|escapejs }}',
{% if widget.tag_spaces_allowed %}true{% else %}false{% endif %},
{% if widget.tag_limit %}{{widget.tag_limit}}{% else %}null{% endif %}
);
</script>

Wyświetl plik

@ -157,3 +157,58 @@ class TestAdminDateTimeInput(TestCase):
'"format": "d.m.Y. H:i"',
html,
)
class TestAdminTagWidget(TestCase):
def get_js_init_params(self, html):
"""Returns a list of the params passed in to initTagField from the supplied HTML"""
# Eg. ["'test\\u002Did'", "'/admin/tag\\u002Dautocomplete/'", 'true', 'null']
start = 'initTagField('
end = ');'
items_after_init = html.split(start)[1]
if items_after_init:
params_raw = items_after_init.split(end)[0]
if params_raw:
return [part.strip() for part in params_raw.split(',')]
return []
def test_render_js_init_basic(self):
"""Chekcs that the 'initTagField' is correctly added to the inline script for tag widgets"""
widget = widgets.AdminTagWidget()
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/'") # autocomplete url
self.assertEqual(params[2], 'true') # tag_spaces_allowed
self.assertEqual(params[3], 'null') # tag_limit
@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"""
widget = widgets.AdminTagWidget()
html = widget.render('tags', None, attrs={'id': 'alpha'})
params = self.get_js_init_params(html)
self.assertEqual(len(params), 4)
self.assertEqual(params[2], 'false') # tag_spaces_allowed
self.assertEqual(params[3], 'null') # tag_limit
@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"""
widget = widgets.AdminTagWidget()
html = widget.render('tags', None, attrs={'id': 'alpha'})
params = self.get_js_init_params(html)
self.assertEqual(len(params), 4)
self.assertEqual(params[2], 'true') # tag_spaces_allowed
self.assertEqual(params[3], '5') # tag_limit

Wyświetl plik

@ -101,6 +101,7 @@ class AdminTagWidget(TagWidget):
context = super().get_context(name, value, attrs)
context['widget']['autocomplete_url'] = reverse('wagtailadmin_tag_autocomplete')
context['widget']['tag_spaces_allowed'] = getattr(settings, 'TAG_SPACES_ALLOWED', True)
context['widget']['tag_limit'] = getattr(settings, 'TAG_LIMIT', None)
return context