Skip to content

Commit

Permalink
fix filter condition value errors with cache (#2444)
Browse files Browse the repository at this point in the history
there were other places where it was using the value
assuming it's a string, so cast it when creating an instance.

SDESK-6883
  • Loading branch information
petrjasek authored Apr 17, 2023
1 parent 2c1eb40 commit e55d3dc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
5 changes: 1 addition & 4 deletions apps/content_filters/filter_condition/filter_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 14 additions & 8 deletions apps/content_filters/filter_condition/filter_condition_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,17 +32,17 @@ 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)

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)
Expand All @@ -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:
Expand All @@ -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")
9 changes: 9 additions & 0 deletions tests/content_filters/filter_condition_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
# 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
from superdesk.tests import TestCase
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


Expand Down Expand Up @@ -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))

0 comments on commit e55d3dc

Please sign in to comment.