sql_app: Access log for user (access history)

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-05-23 19:44:04 +03:00
parent 52a7cffce2
commit 36123b1090
3 changed files with 25 additions and 1 deletions

View File

@ -36,6 +36,8 @@ def get_users(db: Session, skip: int = 0, limit: int = 100):
def get_access_log_for_door_by_door_mac(db: Session, bluetooth_mac : str): def get_access_log_for_door_by_door_mac(db: Session, bluetooth_mac : str):
return db.query(models.DoorAccessLog).filter(models.DoorAccessLog.iot_dev_bluetooth_mac == bluetooth_mac).all() return db.query(models.DoorAccessLog).filter(models.DoorAccessLog.iot_dev_bluetooth_mac == bluetooth_mac).all()
def get_access_log_for_user_by_id(db: Session, id : str):
return db.query(models.DoorAccessLog).filter(models.DoorAccessLog.user_id == id).all()
def create_user(db: Session, user: schemas.UserCreate): def create_user(db: Session, user: schemas.UserCreate):
key = crypto.gen_new_key(user.password) key = crypto.gen_new_key(user.password)

View File

@ -16,7 +16,6 @@ oauth = OAuth2PasswordBearer(tokenUrl="tkn")
app = FastAPI() app = FastAPI()
# Dependency
def get_db(): def get_db():
db = SessionLocal() db = SessionLocal()
try: try:
@ -171,6 +170,20 @@ def get_access_log_for_door(request : schemas.AccessLogRequest,
if not device: raise HTTPException(status_code=404, detail="Iot Entity not found") if not device: raise HTTPException(status_code=404, detail="Iot Entity not found")
return crud.get_access_log_for_door_by_door_mac(db, request.bluetooth_mac) return crud.get_access_log_for_door_by_door_mac(db, request.bluetooth_mac)
@app.post("/admin/user/accesslog/email/", tags=['Admin'])
def get_access_log_history_for_user(request : schemas.UserAccessLogRequestEmail,
db : Session = Depends(get_db)):
user = crud.get_user_by_email(db, request.email)
if not user: raise HTTPException(status_code=404, detail="User not found")
return crud.get_access_log_for_user_by_id(db, user.id)
@app.post("/admin/user/accesslog/username/", tags=['Admin'])
def get_access_log_history_for_user(request : schemas.UserAccessLogRequestUsername,
db : Session = Depends(get_db)):
user = crud.get_user_by_username(db, request.username)
if not user: raise HTTPException(status_code=404, detail="User not found")
return crud.get_access_log_for_user_by_id(db, user.id)
@app.get("/users/acesslist/", response_model=List[schemas.IotEntity], tags=['Users']) @app.get("/users/acesslist/", response_model=List[schemas.IotEntity], tags=['Users'])
def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_active_user)): def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_active_user)):
user = crud.get_user_by_username(db, current_user.username) user = crud.get_user_by_username(db, current_user.username)

View File

@ -100,3 +100,12 @@ class DoorAccessLog(BaseModel):
class AccessLogRequest(BaseModel): class AccessLogRequest(BaseModel):
bluetooth_mac : str bluetooth_mac : str
class UserAccessLogRequestUsername(BaseModel):
username : str
class UserAccessLogRequestEmail(BaseModel):
email : str
class UserAccessLogRequestID(BaseModel):
id : int