From e392e733836fd07db172977b5a0a8133bf8acbe6 Mon Sep 17 00:00:00 2001 From: TheoLechemia Date: Wed, 6 Dec 2023 15:24:27 +0100 Subject: [PATCH] - Set max character info only on configured fields - Improve UI when the limit is exceeded ref (https://github.com/GeotrekCE/Geotrek-admin/issues/3844) --- mapentity/forms.py | 13 +++++++---- .../static/mapentity/mapentity.helpers.js | 23 +++++++++++++------ mapentity/static/mapentity/style.css | 5 ++++ test_app/tests/test_forms.py | 9 +++++--- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/mapentity/forms.py b/mapentity/forms.py index d993e9a16..04e290e96 100644 --- a/mapentity/forms.py +++ b/mapentity/forms.py @@ -107,15 +107,18 @@ def __init__(self, *args, **kwargs): self.helper.form_tag = True # If MAX_CHARACTERS is setted, set help text for rich text fields - textfield_help_text = '' - max_characters = settings.MAPENTITY_CONFIG.get('MAX_CHARACTERS', None) - if max_characters: - textfield_help_text = _('%(max)s characters maximum recommended') % {'max': max_characters} - + max_characters_config = settings.MAPENTITY_CONFIG.get('MAX_CHARACTERS', {}) # Default widgets for fieldname, formfield in self.fields.items(): + textfield_help_text = '' # Custom code because formfield_callback does not work with inherited forms if formfield: + # set max character limit : + if self._meta.model._meta.db_table in max_characters_config: + for conf in max_characters_config[self._meta.model._meta.db_table]: + if fieldname == conf["field"]: + textfield_help_text = _('%(max)s characters maximum recommended') % {'max': conf["value"]} + # Assign map widget to all geometry fields try: formmodel = self._meta.model diff --git a/mapentity/static/mapentity/mapentity.helpers.js b/mapentity/static/mapentity/mapentity.helpers.js index cbc60574c..3371e7de5 100644 --- a/mapentity/static/mapentity/mapentity.helpers.js +++ b/mapentity/static/mapentity/mapentity.helpers.js @@ -107,14 +107,23 @@ function tr(s) { function tinyMceInit(editor) { - // Overflow on characters count + var context = $('body').data(); editor.on('WordCountUpdate', function(event) { - if (("container" in event.target) && (window.SETTINGS.maxCharacters > 0)) { - var characters = event.wordCount.characters; - if (characters > window.SETTINGS.maxCharacters) { - event.target.container.classList.add('cec-overflow'); - } else { - event.target.container.classList.remove('cec-overflow'); + if (("container" in event.target) && (window.SETTINGS.maxCharacters)) { + var fullTableName = context.appname+"_"+context.modelname + if (fullTableName in window.SETTINGS.maxCharacters) { + var currenInputName = event.target.container.previousSibling.name; + window.SETTINGS.maxCharacters[fullTableName].forEach(config => { + if(config.field == currenInputName) { + if(event.wordCount.characters > config.value) { + event.target.container.classList.add('cec-overflow'); + event.target.container.classList.add('is-invalid'); + } else { + event.target.container.classList.remove('cec-overflow'); + event.target.container.classList.remove('is-invalid'); + } + } + }) } } }); diff --git a/mapentity/static/mapentity/style.css b/mapentity/static/mapentity/style.css index 2483e5a74..c5e5a6950 100644 --- a/mapentity/static/mapentity/style.css +++ b/mapentity/static/mapentity/style.css @@ -842,6 +842,11 @@ label.requiredField .asteriskField { .cec-overflow.tox .tox-statusbar { background-color: pink; } +.tox.is-invalid { + border-color: #dc3545!important; + +} + /* diff --git a/test_app/tests/test_forms.py b/test_app/tests/test_forms.py index bf1342e63..4c2f6e107 100644 --- a/test_app/tests/test_forms.py +++ b/test_app/tests/test_forms.py @@ -29,7 +29,9 @@ def test_can_delete_actions(self): class MapEntityRichTextFormTest(TestCase): def setUp(self): - app_settings['MAX_CHARACTERS'] = 1200 + app_settings['MAX_CHARACTERS'] = { + "test_app_dummymodel": [{'field': 'short_description', 'value': 5}] + } @override_settings(MAPENTITY_CONFIG=app_settings) def test_max_characters(self): @@ -37,9 +39,10 @@ def test_max_characters(self): sample_object = DummyModel.objects.create() form = DummyForm(instance=sample_object) - self.assertIn('1200 characters maximum recommended', form.fields['description'].help_text) - self.assertIn('Short description, 1200 characters maximum recommended', + self.assertIn('', form.fields['description'].help_text) + self.assertIn('Short description, 5 characters maximum recommended', form.fields['short_description'].help_text) def tearDown(self): app_settings['MAX_CHARACTERS'] = 1200 +