Demo: simplify resolve_user to a plain previous-state lookup
This commit is contained in:
+5
-17
@@ -7,7 +7,7 @@ from pathlib import Path
|
||||
import msgspec
|
||||
|
||||
from kanta import Kanta
|
||||
from kanta.callbacks import DictPost, DictPre
|
||||
from kanta.callbacks import DictPre
|
||||
from kanta.logging import configure_logging
|
||||
|
||||
|
||||
@@ -47,18 +47,11 @@ kanta_v1 = Kanta(filename, Data(), migrations=sys.modules[__name__])
|
||||
|
||||
|
||||
@kanta_v1.logfmt
|
||||
def resolve_user(
|
||||
value: str, path: str, previous: DictPre, current: DictPost
|
||||
) -> str | None:
|
||||
def resolve_user(value: str, path: str, previous: DictPre) -> str | None:
|
||||
"""Resolve user ids to names from the database state itself."""
|
||||
if path != "$user" and not path.startswith("users."):
|
||||
return None
|
||||
# Post-change state first, then pre-change (deleted users are only there).
|
||||
for state in (current, previous):
|
||||
name = state.get("users", {}).get(value, {}).get("name")
|
||||
if name:
|
||||
return name
|
||||
return None
|
||||
return previous.get("users", {}).get(value, {}).get("name")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
@@ -92,18 +85,13 @@ async def main() -> None:
|
||||
)
|
||||
async with kanta_v1 as kanta:
|
||||
with kanta.transaction(
|
||||
action="import", user="userid001", logdiff=False
|
||||
action="update", user="userid002", extra=filename.name
|
||||
) as data:
|
||||
data.total = 3
|
||||
data.total += 1
|
||||
|
||||
with kanta.transaction(action="delete", user="userid002") as data:
|
||||
del data.users["userid001"]
|
||||
|
||||
with kanta.transaction(
|
||||
action="update", user="userid002", extra=filename.name
|
||||
) as data:
|
||||
data.total = 4
|
||||
|
||||
|
||||
# Fake clock for deterministic timestamps
|
||||
_now = datetime(2027, 1, 1, tzinfo=UTC)
|
||||
|
||||
Reference in New Issue
Block a user