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

Allow to reverse the sorting order on search function #433

Merged
merged 2 commits into from
Aug 13, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ This project should more or less adhere to [Semantic Versioning](https://semver.
* Use setuptools-scm / pyproject.toml (modern packaging). Details in https://github.com/python-caldav/caldav/pull/364 and https://github.com/python-caldav/caldav/pull/367
* Debugging tool - an environment variable can be set, causing the library to spew out server communications into files under /tmp. Details in https://github.com/python-caldav/caldav/pull/249 and https://github.com/python-caldav/caldav/issues/248
* Comaptibility matrix for posteo.de servers in `tests/compatibility_issues.py`
* Added sort_reverse option to the search function to reverse the sorting order of the found objects.

### Security

Expand Down
4 changes: 3 additions & 1 deletion caldav/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ def search(
todo: Optional[bool] = None,
include_completed: bool = False,
sort_keys: Sequence[str] = (),
sort_reverse: bool = False,
split_expanded: bool = True,
props: Optional[List[CalendarData]] = None,
**kwargs,
Expand Down Expand Up @@ -1163,6 +1164,7 @@ def search(
* start, end: do a time range search
* filters - other kind of filters (in lxml tree format)
* sort_keys - list of attributes to use when sorting
* sort_reverse - reverse the sorting order

not supported yet:
* negated text match
Expand Down Expand Up @@ -1290,7 +1292,7 @@ def sort_key_func(x):
return ret

if sort_keys:
objects.sort(key=sort_key_func)
objects.sort(key=sort_key_func, reverse=sort_reverse)

## partial workaround for https://github.com/python-caldav/caldav/issues/201
for obj in objects:
Expand Down
10 changes: 8 additions & 2 deletions tests/test_caldav.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@
from caldav.objects import Todo

if test_xandikos:
from xandikos.web import XandikosBackend, XandikosApp
import asyncio

import aiohttp
import aiohttp.web
import asyncio
from xandikos.web import XandikosApp, XandikosBackend

if test_radicale:
import radicale.config
Expand Down Expand Up @@ -1332,6 +1333,11 @@ def testSearchEvent(self):
assert len(all_events) == 3
assert all_events[0].instance.vevent.summary.value == "Bastille Day Jitsi Party"

## Sorting in reverse order should work also
all_events = c.search(sort_keys=("SUMMARY", "DTSTAMP"), sort_reverse=True)
assert len(all_events) == 3
assert all_events[0].instance.vevent.summary.value == "Our Blissful Anniversary"

def testSearchSortTodo(self):
self.skip_on_compatibility_flag("read_only")
self.skip_on_compatibility_flag("no_todo")
Expand Down