diff options
author | Frederick Yin <fkfd@fkfd.me> | 2021-10-29 14:53:18 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2021-10-29 14:53:18 +0800 |
commit | f56dfc7e850ad4b4c33f35b24a712d7f92a23454 (patch) | |
tree | 5fc68e53d5e58a3761a222b9389553c4c1d47aa5 /jimbrella/routine.py | |
parent | 0f11b95cf154fb93d3e4c943027e594cde845da1 (diff) |
Admin logging in routine
Also, path to test database is changed, and so are the arguments passed
to db.give_back.
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 + ) ) |