async def add_bus_stop(data: pydantic_models.AddBusStop, token_check: check_access_token = Depends()): """ Adding a transport stop HEADER: Authorization: "access_token" """ if not token_check[0]: return pydantic_models.TokenOut(status=False) default_type_bus_stop = ['dirty', 'clean'] cities = writers.keys() city = data.city if data.city in cities else None if not city: return pydantic_models.TokenOut(status=False) bus_stops = get_stop_city(city) bus_stop = data.bus_stop_name if data.bus_stop_name in bus_stops else None type_bus_stop = data.type_bus_stop if data.type_bus_stop in default_type_bus_stop else default_type_bus_stop[ 0] list_key: int = 0 if type_bus_stop == default_type_bus_stop[0] else 1 user = await user_repository.get_user('user_name', token_check[1]) email_model = await user_repository.get_other_model('user_email', user) if bus_stop and email_model.is_activatet: user_two_model = await user_repository.get_other_model( 'user_info', user) if not user_two_model or user_two_model.add_bus_stop_time and user_two_model.add_bus_stop_time.timestamp( ) >= int(time()) - 900: return pydantic_models.TokenOut(status=False) model = writers.get(city)[list_key] await model.objects.create(bus_stop=bus_stop.lower(), time=int(time())) await user_two_model.update(add_bus_stop_time=datetime.now()) return pydantic_models.TokenOut(status=True) return pydantic_models.TokenOut(status=False)
async def refresh_password(data: pydantic_models.RefreshPasswordIn): user = await user_repository.get_user('user_name', data.login) if not user or not verify_password(data.old_password, user.hashed_password): return pydantic_models.TokenOut(status=False) new_password_hash: str = get_password_hash(data.new_password) await user.update(hashed_password=new_password_hash) return pydantic_models.TokenOut(status=True)
async def refresh_token(data: pydantic_models.RefreshTokenIn): user_security = await user_repository.get_refresh_token_model( data.refresh_token) if not user_security: return pydantic_models.TokenOut(status=False) user_name = token_tools.get_user_name_in_token(data.refresh_token) if not user_name: return pydantic_models.TokenOut(status=False) tokens = create_tokens(data={"sub": user_name}) await user_security.update(refresh_token=tokens[1]) return pydantic_models.UserAuthOut(access_token=tokens[0], refresh_token=tokens[1])
async def auth(data: pydantic_models.UserAuthIn): """ User authorization """ user = await user_repository.get_user('user_name', data.login) authenticate = authenticate_user(user, data.password) if not authenticate: return pydantic_models.TokenOut(status=False) _tokens: list = create_tokens(data={"sub": data.login}) security_model = await user_repository.get_other_model( 'user_security', user) await security_model.update(refresh_token=_tokens[1]) return pydantic_models.UserAuthOut(access_token=_tokens[0], refresh_token=_tokens[1])
async def profile(token_check: check_access_token = Depends()): """ Gives profile information \n Still need to pass to header access_token example \n HEADER: Authorization: "access_token" """ if not token_check[0]: return pydantic_models.TokenOut(status=False) user = await user_repository.get_user('user_name', token_check[1]) user_email = await user_repository.get_other_model('user_email', user) user_info = await user_repository.get_other_model('user_info', user) data = { 'user_name': user_info.user_name, 'create_at': user_info.create_at, 'add_bus_stop_time': user_info.add_bus_stop_time, 'email_activatet': user_email.is_activatet, 'email': user.email } return data
async def create_account(request: Request, background_tasks: BackgroundTasks, data: pydantic_models.AccountCreateIn): if not Email.email_validation( data.email) or await user_repository.is_user_exists( data.email, data.login): return pydantic_models.TokenOut(status=False) _tokens: list = create_tokens(data={"sub": data.login}) password_hash = get_password_hash(data.password) user_create = pydantic_models.User(user_name=data.login, hashed_password=password_hash, email=data.email, refresh_token=_tokens[1]) await user_repository.create_user(user_create) postfix: str = get_email_verify_postfix(data.login) url = request.url_for('valid_email') + f'?id={postfix}' background_tasks.add_task(Email.send_email, data.email, url) return pydantic_models.UserAuthOut(access_token=_tokens[0], refresh_token=_tokens[1])
async def check_token(data: pydantic_models.TokenIn): """ Check refresh_token or access_token """ token_status = await token_tools.check_token(data.token) return pydantic_models.TokenOut(status=token_status)
async def valid_email(id: str): email_model = await user_repository.get_email_verify(id) if email_model and not email_model.is_activatet: await email_model.update(is_activatet=True) return pydantic_models.TokenOut(status=True) return pydantic_models.TokenOut(status=False)