diff --git a/pkgs/clan-cli/clan_cli/webui/app.py b/pkgs/clan-cli/clan_cli/webui/app.py index 9e5ffd8..d1d1120 100644 --- a/pkgs/clan-cli/clan_cli/webui/app.py +++ b/pkgs/clan-cli/clan_cli/webui/app.py @@ -33,7 +33,9 @@ async def lifespan(app: FastAPI) -> Any: def setup_app() -> FastAPI: # bind sql engine - sql_models.Base.metadata.drop_all(engine) + # TODO comment aut and add flag to run with pupulated data rm *.sql run pytest with marked then start clan webui + # https://docs.pytest.org/en/7.1.x/example/markers.html + # sql_models.Base.metadata.drop_all(engine) sql_models.Base.metadata.create_all(bind=engine) app = FastAPI(lifespan=lifespan) diff --git a/pkgs/clan-cli/clan_cli/webui/routers/sql_connect.py b/pkgs/clan-cli/clan_cli/webui/routers/sql_connect.py index e2b2813..dab1021 100644 --- a/pkgs/clan-cli/clan_cli/webui/routers/sql_connect.py +++ b/pkgs/clan-cli/clan_cli/webui/routers/sql_connect.py @@ -175,6 +175,28 @@ async def attach( db: Session = Depends(sql_db.get_db), ) -> dict[str, str]: background_tasks.add_task( - sql_crud.check_health, db, entity_did, message="Not attched any more" + attach_entity(entity_did, db), db, entity_did ) return {"message": "Attaching in the background"} + +# TODO +def attach_entity(entity_did: str, db: Session) -> None: + sql_crud.attach_entity_by_did(db, entity_did, True) + while db_entity.attached: + #query status endpoint + #except not reached set false + time.sleep(5) + + +@router.get("/api/v1/detach", response_model=Optional[Entity], tags=[Tags.entities]) +async def detach( + background_tasks: BackgroundTasks, + entity_did: str = "did:sov:test:1234", + skip: int = 0, + limit: int = 100, + db: Session = Depends(sql_db.get_db), +) -> dict[str, str]: + background_tasks.add_task( + sql_crud.set_attached_by_entity_did, db, entity_did, False + ) + return {"message": "Detaching in the background"} \ No newline at end of file diff --git a/pkgs/clan-cli/clan_cli/webui/sql_crud.py b/pkgs/clan-cli/clan_cli/webui/sql_crud.py index 3d46cb7..9ce9210 100644 --- a/pkgs/clan-cli/clan_cli/webui/sql_crud.py +++ b/pkgs/clan-cli/clan_cli/webui/sql_crud.py @@ -134,24 +134,30 @@ def get_entities( ) -> List[sql_models.Entity]: return db.query(sql_models.Entity).offset(skip).limit(limit).all() +# get attached +def get_attached_entities( + db: Session, skip: int = 0, limit: int = 100 +) -> List[sql_models.Entity]: + return db.query(sql_models.Entity).offset(skip).limit(limit).filter(sql_models.Entity.attached == True) + def get_entity_by_did(db: Session, did: str) -> Optional[sql_models.Entity]: return db.query(sql_models.Entity).filter(sql_models.Entity.did == did).first() -def check_health(db: Session, entity_did: str, message: str) -> str: +# set attached + # None if did not found + # Returns same entity if setting didnt changed something +def set_attached_by_entity_did(db: Session, entity_did: str, value: bool) -> bool: # ste attached to true db_entity = get_entity_by_did(db, entity_did) if db_entity is not None: # db_entity.attached = Column(True) - setattr(db_entity, "attached", True) + setattr(db_entity, "attached", value) # save changes in db db.add(db_entity) db.commit() db.refresh(db_entity) - # check every 5 secounds if status has changed - while db_entity.attached: - time.sleep(5) - return f"{entity_did} message" + return db_entity else: - return f"{entity_did} not found and not attached" + return db_entity