aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2021-02-14 17:20:14 +0200
committerJan Tuomi <jan.tuomi@valuemotive.com>2021-02-14 17:20:14 +0200
commit973c8dfdfd90b82e0144af8d979e49e3018ddd0d (patch)
treeb942bb70c597267092cf8456adde56238d2f00b4
parent54978eb6b515afd1da68642de07e482b09acf9f3 (diff)
Improve week processing
-rw-r--r--hommaexceli_py/data_parser.py18
-rw-r--r--hommaexceli_py/sheets_client.py3
-rw-r--r--hommaexceli_py/telegram_client.py10
3 files changed, 21 insertions, 10 deletions
diff --git a/hommaexceli_py/data_parser.py b/hommaexceli_py/data_parser.py
index e8ca33b..92297b4 100644
--- a/hommaexceli_py/data_parser.py
+++ b/hommaexceli_py/data_parser.py
@@ -35,9 +35,15 @@ def _date_to_timestamp(date: datetime.datetime) -> str:
def _process_row(row: SheetsRow):
interval = _calculate_interval(row.interval)
- last_done_date = _timestamp_to_date(row.last_done)
- next_date = last_done_date + interval
- next_date_str = _date_to_timestamp(next_date)
+
+ if row.last_done is not None:
+ last_done_date = _timestamp_to_date(row.last_done)
+ next_date = last_done_date + interval
+ next_date_str = _date_to_timestamp(next_date)
+ else:
+ last_done_date = datetime.datetime.now()
+ next_date = datetime.datetime.now()
+ next_date_str = _date_to_timestamp(next_date)
return ProcessedRow(
name=row.name,
@@ -55,8 +61,10 @@ def process_rows(rows: list[SheetsRow]) -> list[ProcessedRow]:
def _is_this_week(processed_row: ProcessedRow):
next_date = processed_row.next_date
- now = datetime.datetime.now()
- return (next_date - now) < datetime.timedelta(weeks=1)
+ today = datetime.datetime.now()
+ current_week_start = today - datetime.timedelta(days=today.weekday())
+ current_week_end = current_week_start + datetime.timedelta(days=7)
+ return next_date > current_week_start and next_date < current_week_end
def filter_only_this_week(rows: list[ProcessedRow]) -> list[ProcessedRow]:
diff --git a/hommaexceli_py/sheets_client.py b/hommaexceli_py/sheets_client.py
index 9a09f07..95721b3 100644
--- a/hommaexceli_py/sheets_client.py
+++ b/hommaexceli_py/sheets_client.py
@@ -28,8 +28,7 @@ def _row_to_dataclass(values):
interval = _get_or_default(values, 0, "")
name = _get_or_default(values, 1, "")
- today = date.today()
- last_done = _get_or_default(values, 2, today.strftime("%Y-%m-%d"))
+ last_done = _get_or_default(values, 2, None)
return SheetsRow(interval=interval, name=name, last_done=last_done)
diff --git a/hommaexceli_py/telegram_client.py b/hommaexceli_py/telegram_client.py
index ad1f478..d11d876 100644
--- a/hommaexceli_py/telegram_client.py
+++ b/hommaexceli_py/telegram_client.py
@@ -40,9 +40,13 @@ def _callback_alarm(context: CallbackContext, sheets_service: Resource):
this_weeks_rows = filter_only_this_week(processed_rows)
logging.info(f"Sending sheet data to chat id {chat_id}...")
- message = "Tällä viikolla tehtävät hommat:\n" + "\n".join(
- map(lambda task: f"{task.name} ({task.interval})", this_weeks_rows)
- )
+ if len(this_weeks_rows) > 0:
+ message = "Tällä viikolla tehtävät hommat:\n" + "\n".join(
+ map(lambda task: f"{task.name} ({task.interval})", this_weeks_rows)
+ )
+ else:
+ message = "Tällä viikolla ei toistuvia hommia! 🎉 "
+
context.bot.send_message(chat_id=chat_id, text=message)
update_sheet_last_done_column(sheets_service, processed_rows)