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

Convenience fix: allow strings in sort_keys #449

Merged
merged 1 commit into from
Nov 5, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ The format of this file should adhere to [Keep a Changelog](https://keepachangel

This project should more or less adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

* By now `calendar.search(..., sort_keys=("DTSTART")` will work. Sort keys expects a list or a tuple, but it's easy to send an attribute by mistake.

## [1.4.0] - 2024-11-05

* Lots of work lifting the project up to more modern standards and improving code, thanks to Georges Toth (github @sim0nx), Matthias Urlichs (github @smurfix) and @ArtemIsmagilov. While this shouldn't matter for existing users, it will make the library more future-proof.
Expand Down
3 changes: 3 additions & 0 deletions caldav/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,7 @@ def sort_key_func(x):
> datetime.now().strftime("%F%H%M%S")
),
}
## ref https://github.com/python-caldav/caldav/issues/448 - allow strings instead of a sequence here
for sort_key in sort_keys:
val = comp.get(sort_key, None)
if val is None:
Expand All @@ -1327,6 +1328,8 @@ def sort_key_func(x):
return ret

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

## partial workaround for https://github.com/python-caldav/caldav/issues/201
Expand Down
34 changes: 34 additions & 0 deletions tests/test_caldav.py
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,40 @@ def testSearchEvent(self):
assert len(all_events) == 3
assert all_events[0].instance.vevent.summary.value == "Our Blissful Anniversary"

## A more robust check for the sort key
all_events = c.search(sort_keys=("DTSTART",))
assert len(all_events) == 3
assert str(all_events[0].icalendar_component["DTSTART"].dt) < str(
all_events[1].icalendar_component["DTSTART"].dt
)
assert str(all_events[1].icalendar_component["DTSTART"].dt) < str(
all_events[2].icalendar_component["DTSTART"].dt
)
all_events = c.search(sort_keys=("DTSTART",), sort_reverse=True)
assert str(all_events[0].icalendar_component["DTSTART"].dt) > str(
all_events[1].icalendar_component["DTSTART"].dt
)
assert str(all_events[1].icalendar_component["DTSTART"].dt) > str(
all_events[2].icalendar_component["DTSTART"].dt
)

## Ref https://github.com/python-caldav/caldav/issues/448
all_events = c.search(sort_keys=("DTSTART"))
assert len(all_events) == 3
assert str(all_events[0].icalendar_component["DTSTART"].dt) < str(
all_events[1].icalendar_component["DTSTART"].dt
)
assert str(all_events[1].icalendar_component["DTSTART"].dt) < str(
all_events[2].icalendar_component["DTSTART"].dt
)
all_events = c.search(sort_keys=("DTSTART"), sort_reverse=True)
assert str(all_events[0].icalendar_component["DTSTART"].dt) > str(
all_events[1].icalendar_component["DTSTART"].dt
)
assert str(all_events[1].icalendar_component["DTSTART"].dt) > str(
all_events[2].icalendar_component["DTSTART"].dt
)

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