diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-02-01 17:37:42 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-02-01 17:37:42 +0800 |
commit | 8953c6b8189010c37dad8380acb06ba36e08537f (patch) | |
tree | 71e6b9d6502c9714019c8edbb3c87b412b9119ad | |
parent | 0e47cf9a8d06e896c5197cb28cb5a2a518d255d1 (diff) |
Allow datetime.datetime type for Umbrellas.update
-rw-r--r-- | jimbrella/umbrellas.py | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/jimbrella/umbrellas.py b/jimbrella/umbrellas.py index 05b103c..e59bb54 100644 --- a/jimbrella/umbrellas.py +++ b/jimbrella/umbrellas.py @@ -59,6 +59,8 @@ class Umbrellas: Invalid values are rejected as an UmbrellaValueError. If `status` is not "lent" or "overdue", `tenant_*` and `lent_at` are automatically erased. + + `lent_at` may be either an ISO 8601 string or a datetime.datetime object. Must be UTC+8. """ # `id` must be specified. try: @@ -83,24 +85,11 @@ class Umbrellas: raise UmbrellaValueError("status") if status in ("lent", "overdue"): - try: - # timezone must be +08:00 - if ( - datetime.fromisoformat(umb["lent_at"]) - .tzinfo.utcoffset(None) - .seconds - != 28800 - ): - raise UmbrellaValueError("lent_at") - except ValueError: - raise UmbrellaValueError("lent_at") - for key in ( "tenant_name", "tenant_id", "tenant_phone", "tenant_email", - "lent_at", ): if col in umb: db.execute( @@ -109,6 +98,27 @@ class Umbrellas: umb[col] or None, umbid, ) + + if "lent_at" in umb: + try: + # lent_at could be a string, in which case it is parsed + lent_at = datetime.fromisoformat(umb["lent_at"]) + except TypeError: + # or it could be a datetime.datetime + lent_at = umb["lent_at"] + except ValueError: + # anything else is invalid + raise UmbrellaValueError("lent_at") + + if lent_at.tzinfo.utcoffset(None).seconds != 28800: + # timezone must be +08:00 + raise UmbrellaValueError("lent_at") + + db.execute( + "UPDATE Umbrellas SET lent_at = ? WHERE id = ?", + lent_at.isoformat(timespec="milliseconds"), + umbid, + ) else: # discard unneeded fields for col in ( |