[FIX] survey_record_generation allow negative value in boolean question
Some checks failed
pre-commit / pre-commit (pull_request) Has been cancelled

This commit is contained in:
2026-09-01 13:57:25 +02:00
parent 1cece14084
commit e49803baca

View File

@@ -315,25 +315,41 @@ class SurveyUserInput(models.Model):
} }
) )
@staticmethod
def get_boolean_value(answer_value_char: str, question_title: str) -> bool: def _get_boolean_true_values(self):
# Below code is a trick to be able to use "simple_choice" question """Tokens interpreted as true, normalized to lowercase.
# with values 'yes' and 'no' and transform it to boolean.
if boolean_value := answer_value_char in [ Instance method so that a third-party module or a localization can
"1", extend the list without rewriting get_boolean_value.
"True", """
"true", return {"1", "true", "vrai", "yes", "y", "oui", "o", "on", "x"}
"Oui",
"oui", def _get_boolean_false_values(self):
"Yes", """Tokens interpreted as false, normalized to lowercase."""
"yes", return {"0", "false", "faux", "no", "n", "non", "off", ""}
]:
return boolean_value def get_boolean_value(self, answer_value_char: str, question_title: str) -> bool:
else: """Convert the technical value of an answer into a boolean.
Allows a boolean field to be filled from a "simple_choice" question
whose suggested answers carry yes/no values.
"""
if not answer_value_char:
# Empty answer: an unset boolean is false, this is not a
# configuration error.
return False
token = str(answer_value_char).strip().casefold()
if token in self._get_boolean_true_values():
return True
if token in self._get_boolean_false_values():
return False
raise UserError( raise UserError(
_( _(
"[Survey record generation] The boolean value %s(value)s " "[Survey record generation] The boolean value %(value)s is not "
"is not supported (for question %(question)s)." "supported (for question %(question)s)"
) )
% { % {
"value": answer_value_char, "value": answer_value_char,