ajuste de cruds incompleto
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -23,7 +23,6 @@ class Pipeline(Generic[InputT, OutputT]):
|
||||
self._input_data: Any = None
|
||||
self._schema = None
|
||||
self._permission: Optional[str] = None
|
||||
self._affidavit: Optional[Any] = None
|
||||
|
||||
def set_input_data(self, data: Any) -> 'Pipeline':
|
||||
"""Define los datos de entrada"""
|
||||
@@ -40,11 +39,6 @@ class Pipeline(Generic[InputT, OutputT]):
|
||||
self._permission = permission
|
||||
return self
|
||||
|
||||
def with_affidavit(self, affidavit: Any) -> 'Pipeline':
|
||||
"""Define el certificador legal"""
|
||||
self._affidavit = affidavit
|
||||
return self
|
||||
|
||||
def add_step(self, name: str, func: Callable) -> 'Pipeline':
|
||||
"""Agrega un paso al pipeline"""
|
||||
self._steps.append({"name": name, "func": func})
|
||||
@@ -67,7 +61,6 @@ class Pipeline(Generic[InputT, OutputT]):
|
||||
context["input_data"] = self._input_data
|
||||
context["schema"] = self._schema
|
||||
context["permission"] = self._permission
|
||||
context["affidavit"] = self._affidavit
|
||||
|
||||
result = context
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ from app.core.auth import CurrentUser
|
||||
|
||||
|
||||
class CreatePipe(Pipeline):
|
||||
"""Pipe prefab to create enititys"""
|
||||
"""Pipe prefab to create entities"""
|
||||
|
||||
def __init__(self, db: Session, user: CurrentUser, model: Type, repository):
|
||||
super().__init__(db, user, f"CREATE_{model.__tablename__.upper()}")
|
||||
@@ -60,9 +60,7 @@ class CreatePipe(Pipeline):
|
||||
if ctx.get("entity"):
|
||||
ctx["output"] = {c.name: getattr(ctx["entity"], c.name)
|
||||
for c in self.model.__table__.columns}
|
||||
print(f"FORMAT: output creado con {len(ctx['output'])} campos")
|
||||
else:
|
||||
print("FORMAT: No hay entity para formatear")
|
||||
ctx["output"] = None
|
||||
return ctx
|
||||
|
||||
@@ -79,11 +77,11 @@ class ReadPipe(Pipeline):
|
||||
def _build(self):
|
||||
self.add_step("validate_id", self._validate_id)
|
||||
self.add_step("check_permissions", self._check_permissions)
|
||||
self.add_step("execurte", self._execute)
|
||||
self.add_step("execute", self._execute)
|
||||
self.add_step("format_output", self._format)
|
||||
|
||||
async def _validate_id(self, ctx):
|
||||
input_data = ctx["input"].raw_data
|
||||
input_data = ctx.get("input_data", {})
|
||||
entity_id = input_data.get("id")
|
||||
if not entity_id:
|
||||
raise ValueError("se requiere ID")
|
||||
@@ -125,14 +123,18 @@ class UpdatePipe(Pipeline):
|
||||
self.add_step("format_output", self._format)
|
||||
|
||||
async def _validate(self, ctx):
|
||||
data = ctx["input"].get_validated()
|
||||
ctx["validated_data"] = data.dict() if data else {}
|
||||
input_data = ctx.get("input_data", {})
|
||||
if ctx.get("schema"):
|
||||
validated = ctx["schema"](**input_data)
|
||||
ctx["validated_data"] = validated.dict()
|
||||
else:
|
||||
ctx["validated_data"] = input_data
|
||||
return ctx
|
||||
|
||||
async def _validate_id(self, ctx):
|
||||
entity_id = ctx["validated_data"].get("id")
|
||||
if not entity_id:
|
||||
raise ValueError("Se rqeuiere ID")
|
||||
raise ValueError("Se requiere ID")
|
||||
ctx["entity_id"] = entity_id
|
||||
return ctx
|
||||
|
||||
@@ -151,10 +153,8 @@ class UpdatePipe(Pipeline):
|
||||
|
||||
|
||||
async def _format(self, ctx):
|
||||
ctx["output"] = {
|
||||
"id": ctx["entity"].id,
|
||||
"message": f"{self.model.__name__} actualizado exitosamente"
|
||||
}
|
||||
ctx["output"] = {c.name: getattr(ctx["entity"], c.name)
|
||||
for c in self.model.__table__.columns}
|
||||
return ctx
|
||||
|
||||
class SoftDelete(Pipeline):
|
||||
@@ -168,7 +168,6 @@ class SoftDelete(Pipeline):
|
||||
self._build()
|
||||
|
||||
def _build(self):
|
||||
self.add_step("validate", self._validate)
|
||||
self.add_step("validate_id", self._validate_id)
|
||||
self.add_step("check_permissions", self._check_permissions)
|
||||
self.add_step("check_already_deleted", self._check_already_deleted)
|
||||
@@ -177,7 +176,7 @@ class SoftDelete(Pipeline):
|
||||
self.add_step("format_output", self._format)
|
||||
|
||||
async def _validate_id(self, ctx):
|
||||
input_data = ctx["input"].raw_data
|
||||
input_data = ctx.get("input_data", {})
|
||||
entity_id = input_data.get("id")
|
||||
if not entity_id:
|
||||
raise ValueError("se requiere ID")
|
||||
@@ -222,14 +221,16 @@ class SoftDelete(Pipeline):
|
||||
|
||||
|
||||
async def _format(self, ctx):
|
||||
|
||||
ctx["output"]["message"] = f"{self.model.__name__} eliminado corectamente"
|
||||
ctx["output"]["deleted_at"] = ctx["entity"].deleted_at.isoformat()
|
||||
ctx["output"]["deleted_by"] = ctx["entity"].delted_by
|
||||
ctx["output"] = {
|
||||
"id": ctx["entity"].id,
|
||||
"message": f"{self.model.__name__} eliminado correctamente",
|
||||
"deleted_at": ctx["entity"].deleted_at.isoformat() if ctx["entity"].deleted_at else None,
|
||||
"deleted_by": ctx["entity"].deleted_by
|
||||
}
|
||||
return ctx
|
||||
|
||||
class ListPipe(Pipeline):
|
||||
"""Pipeline prefab to list entitys"""
|
||||
"""Pipeline prefab to list entities"""
|
||||
|
||||
def __init__(self, db: Session, user: CurrentUser, model: Type, repository):
|
||||
super().__init__(db, user, f"LIST_{model.__tablename__.upper()}")
|
||||
|
||||
Reference in New Issue
Block a user