From b244f49e19bf1a8f7f7b58569bee7e5ca62ca457 Mon Sep 17 00:00:00 2001 From: David Glick Date: Wed, 14 Jun 2023 13:17:58 -0700 Subject: [PATCH 1/2] Fix int operations to actually accept integers --- plone/app/querystring/queryparser.py | 31 ++++++++---------- .../app/querystring/tests/testQueryParser.py | 32 ++++++++++++++++--- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/plone/app/querystring/queryparser.py b/plone/app/querystring/queryparser.py index d32e1ea..bdc5175 100644 --- a/plone/app/querystring/queryparser.py +++ b/plone/app/querystring/queryparser.py @@ -112,11 +112,13 @@ def _intEqual(context, row): values = [int(v) for v in row.values] except (ValueError, TypeError, AttributeError): pass - elif not isinstance(row.values, int): + else: try: values = int(row.values) except (ValueError, TypeError, AttributeError): pass + if values is None: + return {} return {row.index: {"query": values}} @@ -154,19 +156,16 @@ def _largerThan(context, row): def _intLargerThan(context, row): - value = None - if not isinstance(row.values, int): - try: - value = int(row.values) - except (ValueError, TypeError, AttributeError): - pass - tmp = { + try: + value = int(row.values) + except (ValueError, TypeError, AttributeError): + return {} + return { row.index: { "query": value, "range": "min", }, } - return tmp def _lessThan(context, row): @@ -180,20 +179,16 @@ def _lessThan(context, row): def _intLessThan(context, row): - value = None - if not isinstance(row.values, int): - try: - value = int(row.values) - except (ValueError, TypeError, AttributeError): - # value = 0 - pass - tmp = { + try: + value = int(row.values) + except (ValueError, TypeError, AttributeError): + return {} + return { row.index: { "query": value, "range": "max", }, } - return tmp def _currentUser(context, row): diff --git a/plone/app/querystring/tests/testQueryParser.py b/plone/app/querystring/tests/testQueryParser.py index f2e2e04..c183398 100644 --- a/plone/app/querystring/tests/testQueryParser.py +++ b/plone/app/querystring/tests/testQueryParser.py @@ -327,6 +327,18 @@ def test__equal(self): self.assertEqual(parsed, expected) def test__intEqual(self): + # int + data = Row(index="modified", operator="_intEqual", values=20) + parsed = queryparser._intEqual(MockSite(), data) + expected = {"modified": {"query": 20}} + self.assertEqual(parsed, expected) + + # list of ints + data = Row(index="modified", operator="_intEqual", values=[20, 21]) + parsed = queryparser._intEqual(MockSite(), data) + expected = {"modified": {"query": [20, 21]}} + self.assertEqual(parsed, expected) + # bytes data = Row(index="modified", operator="_intEqual", values=b"20") parsed = queryparser._intEqual(MockSite(), data) @@ -354,7 +366,7 @@ def test__intEqual(self): # bad text data = Row(index="modified", operator="_intEqual", values="bad") parsed = queryparser._intEqual(MockSite(), data) - expected = {"modified": {"query": None}} + expected = {} self.assertEqual(parsed, expected) # list of bad text @@ -362,7 +374,7 @@ def test__intEqual(self): index="modified", operator="_intEqual", values=[b"bad", "text", "values"] ) parsed = queryparser._intEqual(MockSite(), data) - expected = {"modified": {"query": None}} + expected = {} self.assertEqual(parsed, expected) def test__lessThan(self): @@ -372,6 +384,12 @@ def test__lessThan(self): self.assertEqual(parsed, expected) def test__intLessThan(self): + # int + data = Row(index="modified", operator="_intLessThan", values=20) + parsed = queryparser._intLessThan(MockSite(), data) + expected = {"modified": {"query": 20, "range": "max"}} + self.assertEqual(parsed, expected) + # bytes data = Row(index="modified", operator="_intLessThan", values=b"20") parsed = queryparser._intLessThan(MockSite(), data) @@ -387,7 +405,7 @@ def test__intLessThan(self): # bad value data = Row(index="modified", operator="_intLessThan", values="bad") parsed = queryparser._intLessThan(MockSite(), data) - expected = {"modified": {"query": None, "range": "max"}} + expected = {} self.assertEqual(parsed, expected) def test__largerThan(self): @@ -397,6 +415,12 @@ def test__largerThan(self): self.assertEqual(parsed, expected) def test__intLargerThan(self): + # int + data = Row(index="modified", operator="_intLargerThan", values=20) + parsed = queryparser._intLargerThan(MockSite(), data) + expected = {"modified": {"query": 20, "range": "min"}} + self.assertEqual(parsed, expected) + # bytes data = Row(index="modified", operator="_intLargerThan", values=b"20") parsed = queryparser._intLargerThan(MockSite(), data) @@ -412,7 +436,7 @@ def test__intLargerThan(self): # bad value data = Row(index="modified", operator="_intLargerThan", values="bad") parsed = queryparser._intLargerThan(MockSite(), data) - expected = {"modified": {"query": None, "range": "min"}} + expected = {} self.assertEqual(parsed, expected) def test__currentUser(self): From 1c34acb488379428d1827126de6240ae892546fd Mon Sep 17 00:00:00 2001 From: David Glick Date: Wed, 14 Jun 2023 13:20:03 -0700 Subject: [PATCH 2/2] changelog --- news/131.bugfix | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 news/131.bugfix diff --git a/news/131.bugfix b/news/131.bugfix new file mode 100644 index 0000000..fc7541d --- /dev/null +++ b/news/131.bugfix @@ -0,0 +1,2 @@ +Fix integer operations to accept integers. @davisagli +Fix integer operations to avoid adding None to the query when the input is not valid. @davisagli