async def delete_executors_of_shop( executors_id: List[str], shop_id:str, current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Delete one or many executors to shop with shop id and list of executor id ''' invalid_list=[] if crud_shop.get_shop(db=db,shop_id=shopid) is None: raise UnicornException( messages="SHOP ID NOT FOUND", name=shopid ) current_shop=crud_shop.get_shop(db=db,shop_id=shop_id) if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: raise UnicornException( messages="SHOP ID NOT FOUND", name=shopid ) if len(executors_id) ==0: raise UnicornException( messages="INVALID EXECUTOR ID LIST", name=executors_id ) for id in executors_id: if crud_user.get_user(db=db,user_id=id)is None : invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="EXECUTOR NOT EXIST", name=invalid_list ) for id in executors_id: if crud_user.get_user(db=db,user_id=id).role!="executor" : invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="USER IS NOT EXECUTOR", name=invalid_list ) for id in executors_id: if crud_shop_executor.get_shop_executor(db=db,shop_id=shop_id,executor_id=id) is None: invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="EXECUTOR NOT BELONG TO SHOP", name=invalid_list ) for id in executors_id: crud_shop_executor.delete_shop_executor(db=db,shop_id=shop_id,executor_id=id) return {"message":"update success"}
async def add_many_executor_to_shop(executors_id: List[str], shop_id: str, token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): ''' Asign one or many executors to shop with shop id and list of executor id ''' try: check_sercurity_scopes(token=token, scopes=settings.ADD_EXECUTOR_SHOP_SCOPE) payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) current_shop = crud_shop.get_shop(db=db, shop_id=shop_id) if payload.get("role") == "manager": if crud_channel_manager.get_channel_manager( db=db, channel_id=current_shop.channel_id, manager_id=payload.get("id")) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not belong to your channel") if len(executors_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor id list must be not None") if current_shop is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop id doesn't Exist" + shop_id) for id in executors_id: if crud_user.get_user(db=db, user_id=id) is None or crud_user.get_user( db=db, user_id=id).role != "executor": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid executor id") for id in executors_id: if not crud_shop_executor.get_shop_executor( db=db, shop_id=shop_id, executor_id=id) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor already exist in shop") for id in executors_id: crud_shop_executor.create_new_shop_executor(db=db, shop_id=shop_id, executor_id=id) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "update success"}
async def add_many_executor_to_many_shop( executors_id: List[str], shop_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Asign one or many executors to one or many shops with list of executors and shops request ''' if len(executors_id) ==0 : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid value for executor id list" ) if len(shop_id) ==0 : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid value for shop id list" ) for id in shop_id: current_shop=crud_shop.get_shop(db=db,shop_id=id) if current_shop is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop id doesn't Exist"+id ) if current_user.role=="manager": for id in shop_id: current_shop=crud_shop.get_shop(db=db,shop_id=id) if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop doesn't belong to your channel" ) for id in executors_id: if crud_user.get_user(db=db,user_id=id) is None or crud_user.get_user(db=db,user_id=id).role!="executor": raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="User is Not executor" ) for eid in executors_id: for sid in shop_id: if not crud_shop_executor.get_shop_executor(db=db,shop_id=sid,executor_id=eid) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor already exist in shop" ) for eid in executors_id: for sid in shop_id: crud_shop_executor.create_new_shop_executor(db=db,shop_id=sid,executor_id=eid) return {"message":"update success"}
def update_user(username: str, new_user: User_schema.UserCreate, db: Session = Depends(get_db)): if user_crud.get_user(db, username=username): if username != new_user.username: if user_crud.get_user(db, username=new_user.username) is None: return user_crud.update_user(db, username=username, new_user=new_user) raise HTTPException( status_code=401, detail='Unhable to change username into an existing one') return user_crud.update_user(db, username=username, new_user=new_user) raise HTTPException(status_code=401, detail='user does not exist')
def delete_channel_of_manager( manager_id: str, channel_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["channel_manager"]), db: Session = Depends(deps.get_db) ): manager=crud_user.get_user(db=db,user_id=manager_id) if manager is None or manager.role!="manager" : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id" ) for id in channel_id: if crud_channel.get_channel(db=db,channel_id=id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid channel id" ) for id in channel_id: if crud_channel_manager.get_channel_manager(db=db,channel_id=id,manager_id=manager_id) is None: if crud_channel.get_channel(db=db,channel_id=id): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Channel not asign to manager" ) for id in channel_id: crud_channel_manager.delete_channel_manager(db=db,manager_id=manager_id,channel_id=id) return {"message":"delete success"}
def get_all_channel_not_asign_to_manager(manager_id: str, token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): try: check_sercurity_scopes(token=token, scopes=settings.VIEW_USER_DETAIL_SCOPE) executor = crud_user.get_user(db=db, user_id=manager_id) if executor is None or executor.role != "manager": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id") except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return crud_channel.get_all_not_channel_of_manager(db=db, manager_id=manager_id)
def get_user(username: str, db: Session = Depends(get_db)): # return {'gotten': username} # return connection.execute(users.select().where(users.Column.username == username)).fetchall() db_user = user_crud.get_user(db, username=username) if db_user is None: raise HTTPException(status_code=404, detail='user not found') return db_user
async def delete_shop_for_executors( executors_id: str, shop_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): if current_user.role=="manager": for id in shop_id: if crud_shop.check_shop_manager(db=db,shop_id=id,manager_id=current_user.id) is False: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not belong to your channel" ) executor=crud_user.get_user(db=db,user_id=executors_id) if executor is None or executor.role!="executor" : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid executor id" ) for id in shop_id: if crud_shop_executor.get_shop_executor(db=db,shop_id=id,executor_id=executors_id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not found" ) for id in shop_id: crud_shop_executor.delete_shop_executor(db=db,shop_id=id,executor_id=executors_id) return {"message":"delete success"}
def user_detail(id: str, current_user=Security(deps.get_current_active_user, scopes=["read_user"]), db: Session = Depends(deps.get_db)): ''' View user detail with user id ''' return crud_user.get_user(db=db, user_id=id)
def get_all_shops_executor_not_asign(executor_id: str, current_user=Security( deps.get_current_active_user, scopes=["read_user"]), db: Session = Depends(deps.get_db)): executor = crud_user.get_user(db=db, user_id=executor_id) if executor is None or executor.role != "executor": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid executor id") return crud_shop.get_all_not_shop_of_executor(db=db, executor_id=executor_id)
def get_all_channel_of_manager(manager_id: str, current_user=Security( deps.get_current_active_user, scopes=["read_user"]), db: Session = Depends(deps.get_db)): executor = crud_user.get_user(db=db, user_id=manager_id) if executor is None or executor.role != "manager": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id") return crud_channel.get_all_channel_of_manager(db=db, manager_id=manager_id)
async def delete_executors_of_shop( executors_id: List[str], shop_id:str, current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Delete one or many executors to shop with shop id and list of executor id ''' current_shop=crud_shop.get_shop(db=db,shop_id=shop_id) if current_user.role=="manager": if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not belong to your channel" ) if len(executors_id) ==0: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor id list must be not None" ) if current_shop is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop id doesn't Exist"+shop_id ) for id in executors_id: if crud_user.get_user(db=db,user_id=id)is None or crud_user.get_user(db=db,user_id=id).role!="executor" : raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid executor id" ) for id in executors_id: if crud_shop_executor.get_shop_executor(db=db,shop_id=shop_id,executor_id=id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor not exist in shop" ) for id in executors_id: crud_shop_executor.delete_shop_executor(db=db,shop_id=shop_id,executor_id=id) return {"message":"update success"}
def delete_manager_from_channel( channel_id:str, managers_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["read_channel"]), db: Session = Depends(deps.get_db) ): invalid_list=[] if crud_channel.get_channel(db=db,channel_id=channel_id) is None: raise UnicornException( messages="CHANNEL ID NOT FOUND", name=channel_id ) if len(managers_id) ==0: raise UnicornException( messages="INVALID MANAGER ID LIST", name=managers_id ) for id in managers_id: if crud_user.get_user(db=db,user_id=id) is None or crud_user.get_user(db=db,user_id=id).role!="manager": invalid_list.append(id) if len(invalid_list) !=0: raise UnicornException( messages="INVALID MANAGER ID LIST", name=invalid_list ) for id in managers_id: if crud_channel_manager.get_channel_manager(db=db,channel_id=channel_id,manager_id=id) is None: invalid_list.append(id) if len(invalid_list) !=0: raise UnicornException( messages="MANAGER ID NOT FOUND IN CHANNEL", name=invalid_list ) for id in managers_id: crud_channel_manager.delete_channel_manager(db=db,manager_id=id,channel_id=channel_id) return {"message":"update success"}
def delete_manager_from_channel(channel_id: str, managers_id: List[str], token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): try: check_sercurity_scopes(token=token, scopes=settings.ADD_CHANNEL_MANAGER_SCOPE) if len(managers_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id list ") for id in managers_id: if crud_user.get_user(db=db, user_id=id) is None or crud_user.get_user( db=db, user_id=id).role != "manager": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id list ") for id in managers_id: if crud_channel_manager.get_channel_manager( db=db, channel_id=channel_id, manager_id=id) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Manager not asign to channel") for id in managers_id: crud_channel_manager.delete_channel_manager(db=db, manager_id=id, channel_id=channel_id) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "update success"}
def add_manager_from_channel(channel_id: str, managers_id: List[str], current_user=Security( deps.get_current_active_user, scopes=["read_channel"]), db: Session = Depends(deps.get_db)): if len(managers_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id list ") for id in managers_id: if crud_user.get_user(db=db, user_id=id) is None or crud_user.get_user( db=db, user_id=id).role != "manager": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid manager id list ") for id in managers_id: if not crud_channel_manager.get_channel_manager( db=db, channel_id=channel_id, manager_id=id) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Manager already asign to channel") for id in managers_id: crud_channel_manager.create_channel_manager(db=db, manager_id=id, channel_id=channel_id) return {"message": "add success"}
async def delete_shop_for_executors(executors_id: str, shop_id: List[str], token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): try: check_sercurity_scopes(token=token, scopes=settings.DELETE_EXECUTOR_SHOP_SCOPE) payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) if payload.get("role") == "manager": for id in shop_id: if crud_shop.check_shop_manager( db=db, shop_id=id, manager_id=payload.get("id")) is False: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not belong to your channel") executor = crud_user.get_user(db=db, user_id=executors_id) if executor is None or executor.role != "executor": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid executor id") for id in shop_id: if crud_shop_executor.get_shop_executor( db=db, shop_id=id, executor_id=executors_id) is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop not found") for id in shop_id: crud_shop_executor.delete_shop_executor(db=db, shop_id=id, executor_id=executors_id) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "delete success"}
def user_detail(id: str, token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): ''' View user detail with user id ''' try: check_sercurity_scopes(token=token, scopes=settings.VIEW_USER_DETAIL_SCOPE) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return crud_user.get_user(db=db, user_id=id)
async def add_many_executor_to_many_shop(executors_id: List[str], shop_id: List[str], token: Optional[str] = Header(None), db: Session = Depends(deps.get_db)): ''' Asign one or many executors to one or many shops with list of executors and shops request ''' try: current_user_id = check_sercurity_scopes( token=token, scopes=settings.ADD_EXECUTOR_SHOP_SCOPE) if len(executors_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid value for executor id list") if len(shop_id) == 0: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Invalid value for shop id list") for id in shop_id: current_shop = crud_shop.get_shop(db=db, shop_id=id) if current_shop is None: raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop id doesn't Exist" + id) if crud_channel_manager.get_channel_manager( db=db, channel_id=current_shop.channel_id, manager_id=current_user_id) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Shop doesn't belong to your channel") for id in executors_id: if crud_user.get_user(db=db, user_id=id) is None or crud_user.get_user( db=db, user_id=id).role != "executor": raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="User is Not executor") for eid in executors_id: for sid in shop_id: if not crud_shop_executor.get_shop_executor( db=db, shop_id=sid, executor_id=eid) is None: raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="Executor already exist in shop") for eid in executors_id: for sid in shop_id: crud_shop_executor.create_new_shop_executor(db=db, shop_id=sid, executor_id=eid) except (JWTError, ValidationError): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized ", headers={"WWW-Authenticate": "Bearer"}, ) except (mysql.connector.Error): raise HTTPException( status_code=status.HTTP_502_BAD_GATEWAY, detail="My sql connection error ", headers={"WWW-Authenticate": "Bearer"}, ) return {"message": "update success"}
def sign_up(user: User_schema.UserCreate, db: Session = Depends(get_db)): db_user = user_crud.get_user(db, username=user.username) if db_user: raise HTTPException(status_code=400, detail='user already registered') return user_crud.create_user(db=db, user=user)
async def add_many_executor_to_many_shop( executors_id: List[str], shop_id:List[str], current_user= Security(deps.get_current_active_user,scopes=["shop_executor"]), db: Session = Depends(deps.get_db) ): ''' Asign one or many executors to one or many shops with list of executors and shops request ''' invalid_list=[] if len(executors_id) ==0 : raise UnicornException( messages="EXECUTOR ID LIST IS EMPTY", name=executors_id ) if len(shop_id) ==0 : raise UnicornException( messages="SHOP ID LIST IS EMPTY", name=executors_id ) for id in shop_id: current_shop=crud_shop.get_shop(db=db,shop_id=id) if current_shop is None: invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="SOME SHOP NOT FOUND", name=invalid_list ) for id in shop_id: current_shop=crud_shop.get_shop(db=db,shop_id=id) if crud_channel_manager.get_channel_manager(db=db,channel_id=current_shop.channel_id,manager_id=current_user.id) is None: invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="SOME SHOP NOT BELONG TO YOUR CHANNEL", name=invalid_list ) for id in executors_id: if crud_user.get_user(db=db,user_id=id)is None : invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="EXECUTOR NOT EXIST", name=invalid_list ) for id in executors_id: if crud_user.get_user(db=db,user_id=id).role!="executor" : invalid_list.append(id) if len(invalid_list)!=0: raise UnicornException( messages="USER IS NOT EXECUTOR", name=invalid_list ) for eid in executors_id: for sid in shop_id: invalid_list.append([eid,sid]) if len(invalid_list)!=0: raise UnicornException( messages="EXECUTOR&SHOP ALREADY ASIGN", name=invalid_list ) for eid in executors_id: for sid in shop_id: crud_shop_executor.create_new_shop_executor(db=db,shop_id=sid,executor_id=eid) return {"message":"update success"}