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 a sort_posts function #2608

Merged
merged 9 commits into from Dec 28, 2016
Merged

Add a sort_posts function #2608

merged 9 commits into from Dec 28, 2016

Conversation

@Kwpolska
Copy link
Member

Kwpolska commented Dec 27, 2016

This is #2602 — it adds a sort_posts function (available as Jinja filter in global context), which allows general-purpose timeline sorting. Example usage:

sort_posts(timeline, 'title', 'date')
sort_posts(timeline, 'author', reverse=True)

cc @liffiton — does this match your needs?

@felixfontein
Copy link
Contributor

felixfontein commented Dec 27, 2016

From sort_posts(timeline, 'title', 'date') I'd assume that the posts are sorted by title, and if they have the same title, then they are sorted by date. But the code actually sorts them the other way around: first by date, and only for posts on the same date (i.e. with identical timestamp, as date means timestamp) by title.

@Kwpolska
Copy link
Member Author

Kwpolska commented Dec 27, 2016

That’s a sane assumption to make, I added a little reversed() call to account for this.

@felixfontein
Copy link
Contributor

felixfontein commented Dec 27, 2016

How about being able to sort some keys reversed, and some not, by prepending reverse_ to the key name? Standard use case is sorting by date and title: you might want to sort by date reversed to get the newest posts first, but by title alphabetically.

@liffiton
Copy link

liffiton commented Dec 27, 2016

Looks like a good solution to me. It would certainly handle what I'd like to do (and more).

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
@Kwpolska Kwpolska requested a review from felixfontein Dec 28, 2016
@Kwpolska Kwpolska added this to the v7.8.2 milestone Dec 28, 2016
@Kwpolska
Copy link
Member Author

Kwpolska commented Dec 28, 2016

@felixfontein done — prepend with - to get a key reversed (same syntax as used in Django). Review welcome.

# An attribute (or method) of the Post object
a = getattr(posts[0], key)
if callable(a):
keyfunc = operator.methodcaller(key)

This comment has been minimized.

Copy link
@felixfontein

felixfontein Dec 28, 2016

Contributor

Does this actually work when key[0] == '-'?

This comment has been minimized.

Copy link
@Kwpolska

Kwpolska Dec 28, 2016

Author Member

Oh, right. Fixed.

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
Copy link
Contributor

felixfontein left a comment

LGTM

@Kwpolska Kwpolska merged commit f592935 into master Dec 28, 2016
4 of 5 checks passed
4 of 5 checks passed
continuous-integration/travis-ci/pr The Travis CI build failed
Details
codacy/pr Good work! A positive pull request.
Details
continuous-integration/appveyor/branch AppVeyor build succeeded
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
@Kwpolska Kwpolska deleted the sort_posts branch Dec 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.