From 92ca54b191811df4b6e51f7ef06e453dcf1ac71e Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Sat, 19 Feb 2022 17:44:03 +0800 Subject: Unify tenant and admin loggers --- jimbrella/umbrellas.py | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'jimbrella/umbrellas.py') diff --git a/jimbrella/umbrellas.py b/jimbrella/umbrellas.py index 9b5c7b8..241442c 100644 --- a/jimbrella/umbrellas.py +++ b/jimbrella/umbrellas.py @@ -2,7 +2,7 @@ import sqlite3 from datetime import datetime, timezone, timedelta from dateutil.parser import isoparse from typing import Union -from .admin_log import AdminLog +from .logger import Logger from .utils import human_datetime, human_timedelta, CST from .exceptions import * @@ -14,8 +14,7 @@ class Umbrellas: """A database of all umbrellas and their current state. Currently, the data are represented in a SQLite database. Only admins have access to the - database, and have the power to modify it arbitrarily. Each time a modification is made, - AdminLog keeps a log. + database, and have the power to modify it arbitrarily. An SQL row for an umbrella consists of these columns: - id | int. unique identifier for the umbrella. @@ -44,7 +43,7 @@ class Umbrellas: ); """ self.path = path - self.admin_log = AdminLog(path) + self.logger = Logger(path) def read(self, umbid=None) -> Union[dict, list]: """Read umbrella data from database. @@ -177,7 +176,7 @@ class Umbrellas: self.update(umb) umb_new = dict(self.read(umbid)) if umb_old != umb_new: - self.admin_log.log( + self.logger.log_admin( datetime.now(tz=CST).isoformat(timespec="milliseconds"), actor, umbid, @@ -197,25 +196,27 @@ class Umbrellas: if umb["status"] != "available": raise UmbrellaStatusError - self.update( - { - "id": umbid, - "status": "lent", - "tenant_name": tenant_name, - "tenant_id": tenant_id, - "tenant_phone": tenant_phone, - "tenant_email": tenant_email, - "lent_at": date.isoformat(timespec="milliseconds"), - } + umb = { + "id": umbid, + "status": "lent", + "tenant_name": tenant_name, + "tenant_id": tenant_id, + "tenant_phone": tenant_phone, + "tenant_email": tenant_email, + "lent_at": date.isoformat(timespec="milliseconds"), + } + self.update(umb) + self.logger.log_tenant( + date.isoformat(timespec="milliseconds"), "borrow", umbid, umb ) - def give_back(self, umbid, tenant_name, tenant_id) -> None: + def give_back(self, umbid, date, tenant_name, tenant_id) -> None: """When a user has returned an umbrella. `tenant_name` and `tenant_id` are used to verify if the umbrella is returned by the same person who borrowed it. """ - umb = self.read(umbid) + umb = dict(self.read(umbid)) if umb is None: raise UmbrellaNotFoundError(umbid) @@ -230,10 +231,13 @@ class Umbrellas: "status": "available", } ) + self.logger.log_tenant( + date.isoformat(timespec="milliseconds"), "return", umbid, umb + ) def mark_overdue(self, umbid) -> None: """When an umbrella is overdue, change its status to "overdue".""" - umb = self.read(umbid) + umb = dict(self.read(umbid)) if umb is None: raise UmbrellaNotFoundError(umbid) @@ -241,3 +245,9 @@ class Umbrellas: raise UmbrellaStatusError self.update({"id": umbid, "status": "overdue"}) + self.logger.log_tenant( + datetime.now(tz=CST).isoformat(timespec="milliseconds"), + "overdue", + umbid, + umb, + ) -- cgit v1.2.3