[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:
# Below code is a trick to be able to use "simple_choice" question
# with values 'yes' and 'no' and transform it to boolean.
if boolean_value := answer_value_char in [
"1",
"True",
"true",
"Oui",
"oui",
"Yes",
"yes",
]:
return boolean_value
else:
raise UserError(
_(
"[Survey record generation] The boolean value %s(value)s "
"is not supported (for question %(question)s)."
)
% {
"value": answer_value_char,
"question": question_title,
}
def _get_boolean_true_values(self):
"""Tokens interpreted as true, normalized to lowercase.
Instance method so that a third-party module or a localization can
extend the list without rewriting get_boolean_value.
"""
return {"1", "true", "vrai", "yes", "y", "oui", "o", "on", "x"}
def _get_boolean_false_values(self):
"""Tokens interpreted as false, normalized to lowercase."""
return {"0", "false", "faux", "no", "n", "non", "off", ""}
def get_boolean_value(self, answer_value_char: str, question_title: str) -> bool:
"""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(
_(
"[Survey record generation] The boolean value %(value)s is not "
"supported (for question %(question)s)"
)
% {
"value": answer_value_char,
"question": question_title,
}
)