diff options
author | Frederick Yin <fkfd@fkfd.me> | 2021-10-22 23:26:49 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2021-10-22 23:28:09 +0800 |
commit | 64979b1dd30ba1eed531b071aaf74ce1aaa1d511 (patch) | |
tree | 6f17f4613f30502bb4aa9a51a62ae5f3edd84195 /jimbrella | |
parent | a2059e127e4a41bc9d14e85527b834195da436c6 (diff) |
Mark overdue umbrellas in Database
Diffstat (limited to 'jimbrella')
-rw-r--r-- | jimbrella/database.py | 16 | ||||
-rw-r--r-- | jimbrella/routine.py | 13 |
2 files changed, 25 insertions, 4 deletions
diff --git a/jimbrella/database.py b/jimbrella/database.py index ffe5d2f..2b31356 100644 --- a/jimbrella/database.py +++ b/jimbrella/database.py @@ -1,7 +1,7 @@ import csv import os from datetime import datetime, timedelta -from .utils import human_datetime, human_timedelta +from .utils import human_datetime, human_timedelta, local_now from .config import DUE_HOURS from .exceptions import * @@ -169,7 +169,7 @@ class Database: - lent_time_ago_str: string representation for lent_time_ago. """ umbrellas = self._read() - now = datetime.now() + now = local_now() for idx, umb in enumerate(umbrellas): if umb["status"] in ("lent", "overdue"): umbrellas[idx]["lent_at_str"] = human_datetime(umb["lent_at"]) @@ -188,7 +188,7 @@ class Database: def find_overdue(umbrellas) -> list: """(static method) Returns umbrellas in possession of their tenant for too long.""" - now = datetime.now() + now = local_now() return [ umb for umb in umbrellas @@ -225,3 +225,13 @@ class Database: umb[key] = "" umb["lent_at"] = None self._update(umb) + + def mark_overdue(self, serial, date) -> None: + """When an umbrella is overdue, change its status to "overdue".""" + umb = self._find_by_serial(serial) + if umb is None: + raise UmbrellaNotFoundError + elif umb["status"] != "lent": + raise UmbrellaStatusError + umb["status"] = "overdue" + self._update(umb) diff --git a/jimbrella/routine.py b/jimbrella/routine.py index 784b60c..692807e 100644 --- a/jimbrella/routine.py +++ b/jimbrella/routine.py @@ -1,6 +1,7 @@ from .database import Database from .jform import JForm from .config import * +from .utils import local_now from .exceptions import * """A set of routine methods, run at an interval (somewhere from ten minutes to one hour), to: @@ -43,7 +44,11 @@ def sync_database(takeaway: JForm, giveback: JForm, db: Database): if sheet["jform_name"] == "takeaway": try: db.take_away( - sheet["key"], sheet["date"], sheet["name"], sheet["id"], sheet["phone"] + sheet["key"], + sheet["date"], + sheet["name"], + sheet["id"], + sheet["phone"], ) except UmbrellaStatusError: pass @@ -58,6 +63,12 @@ def sync_database(takeaway: JForm, giveback: JForm, db: Database): pass +def process_overdue(db: Database): + overdue = Database.find_overdue(db.read()) + for umb in overdue: + db.mark_overdue(umb["serial"], local_now()) + + if __name__ == "__main__": takeaway = JForm("takeaway", JFORM_TAKEAWAY_URL, JFORM_BOOKMARK_DIR) # giveback = JForm( |