81 lines
2.2 KiB
Python
81 lines
2.2 KiB
Python
from typing import Any
|
|
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel, model_validator
|
|
from core.pipeline import EntityName, Id, validate
|
|
from core.schema import ENTITIES
|
|
from persistance import nodes, review
|
|
from webapi.auth import Caller, caller
|
|
|
|
router = APIRouter(prefix="/review", tags=["review"])
|
|
Rows = list[dict[str, Any]]
|
|
|
|
|
|
@router.get("/held")
|
|
def held(entity: EntityName) -> Rows:
|
|
return review.held(entity)
|
|
|
|
|
|
@router.get("/conflicts")
|
|
def conflicts(entity: EntityName) -> Rows:
|
|
return review.conflicts(entity)
|
|
|
|
|
|
@router.get("/unrecognised")
|
|
def unrecognised(pipeline: Id) -> Rows:
|
|
return review.unrecognised(pipeline)
|
|
|
|
|
|
@router.get("/persons")
|
|
def persons(pipeline: Id, node: Id) -> dict[str, Rows]:
|
|
return review.persons(pipeline, node)
|
|
|
|
|
|
class PersonDecision(BaseModel):
|
|
person: str
|
|
candidate: str
|
|
same: bool
|
|
|
|
|
|
@router.post("/persons", status_code=201)
|
|
def decide_persons(decision: PersonDecision, who: Caller = Depends(caller)) -> dict[str, str]:
|
|
nodes.decide_persons(decision.person, decision.candidate, decision.same, who.id)
|
|
return {"person": decision.person}
|
|
|
|
|
|
class Correction(BaseModel):
|
|
entity: EntityName = "identity"
|
|
record: str
|
|
field: str
|
|
value: str
|
|
|
|
@model_validator(mode="after")
|
|
def _a_valid_value(self):
|
|
if self.field not in ENTITIES[self.entity].model_fields:
|
|
raise ValueError(f"{self.entity} has no field {self.field}")
|
|
reasons = [
|
|
r
|
|
for r in validate(self.entity, {self.field: self.value})
|
|
if r.split(":")[0] == self.field
|
|
]
|
|
if reasons:
|
|
raise ValueError("; ".join(reasons))
|
|
return self
|
|
|
|
|
|
@router.post("/corrections", status_code=201)
|
|
def correct(fix: Correction, who: Caller = Depends(caller)) -> dict[str, str]:
|
|
nodes.correct(fix.record, fix.field, fix.value, who.id, fix.entity)
|
|
return {"record": fix.record}
|
|
|
|
|
|
class Rejection(BaseModel):
|
|
entity: EntityName
|
|
record: str
|
|
reason: str
|
|
|
|
|
|
@router.post("/rejections", status_code=201)
|
|
def reject(decision: Rejection, who: Caller = Depends(caller)) -> dict[str, str]:
|
|
nodes.reject(decision.entity, decision.record, decision.reason, who.id)
|
|
return {"record": decision.record}
|