Commit c92b9d33 authored by Hueser, Christian (FWCC) - 138593's avatar Hueser, Christian (FWCC) - 138593
Browse files

Resolve "Values are all transformed to True if casted to bool when adding data to questions"

parent 63bfa31d
Pipeline #102912 passed with stages
in 4 minutes and 32 seconds
......@@ -25,6 +25,7 @@ These can be constructed from YAML through the YamlConstructable abstract
class.
"""
# alias name to avoid clash with schema.Optional
import logging
from typing import Dict, List, Optional, Set
import schema
......@@ -35,9 +36,7 @@ from hifis_surveyval.models.answer_option import AnswerOption
from hifis_surveyval.models.answer_types import VALID_ANSWER_TYPES
from hifis_surveyval.models.mixins.identifiable import Identifiable
from hifis_surveyval.models.mixins.yaml_constructable import (
YamlConstructable,
YamlDict,
)
YamlConstructable, YamlDict)
from hifis_surveyval.models.translated import Translated
......@@ -172,6 +171,19 @@ class Question(YamlConstructable, Identifiable):
value = self._answer_type(option.label)
# FIXME change: answer option values become a separate field,
# no longer derived from labels
elif self._answer_type == bool:
# When casting to boolean values, Python casts any non-empty string
# to True and only empty strings to False. Consequently, values
# are transformed according to a set of valid true and false
# values to allow for different truth values.
if value in self._settings.TRUE_VALUES:
value = True
elif value in self._settings.FALSE_VALUES:
value = False
else:
logging.error(f"Boolean data is an invalid truth value "
f"in question {self.full_id}: {value}.")
value = None
else:
# try to cast the answer value to the expected type
value = self._answer_type(value)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment