main, crud: implement disallow user

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-04-18 02:39:45 +03:00
parent 32c58daf57
commit 3302c4dc39
2 changed files with 16 additions and 7 deletions

View File

@ -14,7 +14,7 @@ def get_user(db: Session, user_id: int):
return db.query(models.User).get(user_id)
def get_iot_entity(db: Session, id: int):
return db.query(models.IotEntity).filter(models.IotEntity.id == id).first()
return db.query(models.IotEntity).get(id)
def get_iot_entity_by_description(db: Session, description: str):
return db.query(models.IotEntity).filter(models.IotEntity.description == description).first()
@ -41,7 +41,6 @@ def create_user(db: Session, user: schemas.UserCreate):
db.refresh(db_user)
return db_user
def get_iot_entities(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.IotEntity).offset(skip).limit(limit).all()
@ -56,12 +55,24 @@ def create_iot_entity(db: Session, iot_entity: schemas.IotEntityCreate):
def create_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int):
# Ensure link is not already present and it does not allow duplicates
link = db.query(models.UserAuthToIoTDev).filter(models.UserAuthToIoTDev.user_id == user_id).filter(models.UserAuthToIoTDev.iot_entity_id == iot_dev_id).first()
if link: return True
new_link = models.UserAuthToIoTDev(user_id=user_id, iot_entity_id=iot_dev_id)
db.add(new_link)
db.commit()
db.refresh(new_link)
return True
def remove_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int):
# Ensure link is not already present and it does not allow duplicates
link = db.query(models.UserAuthToIoTDev).filter(models.UserAuthToIoTDev.user_id == user_id).filter(models.UserAuthToIoTDev.iot_entity_id == iot_dev_id).first()
if not link: return True
db.delete(link)
db.flush()
db.commit()
#db.refresh(link)
return True
def set_open_door_request(db: Session, iot_entity_id: int):
device = get_iot_entity(db, iot_entity_id)
setattr(device, "open_request", True)

View File

@ -102,7 +102,6 @@ def read_user(user_id: int, db: Session = Depends(get_db)):
raise HTTPException(status_code=404, detail="User not found")
return db_user
# TODO: Can duplicate
@app.post("/admin/users/allowdevice/id", tags=['Admin'])
def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)):
user = crud.get_user(db, request.user_id)
@ -117,7 +116,7 @@ def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReques
if not res:
raise HTTPException(status_code=500, detail="Could not complete operation")
return user
return
@app.post("/admin/users/disallowdevice/id", tags=['Admin'])
def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)):
@ -129,8 +128,7 @@ def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReq
if not iot_entity:
raise HTTPException(status_code=404, detail="Iot Entity not found")
#res = crud.create_user_link_to_iot(db, request.user_id, request.iot_entity_id)
# Implement remove link
res = crud.remove_user_link_to_iot(db, request.user_id, request.iot_entity_id)
if not res:
raise HTTPException(status_code=500, detail="Could not complete operation")