From 41e1645f4a90d44db88efe8ad843bd6d7ac64f6f Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Thu, 28 Oct 2021 23:00:46 +0800 Subject: AdminLog: `read()` returns friendly logs --- jimbrella/admin_log.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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. -- cgit v1.2.3