Skip to content

Commit

Permalink
fix(filters): nested aggregator weren't implemented (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarreau authored Apr 5, 2023
1 parent f0d3550 commit 4361849
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
12 changes: 9 additions & 3 deletions django_forest/resources/utils/queryset/filters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,16 @@ def get_expression(self, condition, Model, tz):
def handle_aggregator(self, filters, Model, tz):
q_objects = Q()
for condition in filters['conditions']:
if filters['aggregator'] == 'or':
q_objects |= self.get_expression(condition, Model, tz)
if "aggregator" in condition:
if condition['aggregator'] == 'or':
q_objects |= self.handle_aggregator(condition, Model, tz)
else:
q_objects &= self.handle_aggregator(condition, Model, tz)
else:
q_objects &= self.get_expression(condition, Model, tz)
if filters['aggregator'] == 'or':
q_objects |= self.get_expression(condition, Model, tz)
else:
q_objects &= self.get_expression(condition, Model, tz)
return q_objects

def get_field_type(self, field, Model):
Expand Down
38 changes: 37 additions & 1 deletion django_forest/tests/resources/views/list/test_list_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_startswith(self, mocked_datetime, *args, **kwargs):
}
]
})

@mock.patch('jose.jwt.decode', return_value={'id': 1, 'rendering_id': 1})
def test_contains(self, mocked_datetime, *args, **kwargs):
response = self.client.get(self.url, {
Expand Down Expand Up @@ -184,6 +184,42 @@ def test_aggregator(self, *args, **kwargs):
]
})

@mock.patch('jose.jwt.decode', return_value={'id': 1, 'rendering_id': 1})
def test_nested_aggregator(self, *args, **kwargs):
response = self.client.get(self.url, {
'fields[tests_question]': 'id,topic,question_text,pub_date',
'fields[topic]': 'name',
'filters': '{"aggregator":"and","conditions":[{"field":"question_text","operator":"equal","value":"what is your favorite color?"},{"aggregator":"and","conditions":[{"field":"id","operator":"equal","value":1}, {"field": "id", "operator": "present", "value":null}]}]}',
'timezone': 'Europe/Paris',
'page[number]': '1',
'page[size]': '15'
})
data = response.json()
self.assertEqual(response.status_code, 200)
self.assertEqual(data, {
'data': [
{
'type': 'tests_question',
'id': 1,
'attributes': {
'pub_date': '2021-06-02T13:52:53.528000+00:00',
'question_text': 'what is your favorite color?'
},
'links': {
'self': '/forest/tests_question/1'
},
'relationships': {
'topic': {
'data': None,
'links': {
'related': '/forest/tests_question/1/relationships/topic'
}
}
},
}
]
})

@mock.patch('jose.jwt.decode', return_value={'id': 1, 'rendering_id': 1})
def test_aggregator_or(self, *args, **kwargs):
response = self.client.get(self.url, {
Expand Down

0 comments on commit 4361849

Please sign in to comment.