diff options
author | Frederick Yin <fkfd@fkfd.me> | 2021-10-28 23:00:46 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2021-10-28 23:02:53 +0800 |
commit | 41e1645f4a90d44db88efe8ad843bd6d7ac64f6f (patch) | |
tree | 1977cb3feb282412be2d1a1d8416f0d526c44d94 | |
parent | b6e66481f85a2ef8a00732a2b0e6435522edfe56 (diff) |
AdminLog: `read()` returns friendly logs
-rw-r--r-- | jimbrella/admin_log.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/jimbrella/admin_log.py b/jimbrella/admin_log.py index d1c57c2..2d130c7 100644 --- a/jimbrella/admin_log.py +++ b/jimbrella/admin_log.py @@ -2,6 +2,7 @@ import csv import os from datetime import datetime from .lockfile import Lockfile +from .utils import human_datetime class AdminLog: @@ -80,6 +81,40 @@ class AdminLog: f.close() return logs + def read(self) -> list: + """Human-friendly representation of each of the admin log entries.""" + logs = self._read() + friendly_logs = [] + for entry in logs: + event = entry["event"] + tenant_info = "(ID: {tenant_id}, phone: {tenant_phone})" + if event == "TAKEAWAY": + description = ( + "{tenant_name} borrowed umbrella #{serial}. " + tenant_info + ) + elif event == "GIVEBACK": + description = ( + "{tenant_name} returned umbrella #{serial}. " + tenant_info + ) + elif event == "OVERDUE": + description = ( + "{tenant_name} missed the due for umbrella #{serial}. " + + tenant_info + ) + elif event == "ADMIN_MODIFY_DB": + description = "{admin_name} changed {column} of umbrella #{serial} from {past_value} to {new_value}." + + friendly_logs.append( + { + "date_str": human_datetime(entry["date"]), + "event": event, + "description": description.format(**entry), + "note": entry["note"], + } + ) + + return friendly_logs + def log(self, event: str, entry: dict, date=None, note="") -> None: """Serialize a log, and append it to the end of the log file. |