[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,28 +315,44 @@ 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.
raise UserError(
_( Allows a boolean field to be filled from a "simple_choice" question
"[Survey record generation] The boolean value %s(value)s " whose suggested answers carry yes/no values.
"is not supported (for question %(question)s)." """
) if not answer_value_char:
% { # Empty answer: an unset boolean is false, this is not a
"value": answer_value_char, # configuration error.
"question": question_title, 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(
_(
"[Survey record generation] The boolean value %(value)s is not "
"supported (for question %(question)s)"
) )
% {
"value": answer_value_char,
"question": question_title,
}
)