Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add combined view model index order by #244

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions binder/plugins/views/combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,32 @@ 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))
field = order_by.fields[name]

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, field, 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
])
Loading