Skip to content

Commit

Permalink
Allow overriding model's default ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
dpoirier committed Dec 1, 2017
1 parent 139a344 commit a5051df
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bread/bread.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,9 @@ def get_queryset(self):
(prefix, self.get_sort_field_name_for_column(column_number)))
# Add any ordering from the model's Meta data that isn't already included.
# That will make the rest of the sort stable, if the model has some default sort order.
default_ordering = getattr(self, 'default_ordering', qset.model._meta.ordering)
order_by_without_leading_dashes = [x.lstrip('-') for x in order_by]
for order_spec in qset.model._meta.ordering:
for order_spec in default_ordering:
if order_spec.lstrip('-') not in order_by_without_leading_dashes:
order_by.append(order_spec)
qset = qset.order_by(*order_by)
Expand Down
6 changes: 6 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
Change Log
==========

0.5.1 - Dec 1, 2017
-------------------

* Allow specifying default ordering on views for cases when we
don't control the model and can't change the ordering there.

0.5.0 - Nov 30, 2017
--------------------

Expand Down
4 changes: 4 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ sorting
(Otherwise, every time we show a new page, we could be working off a different
sorting of the results!) If nothing else, include a sort on the primary key.

If you do not have control of the model and so cannot change its ordering there,
you can add a ``default_ordering`` attribute to the browse view. Bread will use that
if present, instead of the model's ordering.

Configuring the browse view:

If the second item in the ``columns`` entry for a column is not a valid specification
Expand Down
30 changes: 30 additions & 0 deletions tests/test_browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ def test_sort_all_ascending(self, mock_logger):
# No exceptions logged
self.assertFalse(mock_logger.exception.called)

@patch('bread.templatetags.bread_tags.logger')
def test_sort_most_ascending_with_override_default_order(self, mock_logger):
self.set_urls(self.bread)
self.bread.browse_view.default_ordering = ['-other__text', 'age']
BreadTestModelFactory(name='999', other__text='012', age=50)
BreadTestModelFactory(name='555', other__text='333', age=60)
BreadTestModelFactory(name='111', other__text='555', age=10)
BreadTestModelFactory(name='111', other__text='555', age=20)
BreadTestModelFactory(name='111', other__text='555', age=5)
self.give_permission('browse')
url = reverse(self.bread.get_url_name('browse')) + '?o=0,1'
request = self.request_factory.get(url)
request.user = self.user
rsp = self.bread.get_browse_view()(request)
self.assertEqual(OK, rsp.status_code)
rsp.render()
results = rsp.context_data['object_list']

i = 0
while i < len(results) - 1:
sortA = (results[i].name, results[i].other.text)
sortB = (results[i+1].name, results[i+1].other.text)
self.assertLessEqual(sortA, sortB)
if sortA == sortB:
# default sort is 'age'
self.assertLessEqual(results[i].age, results[i+1].age)
i += 1
# No exceptions logged
self.assertFalse(mock_logger.exception.called)

@patch('bread.templatetags.bread_tags.logger')
def test_sort_all_descending(self, mock_logger):
self.set_urls(self.bread)
Expand Down

0 comments on commit a5051df

Please sign in to comment.