diff --git a/apps/content_filters/filter_condition/filter_condition.py b/apps/content_filters/filter_condition/filter_condition.py index c0c5f83c81..c2890b8500 100644 --- a/apps/content_filters/filter_condition/filter_condition.py +++ b/apps/content_filters/filter_condition/filter_condition.py @@ -60,10 +60,7 @@ def does_match(self, article): type(self.operator) is NotInOperator or type(self.operator) is NotLikeOperator or self.operator.operator is FilterConditionOperatorsEnum.ne - or ( - self.operator.operator is FilterConditionOperatorsEnum.eq - and self.value.value.lower() in ("no", "false", "f", "0") - ) + or (self.operator.operator is FilterConditionOperatorsEnum.eq and self.value.is_false()) ) article_value = self.field.get_value(article) diff --git a/apps/content_filters/filter_condition/filter_condition_operator.py b/apps/content_filters/filter_condition/filter_condition_operator.py index 0b46740b2e..6c2fc135e6 100644 --- a/apps/content_filters/filter_condition/filter_condition_operator.py +++ b/apps/content_filters/filter_condition/filter_condition_operator.py @@ -30,6 +30,10 @@ class FilterConditionOperatorsEnum(Enum): class FilterConditionOperator: + operator: FilterConditionOperatorsEnum + mongo_operator: str + elastic_operator: str + @staticmethod def factory(operator): if operator + "_" == FilterConditionOperatorsEnum.in_.name: diff --git a/apps/content_filters/filter_condition/filter_condition_value.py b/apps/content_filters/filter_condition/filter_condition_value.py index 0bcef3897a..6fdd3e43a1 100644 --- a/apps/content_filters/filter_condition/filter_condition_value.py +++ b/apps/content_filters/filter_condition/filter_condition_value.py @@ -9,6 +9,7 @@ # at https://www.sourcefabric.org/superdesk/license import re from apps.content_filters.filter_condition.filter_condition_operator import ( + FilterConditionOperator, FilterConditionOperatorsEnum, ComparisonOperator, ExistsOperator, @@ -31,9 +32,9 @@ class FilterConditionValue: FilterConditionOperatorsEnum.match: "{}:{}", } - def __init__(self, operator, value): + def __init__(self, operator: FilterConditionOperator, value): self.operator = operator - self.value = value + self.value = str(value) self.mongo_regex = self.mongo_mapper.get(operator.operator) self.elastic_regex = self.elastic_mapper.get(operator.operator) @@ -41,7 +42,7 @@ def get_value(self, field, operator): if isinstance(operator, ComparisonOperator) or isinstance(operator, ExistsOperator): t = field.get_type() if t is bool: - return self.value.lower() in ("yes", "true", "t", "1") + return self.is_true() return t(self.value) else: return self.get_mongo_value(field) @@ -56,7 +57,7 @@ def get_elastic_value(self, field, operator): if isinstance(operator, ComparisonOperator): t = field.get_type() if t is bool: - return self.value.lower() in ("yes", "true", "t", "1"), field.get_entity_name() + return self.is_true(), field.get_entity_name() return t(self.value), field.get_entity_name() if self.elastic_regex: @@ -69,7 +70,12 @@ def _get_regex_value(self): def _get_value(self, field): t = field.get_type() - value = str(self.value) - if value.find(",") > 0: - return [t(x) for x in value.strip().split(",")] - return [t(value)] + if self.value.find(",") > 0: + return [t(x) for x in self.value.strip().split(",")] + return [t(self.value)] + + def is_true(self) -> bool: + return self.value.lower() in ("yes", "true", "t", "1") + + def is_false(self) -> bool: + return self.value.lower() in ("no", "false", "f", "0") diff --git a/tests/content_filters/filter_condition_tests.py b/tests/content_filters/filter_condition_tests.py index f1af3fd36f..b4bd00569b 100644 --- a/tests/content_filters/filter_condition_tests.py +++ b/tests/content_filters/filter_condition_tests.py @@ -8,11 +8,13 @@ # AUTHORS and LICENSE files distributed with this source code, or # at https://www.sourcefabric.org/superdesk/license import json +import bson import re import os from datetime import timedelta from eve.utils import ParsedRequest +from apps.content_filters.filter_condition.filter_condition_field import FilterConditionDeskField from superdesk.utc import utcnow from superdesk import get_resource_service @@ -20,6 +22,7 @@ from apps.content_filters.filter_condition.filter_condition_service import FilterConditionService from apps.content_filters.filter_condition.filter_condition import FilterCondition from apps.content_filters.filter_condition.filter_condition_operator import FilterConditionOperator +from apps.content_filters.filter_condition.filter_condition_value import FilterConditionValue from apps.prepopulate.app_populate import AppPopulateCommand @@ -807,3 +810,9 @@ def test_elastic_using_featuremedia_exists(self): doc_ids = [d["_id"] for d in docs] self.assertEqual(1, docs.count()) self.assertTrue("4" in doc_ids) + + def test_filter_condition_value_deserialized(self): + desk_id = bson.ObjectId() + field = FilterConditionDeskField("") + value = FilterConditionValue(FilterConditionOperator.factory("in"), desk_id) + self.assertEqual([str(desk_id)], value._get_value(field))