Skip to content

Commit

Permalink
Add combiend view model index order by
Browse files Browse the repository at this point in the history
  • Loading branch information
daanvdk committed Jul 5, 2024
1 parent 3085224 commit 01c0941
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
35 changes: 23 additions & 12 deletions binder/plugins/views/combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,30 @@ def combined_view(request, router, names):
queryset = querysets[name]
suborder_bys = []
id_expr = F('id') * Value(len(names)) + Value(i)

for order_by in order_bys:
# So this is a bit of a hack, filtering on annotations is normally
# only possible on the top model so binder assumes this is always
# the case which is valid. However in our case we want to always
# filter on the submodel. We thus patch the annotations function
# temporarily
old_annotations = views[name].annotations
views[name].annotations = lambda *args, **kwargs: include_annotations.get(name)
try:
queryset, field, nulls_last = views[name]._parse_order_by(queryset, order_by.fields[name], request)
finally:
views[name].annotations = old_annotations
suborder_bys.append(id_expr if field == 'id' else F(field))
if field == 'id':
suborder_by = id_expr

elif field == 'model_index':
suborder_by = Value(i)

else:
# So this is a bit of a hack, filtering on annotations is normally
# only possible on the top model so binder assumes this is always
# the case which is valid. However in our case we want to always
# filter on the submodel. We thus patch the annotations function
# temporarily
old_annotations = views[name].annotations
views[name].annotations = lambda *args, **kwargs: include_annotations.get(name)
try:
queryset, field, nulls_last = views[name]._parse_order_by(queryset, order_by.fields[name], request)
finally:
views[name].annotations = old_annotations
suborder_by = F(field)

suborder_bys.append(suborder_by)

queryset = queryset.values_list(
id_expr,
*suborder_bys,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,31 @@ def test_combined_order_by_multi_field(self):
animal2.id * 2 + 1, # Harambe
zoo1.id * 2, # Apenheul
])

def test_combined_order_by_combined_index(self):
zoo1 = Zoo.objects.create(name='Apenheul', founding_date='1980-01-01')
zoo2 = Zoo.objects.create(name='Emmen', founding_date='1990-01-01')
animal1 = Animal.objects.create(zoo=zoo1, name='Bokito', birth_date='1995-01-01')
animal2 = Animal.objects.create(zoo=zoo2, name='Harambe', birth_date='1985-01-01')

res = self.client.get(f'/combined/zoo/animal/?order_by=model_index')
self.assertEqual(res.status_code, 200)
data = json.loads(res.content)
ids = [obj['id'] for obj in data['data']]
self.assertEqual(ids, [
zoo1.id * 2, # Apenheul
zoo2.id * 2, # Emmen
animal1.id * 2 + 1, # Bokito
animal2.id * 2 + 1, # Harambe
])

res = self.client.get(f'/combined/zoo/animal/?order_by=-model_index')
self.assertEqual(res.status_code, 200)
data = json.loads(res.content)
ids = [obj['id'] for obj in data['data']]
self.assertEqual(ids, [
animal1.id * 2 + 1, # Bokito
animal2.id * 2 + 1, # Harambe
zoo1.id * 2, # Apenheul
zoo2.id * 2, # Emmen
])

0 comments on commit 01c0941

Please sign in to comment.