Skip to content

Commit

Permalink
- Set max character info only on configured fields
Browse files Browse the repository at this point in the history
- Improve UI when the limit is exceeded
ref (GeotrekCE/Geotrek-admin#3844)
  • Loading branch information
TheoLechemia committed Dec 12, 2023
1 parent 3f3a949 commit e392e73
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
13 changes: 8 additions & 5 deletions mapentity/forms.py
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions mapentity/static/mapentity/mapentity.helpers.js
Expand Up @@ -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');
}
}
})
}
}
});
Expand Down
5 changes: 5 additions & 0 deletions mapentity/static/mapentity/style.css
Expand Up @@ -842,6 +842,11 @@ label.requiredField .asteriskField {
.cec-overflow.tox .tox-statusbar {
background-color: pink;
}
.tox.is-invalid {
border-color: #dc3545!important;

}



/*
Expand Down
9 changes: 6 additions & 3 deletions test_app/tests/test_forms.py
Expand Up @@ -29,17 +29,20 @@ 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):
"""Test if help text is set with MAX_CHARACTERS setting"""
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

0 comments on commit e392e73

Please sign in to comment.