From acd21b2fdb593d8f45482e01ac81446e167054d2 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 15 May 2024 18:37:56 +0300 Subject: [PATCH] refactor: [ACI-976] upgrade boolean values check in datarules --- credentials/apps/badges/models.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/credentials/apps/badges/models.py b/credentials/apps/badges/models.py index e79a01f4c1..a44aa8490e 100644 --- a/credentials/apps/badges/models.py +++ b/credentials/apps/badges/models.py @@ -297,10 +297,25 @@ def apply(self, data: dict) -> bool: """ comparison_func = getattr(operator, self.operator, None) + if comparison_func: data_value = str(keypath(data, self.data_path)) - return comparison_func(data_value, self.value) + return comparison_func(data_value, self._value_to_bool()) return False + + def _value_to_bool(self): + """ + Converts the value to a boolean or returns the original value if it is not a boolean string. + """ + + TRUE_VALUES = ["True", "true", "Yes", "yes", "+", "1"] + FALSE_VALUES = ["False", "false", "No", "no", "-", "0"] + + if self.value in TRUE_VALUES: + return "True" + if self.value in FALSE_VALUES: + return "False" + return self.value class DataRule(AbstractDataRule):