Skip to content

Commit 69997c9

Browse files
committed
fix for #237 + some refactoring
1 parent 896bca7 commit 69997c9

2 files changed

Lines changed: 26 additions & 33 deletions

File tree

caldav/collection.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ def search(
778778
unless the next parameter is set ...
779779
* include_completed - include completed tasks
780780
* event - sets comp_class to event
781+
* journal - sets comp_class to journal
781782
* text attribute search parameters: category, uid, summary, comment,
782783
description, location, status
783784
* no-category, no-summary, etc ... search for objects that does not
@@ -974,6 +975,7 @@ def build_search_xml_query(
974975
ignore_completed2=None,
975976
ignore_completed3=None,
976977
event=None,
978+
journal=None,
977979
filters=None,
978980
expand=None,
979981
start=None,
@@ -1046,39 +1048,29 @@ def build_search_xml_query(
10461048
cdav.CompFilter("VALARM") + cdav.TimeRange(alarm_start, alarm_end)
10471049
)
10481050

1049-
if todo is not None:
1050-
if not todo:
1051-
raise NotImplementedError()
1052-
if todo:
1053-
if comp_class is not None and comp_class is not Todo:
1054-
raise error.ConsistencyError(
1055-
"inconsistent search parameters - comp_class = %s, todo=%s"
1056-
% (comp_class, todo)
1057-
)
1058-
comp_filter = cdav.CompFilter("VTODO")
1059-
comp_class = Todo
1060-
if event is not None:
1061-
if not event:
1062-
raise NotImplementedError()
1063-
if event:
1064-
if comp_class is not None and comp_class is not Event:
1065-
raise error.ConsistencyError(
1066-
"inconsistent search parameters - comp_class = %s, event=%s"
1067-
% (comp_class, event)
1068-
)
1069-
comp_filter = cdav.CompFilter("VEVENT")
1070-
comp_class = Event
1071-
elif comp_class:
1072-
if comp_class is Todo:
1073-
comp_filter = cdav.CompFilter("VTODO")
1074-
elif comp_class is Event:
1075-
comp_filter = cdav.CompFilter("VEVENT")
1076-
elif comp_class is Journal:
1077-
comp_filter = cdav.CompFilter("VJOURNAL")
1078-
else:
1079-
raise error.ConsistencyError(
1080-
"unsupported comp class %s for search" % comp_class
1081-
)
1051+
## Deal with event, todo, journal or comp_class
1052+
for flagged, comp_name, comp_class_ in (
1053+
(event, 'VEVENT', Event),
1054+
(todo, 'VTODO', Todo),
1055+
(journal, 'VJOURNAL', Journal)):
1056+
if flagged is not None:
1057+
if not flagged:
1058+
raise NotImplementedError(f"Negated search for {comp_name} not supported yet")
1059+
if flagged:
1060+
## event/journal/todo is set, we adjust comp_class accordingly
1061+
if comp_class is not None and comp_class is not comp_class:
1062+
raise error.ConsistencyError(
1063+
f"inconsistent search parameters - comp_class = {comp_class}, want {comp_class_}"
1064+
)
1065+
comp_class = comp_class_
1066+
1067+
if comp_class == comp_class_:
1068+
comp_filter = cdav.CompFilter(comp_name)
1069+
1070+
if comp_class and not comp_filter:
1071+
raise error.ConsistencyError(
1072+
f"unsupported comp class {comp_class} for search"
1073+
)
10821074

10831075
for other in kwargs:
10841076
find_not_defined = other.startswith("no_")

tests/test_caldav.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,7 @@ def testCreateJournalListAndJournalEntry(self):
19581958
uid="ctuid1",
19591959
)
19601960
assert len(c.journals()) == 2
1961+
assert len(c.search(journal=True)) == 2
19611962
todos = c.todos()
19621963
events = c.events()
19631964
assert todos + events == []

0 commit comments

Comments
 (0)