-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Night owl service for completing daily tasks after midnight
- Loading branch information
Kristian Tashkov
committed
Sep 5, 2023
1 parent
e2b1c8e
commit 84bf974
Showing
8 changed files
with
354 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
Copyright (C) 2020-2023 Kristian Tashkov <[email protected]> | ||
This file is part of "Tools for Todoist". | ||
"Tools for Todoist" is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by the | ||
Free Software Foundation, either version 3 of the License, or (at your | ||
option) any later version. | ||
"Tools for Todoist" is distributed in the hope that it will be useful, but | ||
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
from datetime import datetime | ||
from typing import Any, Dict | ||
|
||
from dateutil.tz import gettz | ||
|
||
from tools_for_todoist.models.google_calendar import GoogleCalendar | ||
from tools_for_todoist.models.todoist import Todoist | ||
from tools_for_todoist.storage import get_storage | ||
|
||
NIGHT_OWL_DAY_SWITCH_HOUR = 'night_owl.day_switch_hour' | ||
|
||
|
||
class NightOwlEnabler: | ||
def __init__(self, todoist: Todoist, calendar: GoogleCalendar) -> None: | ||
self._todoist = todoist | ||
self._google_calendar = calendar | ||
self._day_switch_hour = int(get_storage().get_value(NIGHT_OWL_DAY_SWITCH_HOUR, 4)) | ||
|
||
def on_todoist_sync(self, sync_result: Dict[str, Any]) -> bool: | ||
should_sync = False | ||
for item_id in sync_result['completed']: | ||
item = self._todoist.get_item_by_id(item_id) | ||
if ( | ||
item is None | ||
or item.get_due_string() is None | ||
or 'every day' not in item.get_due_string() | ||
): | ||
continue | ||
now = datetime.now(gettz(self._google_calendar.default_timezone)) | ||
seconds_from_midnight = (now - now.replace(hour=0, minute=0, second=0)).total_seconds() | ||
if seconds_from_midnight / 3600 > self._day_switch_hour: | ||
continue | ||
|
||
next_due = item.next_due_date().replace(year=now.year, month=now.month, day=now.day) | ||
item.set_due(next_date=next_due, due_string=item.get_due_string()) | ||
should_sync |= item.save() | ||
return should_sync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
Copyright (C) 2020-2023 Kristian Tashkov <[email protected]> | ||
This file is part of "Tools for Todoist". | ||
"Tools for Todoist" is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by the | ||
Free Software Foundation, either version 3 of the License, or (at your | ||
option) any later version. | ||
"Tools for Todoist" is distributed in the hope that it will be useful, but | ||
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
from contextlib import ExitStack | ||
from typing import Any, Dict, Optional | ||
from unittest import TestCase | ||
from unittest.mock import MagicMock | ||
|
||
from tools_for_todoist.models.item import TodoistItem | ||
from tools_for_todoist.storage import set_storage | ||
from tools_for_todoist.storage.storage import KeyValueStorage | ||
|
||
|
||
class ServicesTestCase(TestCase): | ||
def setUp(self) -> None: | ||
self._exit_stack = ExitStack() | ||
|
||
self._todoist_items: Dict[str, TodoistItem] = {} | ||
self._todoist_mock = MagicMock() | ||
self._todoist_mock.get_item_by_id.side_effect = lambda item_id: self._todoist_items.get( | ||
item_id | ||
) | ||
|
||
self._google_calendar_mock = MagicMock() | ||
self._google_calendar_mock.default_timezone = 'Europe/Zurich' | ||
|
||
self._storage = KeyValueStorage() | ||
set_storage(self._storage) | ||
|
||
def tearDown(self) -> None: | ||
self._exit_stack.close() | ||
|
||
def _create_todoist_item(self, due: Optional[Dict[str, Any]] = None) -> TodoistItem: | ||
raw_item = { | ||
'id': 'TEST_ITEM_ID', | ||
'project_id': 'TEST_PROJECT_ID', | ||
'content': 'Test item', | ||
'priority': 1, | ||
'description': 'Test item description', | ||
'checked': False, | ||
'duration': None, | ||
'labels': [], | ||
'due': due, | ||
} | ||
item = TodoistItem.from_raw(self._todoist_mock, raw_item) | ||
self._todoist_items[item.id] = item | ||
return item |
Oops, something went wrong.