summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-02-03 16:09:32 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-02-03 16:09:32 +0800
commit3d30bce15559afdf3acf6277648231611e145037 (patch)
treed0388a0c41141666eec87774357624387c92ebc4
parentbad6b0a8d24392f1a8c4b45e2dd06ec84719f456 (diff)
Adapt process_overdue
-rw-r--r--jimbrella/routine.py54
1 files changed, 30 insertions, 24 deletions
diff --git a/jimbrella/routine.py b/jimbrella/routine.py
index 9060962..2626a76 100644
--- a/jimbrella/routine.py
+++ b/jimbrella/routine.py
@@ -1,3 +1,5 @@
+from datetime import datetime, timedelta
+from dateutil.parser import isoparse
import logging
from .umbrellas import Umbrellas
from .jform import JForm
@@ -5,6 +7,7 @@ from .admin_log import AdminLog
from .config import *
from .exceptions import *
from .config import DUE_HOURS
+from .utils import CST
"""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
@@ -100,31 +103,34 @@ def sync_jform(takeaway: JForm, giveback: JForm, db: Umbrellas, admin: AdminLog)
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)
- ]
+ now = datetime.now().astimezone(CST)
+ for umb in umbrellas:
+ if umb["status"] == "lent":
+ try:
+ lent_at = isoparse(umb["lent_at"])
+ except ValueError:
+ logging.error(
+ "Invalid lent_at in umbrella #%d: %s", umb["id"], umb["lent_at"]
+ )
+ continue
- for umb in overdue:
- db.mark_overdue(umb["id"])
- logging.warning(
- "Umbrella #{id} is now overdue, tenant: {tenant_name} (ID: {tenant_id}, phone: {tenant_phone})".format(
- **umb
- )
- )
- admin.log(
- "OVERDUE",
- {
- "key": umb["id"],
- "name": umb["tenant_name"],
- "phone": umb["tenant_phone"],
- "id": umb["tenant_id"],
- "email": "",
- },
- )
+ if now - lent_at > timedelta(hours=DUE_HOURS):
+ db.mark_overdue(umb["id"])
+ logging.warning(
+ "Umbrella #{id} is now overdue, tenant: {tenant_name} (ID: {tenant_id}, phone: {tenant_phone})".format(
+ **umb
+ )
+ )
+ admin.log(
+ "OVERDUE",
+ {
+ "key": umb["id"],
+ "name": umb["tenant_name"],
+ "phone": umb["tenant_phone"],
+ "id": umb["tenant_id"],
+ "email": "",
+ },
+ )
if __name__ == "__main__":