diff options
Diffstat (limited to 'jimbrella')
-rw-r--r-- | jimbrella/routine.py | 52 | ||||
-rw-r--r-- | jimbrella/test/test_routine.py | 7 |
2 files changed, 38 insertions, 21 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 + ) ) diff --git a/jimbrella/test/test_routine.py b/jimbrella/test/test_routine.py index 2a65b35..0b15717 100644 --- a/jimbrella/test/test_routine.py +++ b/jimbrella/test/test_routine.py @@ -8,6 +8,7 @@ from .jform_data import mock_jform_data from ..routine import sync_database, process_overdue from ..database import Database from ..jform import JForm +from ..admin_log import AdminLog from ..config import * """Set up logging.""" @@ -58,12 +59,14 @@ def api(endpoint): Thread(target=jform.run, kwargs={"port": 5001}).start() """Initialize Database and JForm.""" -TEST_DATABASE_PATH = "/tmp/jimbrella.test.db.csv" +TEST_DATABASE_PATH = "/tmp/jimbrella.db.csv" +TEST_ADMIN_LOG_PATH = "/tmp/jimbrella.admin.log" shutil.copyfile(DATABASE_PATH, TEST_DATABASE_PATH) db = Database(TEST_DATABASE_PATH) takeaway_jform = JForm("takeaway", "http://localhost:5001/takeaway", "/tmp") giveback_jform = JForm("giveback", "http://localhost:5001/giveback", "/tmp") -sync_database(takeaway_jform, giveback_jform, db) +admin = AdminLog(TEST_ADMIN_LOG_PATH) +sync_database(takeaway_jform, giveback_jform, db, admin) process_overdue(db) """Cleanup""" |