summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-02-01 18:45:33 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-02-01 18:45:33 +0800
commit02547e6078a7cd6c1371379f1bab72bf48add23d (patch)
tree1f80693055101ccdb1d4be8a054ab67387652e15
parent3c781cba418f09d9d7249c5e7bef82ffcc1b5cfa (diff)
Refine routines
-rw-r--r--jimbrella/routine.py67
1 files changed, 29 insertions, 38 deletions
diff --git a/jimbrella/routine.py b/jimbrella/routine.py
index 33c48dc..9060962 100644
--- a/jimbrella/routine.py
+++ b/jimbrella/routine.py
@@ -1,10 +1,10 @@
import logging
-from .database import Database
+from .umbrellas import Umbrellas
from .jform import JForm
from .admin_log import AdminLog
from .config import *
-from .utils import local_now
from .exceptions import *
+from .config import DUE_HOURS
"""A set of routine methods, run at an interval (somewhere from ten minutes to one hour), to:
- sync JImbrella's databse against data pulled from jForm
@@ -38,11 +38,11 @@ def chronological_merge(*sheet_lists) -> list:
return chronicle
-def sync_database(takeaway: JForm, giveback: JForm, db: Database, admin: AdminLog):
+def sync_jform(takeaway: JForm, giveback: JForm, db: Umbrellas, admin: AdminLog):
takeaway_unread = takeaway.get_unread()
giveback_unread = giveback.get_unread()
logging.info(
- "Sync database: found %d unread takeaway sheet(s) and %d unread giveback sheet(s)",
+ "Sync jForm: found %d unread takeaway sheet(s) and %d unread giveback sheet(s)",
len(takeaway_unread),
len(giveback_unread),
)
@@ -50,8 +50,7 @@ def sync_database(takeaway: JForm, giveback: JForm, db: Database, admin: AdminLo
# NOTE: beyond this point `takeaway_unread` and `giveback_unread` are empty
# because chronological_merge popped all their elements
for sheet in unread:
- sheet["date"] = sheet["date"].replace(tzinfo=None) # it's UTC+8 anyway
- sheet["date_str"] = sheet["date"].isoformat(timespec="seconds")
+ sheet["date_str"] = sheet["date"].isoformat(timespec="milliseconds")
tenant_identity = "{name} (ID: {id}, phone: {phone})".format(**sheet)
if sheet["jform_name"] == "takeaway":
try:
@@ -67,17 +66,10 @@ def sync_database(takeaway: JForm, giveback: JForm, db: Database, admin: AdminLo
+ " borrowed umbrella #{key} at {date_str}".format(**sheet)
)
admin.log("TAKEAWAY", sheet, date=sheet["date"])
- except UmbrellaStatusError:
+ except (UmbrellaStatusError, UmbrellaNotFoundError):
logging.warning(
tenant_identity
- + " attempted to borrow inavailable umbrella #{key} at {date_str}".format(
- **sheet
- )
- )
- except UmbrellaNotFoundError:
- logging.warning(
- tenant_identity
- + " attempted to borrow non-existent umbrella #{key} at {date_str}".format(
+ + " attempted to borrow umbrella #{key} at {date_str}".format(
**sheet
)
)
@@ -89,45 +81,44 @@ def sync_database(takeaway: JForm, giveback: JForm, db: Database, admin: AdminLo
+ " returned umbrella #{key} at {date_str}".format(**sheet)
)
admin.log("GIVEBACK", sheet, date=sheet["date"])
- except UmbrellaStatusError:
+ except (UmbrellaStatusError, UmbrellaNotFoundError):
logging.warning(
tenant_identity
- + " attempted to return available umbrella #{key} at {date_str}".format(
+ + " attempted to return umbrella #{key} at {date_str}".format(
**sheet
)
)
- except UmbrellaNotFoundError:
- logging.warning(
- tenant_identity
- + " attempted to return non-existent umbrella #{key} at {date_str}".format(
- **sheet
- ),
- )
except TenantIdentityError as e:
logging.warning(
tenant_identity
- + " attempted to return umbrella #{key} whose actual tenant is {expected}".format(
+ + " attempted to return umbrella #{key} at {date_str}, expecting tenant {expected}".format(
expected=e.expected, **sheet
)
)
-def process_overdue(db: Database, admin: AdminLog):
- overdue = Database.find_overdue(db.read())
- # mark and log umbrellas that were not, but just became overdue
- for umb in filter(lambda u: u["status"] == "lent", overdue):
- db.mark_overdue(umb["serial"])
+def process_overdue(db: Umbrellas, admin: AdminLog):
+ """mark and log umbrellas that were not, but just became overdue"""
+ umbrellas = db.read()
+ overdue = [
+ umb
+ for umb in umbrellas
+ if umb["status"] == "lent"
+ and umb["lent_at"] is not None
+ and now - umb["lent_at"] > timedelta(hours=DUE_HOURS)
+ ]
+
+ for umb in overdue:
+ db.mark_overdue(umb["id"])
logging.warning(
- "Umbrella #%d is now overdue, tenant: %s (ID: %s, phone: %s)",
- umb["serial"],
- umb["tenant_name"],
- umb["tenant_id"],
- umb["tenant_phone"],
+ "Umbrella #{id} is now overdue, tenant: {tenant_name} (ID: {tenant_id}, phone: {tenant_phone})".format(
+ **umb
+ )
)
admin.log(
"OVERDUE",
{
- "key": umb["serial"],
+ "key": umb["id"],
"name": umb["tenant_name"],
"phone": umb["tenant_phone"],
"id": umb["tenant_id"],
@@ -139,7 +130,7 @@ def process_overdue(db: Database, admin: AdminLog):
if __name__ == "__main__":
takeaway = JForm("takeaway", JFORM_TAKEAWAY_URL, JFORM_BOOKMARK_DIR)
giveback = JForm("giveback", JFORM_GIVEBACK_URL, JFORM_BOOKMARK_DIR)
- db = Database(DATABASE_PATH)
+ db = Umbrellas(DATABASE_PATH)
admin_log = AdminLog(ADMIN_LOG_PATH)
- sync_database(takeaway, giveback, db, admin_log)
+ sync_jform(takeaway, giveback, db, admin_log)
process_overdue(db, admin_log)