async def list_comments_for_announcement( user: Optional[User] = Depends( get_current_user_authorizer(required=False)), comments_repo: CommentsRepository = Depends( get_repository(CommentsRepository)), ) -> ListOfCommentsInResponse: comments = await comments_repo.get_comments_for_announcement( announcement=None, user=user) return ListOfCommentsInResponse(comments=comments)
async def get_post_by_id( id: int, posts_repo: PostsRepository = Depends(get_repository(PostsRepository)) ) -> PostPublic: post = await posts_repo.get_post_by_id(id=id) if not post: raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="No post found with that id.") return post
async def create_new_cleaning( new_flashcard: FlashCardCreate = Body(..., embed=True), flashcards_repo: FlashCardsRepository = Depends( get_repository(FlashCardsRepository)), ) -> FlashCardPublic: created_flashcard = await flashcards_repo.create_flashcards( new_flashcard=new_flashcard) return created_flashcard
async def get_cleaning_by_id( cleaning_id: int, cleanings_repo: CleaningsRepository = Depends(get_repository(CleaningsRepository)) ) -> CleaningPublic: cleaning = await cleanings_repo.get_cleaning_by_id(get_id=cleaning_id) if not cleaning: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No cleaning found that id") return cleaning
async def delete_collection_by_id( collection_id: int = Path(..., ge=1), current_user: UserInDB = Depends(get_current_active_user), collection_repo: CollectionsRepository = Depends(get_repository(CollectionsRepository)), ) -> int: deleted_id = await collection_repo.update_delete_collection_field_by_id(id=collection_id, requesting_user=current_user) if not deleted_id: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No cleaning found with that id.") return deleted_id
async def get_collections_by_id( collection_id: int = Path(..., ge=1), current_user: UserInDB = Depends(get_current_active_user), collections_repo: CollectionsRepository = Depends(get_repository(CollectionsRepository)), ) -> CollectionPublic: collection = await collections_repo.get_collection_by_id(id=collection_id, requesting_user=current_user) if not collection: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No collection found with that id.") return collection
async def create_new_cleaning( new_cleaning: CleaningCreate = Body(..., embed=True), cleanings_repo: CleaningsRepository = Depends( get_repository(CleaningsRepository)), ) -> CleaningPublic: created_cleaning = await cleanings_repo.create_cleaning( new_cleaning=new_cleaning) return created_cleaning
async def update_own_profile( profile_update: ProfileUpdate = Body(..., embed=True), current_user: UserInDB = Depends(get_current_active_user), profiles_repo: ProfilesRepository = Depends( get_repository(ProfilesRepository)), ) -> ProfilePublic: updated_profile = await profiles_repo.update_profile( profile_update=profile_update, requesting_user=current_user) return updated_profile
async def _get_current_user_optional( repo: UsersRepository = Depends(get_repository(UsersRepository)), token: str = Depends(_get_authorization_header_retriever(required=False)), ) -> Optional[User]: print("_get_current_user_optional token:", token) if token: return await _get_current_user(repo, token) return None
async def create_new_vehicle( current_user: UserPublic = Depends(get_current_active_user), new_vehicle: VehiclesCreate = Body(..., embed=True), vehicles_repo: VehiclesRepository = Depends( get_repository(VehiclesRepository)), ) -> VehiclesPublic: created_vehicle = await vehicles_repo.create_vehicle( new_vehicle=new_vehicle, id=current_user.id) return created_vehicle
async def register_new_user( new_user: UserCreate = Body(..., embed=True), user_repo: UsersRepository = Depends(get_repository(UsersRepository)), ) -> UserPublic: created_user = await user_repo.register_new_user(new_user=new_user) access_token = AccessToken( access_token=auth_service.create_access_token_for_user(user=created_user), token_type="bearer" ) return UserPublic(**created_user.dict(), access_token=access_token)
async def get_cleaner_evaluation_for_cleaning_from_path( cleaning: CleaningInDB = Depends(get_cleaning_by_id_from_path), cleaner: UserInDB = Depends(get_user_by_username_from_path), evals_repo: EvaluationsRepository = Depends(get_repository(EvaluationsRepository)), ) -> EvaluationInDB: evaluation = await evals_repo.get_cleaner_evaluation_for_cleaning(cleaning=cleaning, cleaner=cleaner) if not evaluation: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No evaluation found for that cleaning.") return evaluation
async def create_new_accident_sketch( accident_id: int, new_accident_sketch: Accident_Sketch_Create = Body(..., embed=True), current_user: UserPublic = Depends(get_current_active_user), sketch_repo: AccidentSketchRepository = Depends(get_repository(AccidentSketchRepository)), accident_stmt_repo: AccidentStatementRepository = Depends(get_repository(AccidentStatementRepository)), ) -> Accident_Sketch_Public: accident_stmt = await accident_stmt_repo.get_accident_statement_by_accident_id_user_id(accident_id= accident_id, user_id = current_user.id) if not accident_stmt: raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="You can not update this sketch") if accident_stmt.done: raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="You have declared this accident statement as completed") new_accident_sketch.statement_id=accident_stmt.id created_sketch = await sketch_repo.create_new_accident_sketch(new_accident_sketch=new_accident_sketch, statement_id= accident_stmt.id) return created_sketch
async def list_all_user_cleanings( current_user: UserInDB = Depends(get_current_active_user), cleanings_repo: CleaningsRepository = Depends( get_repository(CleaningsRepository)), ) -> List[CleaningPublic]: cleanings = await cleanings_repo.list_all_user_cleanings( requesting_user=current_user) return cleanings
async def get_all_vehicles( current_user: UserPublic = Depends(get_current_active_user), vehicles_repo: VehiclesRepository = Depends( get_repository(VehiclesRepository))) -> List[VehiclesPublic]: if current_user.is_master: return await vehicles_repo.get_all_vehicles() else: raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="No access")
async def create_new_accident_image( accident_id: int = Form(...), image: UploadFile = File(...), current_user: UserPublic = Depends(get_current_active_user), accident_image_repo: AccidentImageRepository = Depends(get_repository(AccidentImageRepository)), accident_stmt_repo: AccidentStatementRepository = Depends(get_repository(AccidentStatementRepository)), ): accident_stmt = await accident_stmt_repo.get_accident_statement_by_accident_id_user_id(accident_id= accident_id, user_id = current_user.id) if not accident_stmt: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="You can not add image") print("OK") if accident_stmt.done: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="You have declared this accident statement as completed") contentsImage = await image.read() new_accident_image = Accident_Image_Create(statement_id=accident_stmt.id, image = contentsImage) accident_image = await accident_image_repo.add_new_accident_image(new_accident_image = new_accident_image) return accident_image
async def update_cleaning_by_id( cleaning: CleaningInDB = Depends(get_cleaning_by_id_from_path), cleaning_update: CleaningUpdate = Body(..., embed=True), cleanings_repo: CleaningsRepository = Depends( get_repository(CleaningsRepository)), ) -> CleaningPublic: updated_cleaning = await cleanings_repo.update_cleaning( cleaning=cleaning, cleaning_update=cleaning_update) return updated_cleaning
async def create_new_cleaning( new_cleaning: CleaningCreate = Body(..., embed=True), current_user: UserInDB = Depends(get_current_active_user), cleanings_repo: CleaningsRepository = Depends( get_repository(CleaningsRepository)), ) -> CleaningPublic: created_cleaning = await cleanings_repo.create_cleaning( new_cleaning=new_cleaning, requesting_user=current_user) return created_cleaning
async def update_article_by_slug( article_update: ArticleInUpdate = Body(..., embed=True, alias="article"), current_article: Article = Depends(get_article_by_slug_from_path), articles_repo: ArticlesRepository = Depends(get_repository(ArticlesRepository)), ) -> ArticleInResponse: slug = get_slug_for_article(article_update.title) if article_update.title else None article = await articles_repo.update_article( article=current_article, slug=slug, **article_update.dict() ) return ArticleInResponse(article=article)
async def _get_current_user_optional( repo: UsersRepository = Depends(get_repository(UsersRepository)), token: str = Depends( _get_authorization_header_retriever(required=False)), settings: AppSettings = Depends(get_app_settings), ) -> Optional[User]: if token: return await _get_current_user(repo, token, settings) return None
async def delete_cleaning_by_id( cleaning_id: int = Path(..., ge=1, title="The ID of the cleaning to delete."), cleanings_repo: CleaningsRepository = Depends(get_repository(CleaningsRepository)) ) -> int: deleted_id = await cleanings_repo.delete_cleanings_by_id(delete_id=cleaning_id) if not deleted_id: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No cleaning found with that id.") return deleted_id
async def get_accident_by_id(id: int, current_user: UserPublic = Depends(get_current_active_user), accident_repo: AccidentRepository = Depends(get_repository(AccidentRepository)), accidentstmt_repo: AccidentStatementRepository = Depends(get_repository(AccidentStatementRepository)))\ -> AccidentPublic: if current_user.is_superuser: return await accident_repo.get_accident_by_id(id = id) else: print("ok") accident = await accident_repo.get_accident_from_user_with_statement_id(id=id, user_id = current_user.id) accident1 = await accident_repo.get_accident_by_temporary_driver_by_email(id=id, email = current_user.email) if not accident: if not accident1: raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="No accident found") else: accident = accident1 return accident else: return accident
async def new_user_role_by_vehicle_id( vehicle_id: int, current_user: UserPublic = Depends(get_current_active_user), vehicles_repo: VehiclesRepository = Depends( get_repository(VehiclesRepository)), new_role: RoleCreate = Body(..., embed=True), roles_repo: RolesRepository = Depends(get_repository(RolesRepository)) ) -> RolePublic: vehicle = await vehicles_repo.get_vehicle_by_id(id=vehicle_id, user_id=current_user.id) if not vehicle: raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Please select one of your vehicles") new_role.vehicle_id = vehicle_id new_role.user_id = current_user.id role = await roles_repo.create_new_role_of_user_for_vehicle( new_role=new_role, vehicle_id=vehicle_id, user_id=current_user.id) return role
async def update_insurance_by_vehicle_id( vehicle_id: int, current_user: UserPublic = Depends(get_current_active_user), vehicles_repo: VehiclesRepository = Depends( get_repository(VehiclesRepository)), insurance_update: InsuranceUpdate = Body(..., embed=True), insurances_repo: InsuranceRepository = Depends( get_repository(InsuranceRepository)) ) -> InsurancePublic: vehicle = await vehicles_repo.get_vehicle_by_id(id=vehicle_id, user_id=current_user.id) if not vehicle: raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="Please select one of your vehicles") insurance = await insurances_repo.update_last_created_insurance( vehicle_id=vehicle_id, insurance_update=insurance_update) return insurance
async def create_comment_for_article( comment_create: CommentInCreate = Body(..., embed=True, alias="comment"), article: Article = Depends(get_article_by_slug_from_path), user: User = Depends(get_current_user_authorizer()), comments_repo: CommentsRepository = Depends(get_repository(CommentsRepository)), ) -> CommentInResponse: comment = await comments_repo.create_comment_for_article( body=comment_create.body, article=article, user=user, ) return CommentInResponse(comment=comment)
async def list_comments_for_article( article: Article = Depends(get_article_by_slug_from_path), user: Optional[User] = Depends( get_current_user_authorizer(required=False)), comments_repo: CommentsRepository = Depends( get_repository(CommentsRepository)), ) -> ListOfCommentsInResponse: comments = await comments_repo.get_comments_for_article(article=article, user=user) return ListOfCommentsInResponse(comments=comments)
async def get_user_from_token( *, token: str = Depends(oauth2_scheme), user_repo: UsersRepository = Depends(get_repository(UsersRepository)), ) -> Optional[UserInDB]: try: username = auth_service.get_username_from_token(token=token, secret_key=str(SECRET_KEY)) user = await user_repo.get_user_by_username(username=username) except Exception as e: raise e return user
async def get_profile_by_username( username: str = Path(..., min_length=3, regex='[a-zA-Z0-9_-]+$'), current_user: UserInDB = Depends(get_current_active_user), profile_repo: ProfilesRepository = Depends(get_repository(ProfilesRepository)), ) -> ProfilePublic: profile = await profile_repo.get_profile_by_username(username=username) if not profile: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='No profile foudn with that username.') return profile
async def delete_one( db_id: int, db_repo: ChessDbRepository = Depends(get_repository(ChessDbRepository)), user: Auth0User = Depends(requires_auth), ) -> None: db = await db_repo.find_by_id(db_id=db_id) if not db: raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail="this database doesn't exists") if db.user_id != user.id: raise HTTPException(status_code=HTTP_401_UNAUTHORIZED) await db_repo.delete_db(db_id=db_id)
async def register_new_user( new_user: UserCreate = Body(..., embed=True), user_repo: UsersRepository = Depends(get_repository(UsersRepository)), ) -> UserPublic: created_user = await user_repo.register_new_user(new_user=new_user) access_token = AccessToken( access_token=auth_service.create_access_token_for_user(user=created_user), token_type="bearer" ) # we can return the access_token because we added it as # an optional property in UserPublic return created_user.copy(update={"access_token": access_token})