Skip to content

Commit

Permalink
style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tobixen committed Dec 8, 2023
1 parent 0e229db commit a0c33a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
18 changes: 9 additions & 9 deletions caldav/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,21 +1118,21 @@ def sort_key_func(x):
"dtstart": "1970-01-01",
"priority": 0,
"status": {
'VTODO': 'NEEDS-ACTION',
'VJOURNAL': 'FINAL',
'VEVENT': 'TENTATIVE'
}[comp.name],
"VTODO": "NEEDS-ACTION",
"VJOURNAL": "FINAL",
"VEVENT": "TENTATIVE",
}[comp.name],
"category": "",
## Usage of strftime is a simple way to ensure there won't be
## problems if comparing dates with timestamps
"isnt_overdue": not (
'due' in comp and
comp['due'].dt.strftime("%F%H%M%S")
"due" in comp
and comp["due"].dt.strftime("%F%H%M%S")
< datetime.now().strftime("%F%H%M%S")
),
"hasnt_started": (
"dtstart" in comp
and comp['dtstart'].dt.strftime("%F%H%M%S")
and comp["dtstart"].dt.strftime("%F%H%M%S")
> datetime.now().strftime("%F%H%M%S")
),
}
Expand All @@ -1141,9 +1141,9 @@ def sort_key_func(x):
if val is None:
ret.append(defaults.get(sort_key.lower(), ""))
continue
if hasattr(val, 'dt'):
if hasattr(val, "dt"):
val = val.dt
elif hasattr(val, 'cats'):
elif hasattr(val, "cats"):
val = ",".join(val.cats)
if hasattr(val, "strftime"):
ret.append(val.strftime("%F%H%M%S"))
Expand Down
55 changes: 38 additions & 17 deletions tests/test_caldav.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,35 +1331,56 @@ def testSearchEvent(self):
all_events = c.search(sort_keys=("SUMMARY", "DTSTAMP"))
assert len(all_events) == 3
assert all_events[0].instance.vevent.summary.value == "Bastille Day Jitsi Party"

def testSearchSortTodo(self):
self.skip_on_compatibility_flag("read_only")
self.skip_on_compatibility_flag("no_todo")
c = self._fixCalendar(supported_calendar_component_set=["VTODO"])
t1 = c.save_todo(summary='1 task overdue', due=date(2022,12,12), dtstart=date(2022,10,11), uid='1')
t2 = c.save_todo(summary='2 task future', due=datetime.now()+timedelta(hours=15), dtstart=datetime.now()+timedelta(minutes=15), uid='2')
t3 = c.save_todo(summary='3 task future due', due=datetime.now()+timedelta(hours=15), dtstart=datetime(2022,12,11,10,9,8), uid='3')
t4 = c.save_todo(summary='4 task priority low', priority=9, uid='4')
t5 = c.save_todo(summary='5 task status completed', status='COMPLETED', uid='5')
t6 = c.save_todo(summary='6 task has categories', categories='home,garden,sunshine', uid='6')
t1 = c.save_todo(
summary="1 task overdue",
due=date(2022, 12, 12),
dtstart=date(2022, 10, 11),
uid="1",
)
t2 = c.save_todo(
summary="2 task future",
due=datetime.now() + timedelta(hours=15),
dtstart=datetime.now() + timedelta(minutes=15),
uid="2",
)
t3 = c.save_todo(
summary="3 task future due",
due=datetime.now() + timedelta(hours=15),
dtstart=datetime(2022, 12, 11, 10, 9, 8),
uid="3",
)
t4 = c.save_todo(summary="4 task priority low", priority=9, uid="4")
t5 = c.save_todo(summary="5 task status completed", status="COMPLETED", uid="5")
t6 = c.save_todo(
summary="6 task has categories", categories="home,garden,sunshine", uid="6"
)

def check_order(tasks, order):
assert([str(x.icalendar_component['uid']) for x in tasks] == [str(x) for x in order])

all_tasks = c.search(todo=True, sort_keys=('uid',))
check_order(all_tasks, (1,2,3,4,6))

all_tasks = c.search(sort_keys=('summary',))
check_order(all_tasks, (1,2,3,4,5,6))

all_tasks = c.search(sort_keys=('isnt_overdue','categories', 'dtstart','priority', 'status'))
assert [str(x.icalendar_component["uid"]) for x in tasks] == [
str(x) for x in order
]

all_tasks = c.search(todo=True, sort_keys=("uid",))
check_order(all_tasks, (1, 2, 3, 4, 6))

all_tasks = c.search(sort_keys=("summary",))
check_order(all_tasks, (1, 2, 3, 4, 5, 6))

all_tasks = c.search(
sort_keys=("isnt_overdue", "categories", "dtstart", "priority", "status")
)
## This is difficult ...
## * 1 is the only one that is overdue, and False sorts before True, so 1 comes first
## * categories, empty string sorts before a non-empty string, so 6 is at the end of the list
## So we have 2-5 still to worry about ...
## * dtstart - default is "long ago", so 4,5 or 5,4 should be first, followed by 3,2
## * priority - default is 0, so 5 comes before 4
check_order(all_tasks, (1,5,4,3,2,6))
check_order(all_tasks, (1, 5, 4, 3, 2, 6))

def testSearchTodos(self):
self.skip_on_compatibility_flag("read_only")
Expand Down

0 comments on commit a0c33a6

Please sign in to comment.