summaryrefslogtreecommitdiff
path: root/jimbrella/umbrellas.py
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-02-19 17:44:03 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-02-19 17:44:03 +0800
commit92ca54b191811df4b6e51f7ef06e453dcf1ac71e (patch)
tree68d1b5951c22c319e5689483facabde088f5c0e4 /jimbrella/umbrellas.py
parentf92064d93b8b6b2e8ee5d8c6579d54f4e331af71 (diff)
Unify tenant and admin loggers
Diffstat (limited to 'jimbrella/umbrellas.py')
-rw-r--r--jimbrella/umbrellas.py46
1 files changed, 28 insertions, 18 deletions
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,
+ )