generated from Luis/nextjs-python-web-template
attached
This commit is contained in:
@@ -33,7 +33,9 @@ async def lifespan(app: FastAPI) -> Any:
|
|||||||
|
|
||||||
def setup_app() -> FastAPI:
|
def setup_app() -> FastAPI:
|
||||||
# bind sql engine
|
# 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)
|
sql_models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
app = FastAPI(lifespan=lifespan)
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|||||||
@@ -175,6 +175,28 @@ async def attach(
|
|||||||
db: Session = Depends(sql_db.get_db),
|
db: Session = Depends(sql_db.get_db),
|
||||||
) -> dict[str, str]:
|
) -> dict[str, str]:
|
||||||
background_tasks.add_task(
|
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"}
|
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"}
|
||||||
@@ -134,24 +134,30 @@ def get_entities(
|
|||||||
) -> List[sql_models.Entity]:
|
) -> List[sql_models.Entity]:
|
||||||
return db.query(sql_models.Entity).offset(skip).limit(limit).all()
|
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]:
|
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()
|
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
|
# ste attached to true
|
||||||
db_entity = get_entity_by_did(db, entity_did)
|
db_entity = get_entity_by_did(db, entity_did)
|
||||||
if db_entity is not None:
|
if db_entity is not None:
|
||||||
# db_entity.attached = Column(True)
|
# db_entity.attached = Column(True)
|
||||||
setattr(db_entity, "attached", True)
|
setattr(db_entity, "attached", value)
|
||||||
# save changes in db
|
# save changes in db
|
||||||
db.add(db_entity)
|
db.add(db_entity)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_entity)
|
db.refresh(db_entity)
|
||||||
# check every 5 secounds if status has changed
|
return db_entity
|
||||||
while db_entity.attached:
|
|
||||||
time.sleep(5)
|
|
||||||
return f"{entity_did} message"
|
|
||||||
else:
|
else:
|
||||||
return f"{entity_did} not found and not attached"
|
return db_entity
|
||||||
|
|||||||
Reference in New Issue
Block a user