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

contrib/alchemy: Use sort_attribute as default sort for instances, support DESC sorting #152

Merged
merged 2 commits into from
Nov 30, 2018
Merged

contrib/alchemy: Use sort_attribute as default sort for instances, support DESC sorting #152

merged 2 commits into from
Nov 30, 2018

Conversation

albertodonato
Copy link
Contributor

Use the sort_attribute if defined instead of id_attribute to sort instances by default.
Also, allow reversing default sorting by prefixing the attribute name with -

@coveralls
Copy link

coveralls commented Nov 19, 2018

Coverage Status

Coverage increased (+0.01%) to 89.111% when pulling b39e520 on albertodonato:sort-attribute-desc into 2e1a394 on biosustain:master.

@lyschoening
Copy link
Contributor

There's an existing PR in #138 for sort attribute. While your implementation is more proper in that it edits the default_sort_expression; the other PR uses a syntax of attribute, reverse for the sort attribute, which is more in keeping with how sorting works elsewhere in the library. It also adds documentation.

Would you be willing to update your code to use @luord's syntax and documentation? If not, let me know and I will do this after merging.

@luord
Copy link
Contributor

luord commented Nov 28, 2018

Correct me if I'm wrong, but wouldn't this do the sort only for SQLAlchemy? What I did in my PR was at the resource level, independent of the manager. It was a while ago and I don't remember everything about the code so I could be mistaken, though.

@albertodonato
Copy link
Contributor Author

@luord correct, this is for sqlalchemy.

@lyschoening I've ported the relevant changes from #138 in this branch, FWIW

@lyschoening lyschoening merged commit 66f11dc into biosustain:master Nov 30, 2018
@albertodonato albertodonato deleted the sort-attribute-desc branch November 30, 2018 12:29
response = self.client.get('/machine?sort={"type": false}')
self.assert200(response)
type_uris = [entry['type']['$ref'] for entry in response.json]
self.assertTrue(type_uris, [bbb_uri, aaa_uri])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we pass true of false the test check the same thing

@@ -86,6 +87,14 @@ def _init_model(self, resource, model, meta):
fs.required.add(name)
fs.set(name, self._get_field_from_column_type(column, name, io=io))

def _get_sort_expression(self, model, meta, id_column):
if meta.sort_attribute is None:
return id_column.asc()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it sort everything by default on primary key column ?
What if we do not want to have this behaviour ?
Sorting can have substantial impact on performance for instance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That has been the behavior for a while. Without an intrinsic sort order, it is not possible to do reliable pagination. Since the primary key is indexed, it is a good default. Do you see a practical scenario where no sort is preferred? We could support sort_attribute=False to mean no sorting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, that's a good point.
I'd take inspiration from django. If Pagination is activated and if the query is not sorted, we could raise a warning. But having a default on primary key is not that interesting for our use case (we would prefer on last_updated for instance). Though, not a big deal either. Feel free to ignore.

reference:
https://github.com/django/django/blob/ed4bfacb3c942d0d32795e1a733b1b9367afd2e9/django/core/paginator.py#L110

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants