From e49803bacaec0d1bec21026cbc53ff1d670aa993 Mon Sep 17 00:00:00 2001 From: maud-buchwalter Date: Tue, 1 Sep 2026 13:57:25 +0200 Subject: [PATCH] [FIX] survey_record_generation allow negative value in boolean question --- .../models/survey_user_input.py | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/survey_record_generation/models/survey_user_input.py b/survey_record_generation/models/survey_user_input.py index a490741..f8e3930 100644 --- a/survey_record_generation/models/survey_user_input.py +++ b/survey_record_generation/models/survey_user_input.py @@ -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, + } + )