Skip to content

Commit

Permalink
Keep old paramter MAX_CHARACTERS and add a deprecation warning on it
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoLechemia committed Feb 26, 2024
1 parent f2c97a8 commit 18cc0d5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ CHANGELOG
8.6.1+dev (XXXX-XX-XX)
-----------------------

- Fix of the widget `SelectMultipleWithPop` which did not add the newly created element in the related list
- Add `MAX_CHARACTERS_BY_FIELD` to control the max length of a rich text field.
- Deprecate the `MAX_CHARACTERS` paramter


8.6.1 (2023-09-18)
Expand Down
9 changes: 6 additions & 3 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,12 @@ Or change just one parameter (the opacity for example) :
Edition
'''''''

For rich text fields, it is possible to a max number of caracters (spaces includes).
A help message will be added, and color of TinyMCE status bar will be colored in pink.
For rich text fields, it is possible to indicate a max number of characters on a specified field (spaces includes).
A help message will be added, and color of TinyMCE status bar and border will be colored in red.

.. code-block :: python
MAPENTITY_CONFIG['MAX_CHARACTERS'] = 1500
MAPENTITY_CONFIG['MAX_CHARACTERS_BY_FIELD'] = {
"tourism_touristicevent": [{'field': 'description_teaser_fr', 'value': 50}, {'field': 'accessibility_fr', 'value': 25}],
"trekking_trek": [{'field': 'description_teaser_fr', 'value': 150}],
}
18 changes: 15 additions & 3 deletions mapentity/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import copy

from warnings import warn

from crispy_forms.bootstrap import FormActions
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Button, HTML, Submit
Expand Down Expand Up @@ -107,15 +109,25 @@ def __init__(self, *args, **kwargs):
self.helper.form_tag = True

# If MAX_CHARACTERS is setted, set help text for rich text fields
max_characters_config = settings.MAPENTITY_CONFIG.get('MAX_CHARACTERS', {}) or {}
textfield_help_text = ''
max_characters = settings.MAPENTITY_CONFIG.get('MAX_CHARACTERS', None)
if max_characters:
warn(
"Parameters MAX_CHARACTERS is deprecated, please use MAX_CHARACTERS_BY_FIELD instead",
DeprecationWarning,
stacklevel=2
)
textfield_help_text = _('%(max)s characters maximum recommended') % {'max': max_characters}

max_characters_by_field_config = settings.MAPENTITY_CONFIG.get('MAX_CHARACTERS_BY_FIELD', {}) or {}
# 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 self._meta.model._meta.db_table in max_characters_by_field_config:
for conf in max_characters_by_field_config[self._meta.model._meta.db_table]:
if fieldname == conf["field"]:
textfield_help_text = _('%(max)s characters maximum recommended') % {'max': conf["value"]}

Expand Down
16 changes: 13 additions & 3 deletions mapentity/static/mapentity/mapentity.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,21 @@ function tr(s) {
function tinyMceInit(editor) {
var context = $('body').data();
editor.on('WordCountUpdate', function(event) {
if (("container" in event.target) && (window.SETTINGS.maxCharacters)) {
console.log(window.SETTINGS);
// DEPRECATED paramters maxCharacters -> to remove
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.maxCharactersByField)) {
var fullTableName = context.appname+"_"+context.modelname
if (fullTableName in window.SETTINGS.maxCharacters) {
if (fullTableName in window.SETTINGS.maxCharactersByField) {
var currenInputName = event.target.container.previousSibling.name;
window.SETTINGS.maxCharacters[fullTableName].forEach(config => {
window.SETTINGS.maxCharactersByField[fullTableName].forEach(config => {
if(config.field == currenInputName) {
var statusBar = $(event.target.container).find(".tox-statusbar__wordcount");
$(event.target.container).find(".injectedCount").remove()
Expand Down
2 changes: 2 additions & 0 deletions mapentity/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ class ModelName(mapentity_models.MapEntityMixin, models.Model):
# Languages
dictsettings['languages'] = dict(available=dict(app_settings['TRANSLATED_LANGUAGES']),
default=app_settings['LANGUAGE_CODE'])
# MAX_CHARACTERS paramters is deprecated : to remove
dictsettings['maxCharacters'] = app_settings['MAX_CHARACTERS']
dictsettings['maxCharactersByField'] = app_settings['MAX_CHARACTERS_BY_FIELD']
return dictsettings


Expand Down

0 comments on commit 18cc0d5

Please sign in to comment.