async def read_users_actions_periods_histogram( db: Session = Depends(get_db), period_time: Literal["hour", "day", "month"] = Query( "day", description=( "Period of time for the histogram. It should be one of:\n\n" "- `hour`\n- `day`\n- `month`\n"), ), current_user: schemas.User = Depends(get_current_user), ): """ A `GET` call that returns an histogram containing information about all different types of actions. Each registered action will have the following information: - **timestamps**: A list with the timestamps of the action - **size**: The total number of timestamps for the action - **min**: The first timestamp of the action - **max**: The last timestamp of the action """ actions_data = crud.get_users_periods_histogram(db, period_time) # register last actions query crud.create_user_action( db, schemas.ActionCreate( **{"title": f"Queried period histogram ({period_time})"}, ), current_user.id, ) return actions_data
async def login_for_access_token( form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db), ): """ A `POST` call to login into the server and receive a `Token` to be used for API's private calls. """ db_user = authenticate_user(form_data.username, form_data.password, db) if not db_user: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect username or password", headers={"WWW-Authenticate": "Bearer"}, ) access_token = create_access_token( data={"sub": db_user.username}, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES), ) # register user login crud.create_user_action( db, schemas.ActionCreate(**{"title": "Logged into account"}), db_user.id, ) return {"access_token": access_token, "token_type": "bearer"}
async def read_actions( user_id: int, sort: str = Query( "desc", title="Sort results", description="It should be one of: `asc` or `desc`", ), limit: int = Query( 100, title="Limit results", description=( "The results will be limited to the supplied number. If the " "supplied number is `0`, all the result will be shown."), ), db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_user), ): """A `GET` call to retrieve the user actions information.""" assert check_user_id(current_user.id, user_id, "actions") is True if sort not in {"asc", "desc"}: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=( WRONG_QUERY_ARGUMENTS_MSG.format(query_arg="order_direction") + "It should be one of: `asc` or `desc`."), ) actions = crud.get_user_actions( db, user_id=user_id, sort=sort, limit=limit, ) # register actions query order = {"asc": "ascending", "desc": "descending"} lim = f"{'unlimited' if limit == 0 else ('limited to ' + str(limit))}" crud.create_user_action( db, schemas.ActionCreate( **{"title": f"Queried actions in {order[sort]} sorting ({lim})"}, ), user_id, ) return actions
async def change_user_password( user_id: int, new_password: str, db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_user), ): """A `PUT` call to update user password.""" assert check_user_id(current_user.id, user_id, "password") is True crud.change_user_password(db, user_id, new_password) # register action: changed user password crud.create_user_action( db, schemas.ActionCreate(**{"title": "Changed user password"}), user_id, ) return {"details": "Successfully changed user password"}
async def read_users_actions_types_histogram( db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_user), ): """ A `GET` call that returns all types of registered actions, alongside the count for each action. The result will be a dict, where the keys are the action's title, and the value the number of times that the action was used. """ types_of_actions = crud.get_users_types_histogram(db) # register actions types query crud.create_user_action( db, schemas.ActionCreate(**{"title": "Queried types histogram"}), current_user.id, ) return types_of_actions
async def read_last_actions( user_id: int, db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_user), ): """A `GET` call to query latest action of each kind.""" assert check_user_id(current_user.id, user_id, "last actions") is True last_actions = crud.get_latest_user_actions( db, user_id=user_id, ) # register last actions query crud.create_user_action( db, schemas.ActionCreate(**{"title": f"Queried last actions"}), user_id, ) return last_actions