diff options
Diffstat (limited to 'jimbrella/routine.py')
-rw-r--r-- | jimbrella/routine.py | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/jimbrella/routine.py b/jimbrella/routine.py index 469c78d..c1f7870 100644 --- a/jimbrella/routine.py +++ b/jimbrella/routine.py @@ -38,7 +38,7 @@ def chronological_merge(*sheet_lists) -> list: return chronicle -def sync_database(takeaway: JForm, giveback: JForm, db: Database): +def sync_database(takeaway: JForm, giveback: JForm, db: Database, admin: AdminLog): takeaway_unread = takeaway.get_unread() giveback_unread = giveback.get_unread() logging.info( @@ -51,13 +51,8 @@ def sync_database(takeaway: JForm, giveback: JForm, db: Database): # because chronological_merge popped all their elements for sheet in unread: sheet["date"] = sheet["date"].replace(tzinfo=None) # it's UTC+8 anyway - log_args = ( - sheet["name"], - sheet["id"], - sheet["phone"], - sheet["key"], - sheet["date"].isoformat(timespec="seconds"), - ) + sheet["date_str"] = sheet["date"].isoformat(timespec="seconds") + tenant_identity = "{name} (ID: {id}, phone: {phone})".format(**sheet) if sheet["jform_name"] == "takeaway": try: db.take_away( @@ -68,33 +63,52 @@ def sync_database(takeaway: JForm, giveback: JForm, db: Database): sheet["phone"], ) logging.info( - "%s (ID: %s, phone: %s) borrowed umbrella #%d at %s", *log_args + tenant_identity + + " borrowed umbrella #{key} at {date_str}".format(**sheet) ) + admin.log("TAKEAWAY", sheet, date=sheet["date"]) except UmbrellaStatusError: logging.warning( - "%s (ID: %s, phone: %s) attempted to borrow inavailable umbrella #%d at %s", - *log_args + tenant_identity + + " attempted to borrow inavailable umbrella #{key} at {date_str}".format( + **sheet + ) ) except UmbrellaNotFoundError: logging.warning( - "%s (ID: %s, phone: %s) attempted to borrow non-existent umbrella #%d at %s", - *log_args + tenant_identity + + " attempted to borrow non-existent umbrella #{key} at {date_str}".format( + **sheet + ) ) elif sheet["jform_name"] == "giveback": try: - db.give_back(sheet["key"]) + db.give_back(sheet["key"], sheet["name"], sheet["id"]) logging.info( - "%s (ID: %s, phone: %s) returned umbrella #%d at %s", *log_args + tenant_identity + + " returned umbrella #{key} at {date_str}".format(**sheet) ) + admin.log("GIVEBACK", sheet, date=sheet["date"]) except UmbrellaStatusError: logging.warning( - "%s (ID: %s, phone: %s) attempted to return available umbrella #%d at %s", - *log_args + tenant_identity + + " attempted to return available umbrella #{key} at {date_str}".format( + **sheet + ) ) except UmbrellaNotFoundError: logging.warning( - "%s (ID: %s, phone: %s) attempted to return non-existent umbrella #%d at %s", - *log_args + 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( + expected=e.expected, **sheet + ) ) |