Compare commits
1 Commits
16.0-save-
...
16.0
| Author | SHA1 | Date | |
|---|---|---|---|
| e49803baca |
@@ -315,25 +315,41 @@ 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:
|
||||
|
||||
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 %s(value)s "
|
||||
"is not supported (for question %(question)s)."
|
||||
"[Survey record generation] The boolean value %(value)s is not "
|
||||
"supported (for question %(question)s)"
|
||||
)
|
||||
% {
|
||||
"value": answer_value_char,
|
||||
|
||||
Reference in New Issue
Block a user