Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

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

Merged
lyschoening merged 2 commits into
biosustain:masterfrom
albertodonato:sort-attribute-desc
Nov 30, 2018
Merged

contrib/alchemy: Use sort_attribute as default sort for instances, support DESC sorting#152
lyschoening merged 2 commits into
biosustain:masterfrom
albertodonato:sort-attribute-desc

Conversation

@albertodonato

Copy link
Copy Markdown
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

coveralls commented Nov 19, 2018

Copy link
Copy Markdown

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
Copy Markdown
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

luord commented Nov 28, 2018

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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


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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants