I am writing a small api for creating different objects. And I wanted to carry out a routine check that can be found on every website with registration. But I came across such an unpleasant use of the if operator.
class AuthenticateController(RegisterCRUD, Controller):
prefix = "/api/auth"
tags = ["auth"]
refresh_token_ttl = 30*60
access_token_ttl = 15*60
@post("/register")
async def register(user: UserIn):
if not (user.name and user.secondname and user.lastname and user.phone and user.login):
raise HTTPException(status_code=400, detail="Please fill in all required fields.")
if not RegisterCRUD.phone_validator(user.phone):
raise HTTPException(status_code=400, detail="Please enter the correct phone number.")
if RegisterCRUD.check_phone(user.phone):
raise HTTPException(status_code=400, detail="This phone number is already registered.")
if RegisterCRUD.check_login(user.login):
raise HTTPException(status_code=400, detail="This login is already occupied.")
if RegisterCRUD.register(user.name, user.secondname, user.lastname, user.phone,
user.login, HashedData.hash_password(user.password)):
return JSONResponse(status_code=201, content={"message": "The user has been successfully created."})
raise HTTPException(status_code=501, detail="Failed to create a user.")
But in general, my login and phone db models have the value unique=True. So trying to create the same values causes an error. But I haven't figured out how to link the return of the desired message to it.
I would like to clarify whether it is correct to carry out verification in this way? Or there is a more different way.
In general, I am waiting for criticism and advice! Thank you all in advance!