mekasu0124

Untitled

Nov 4th, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. class UserBase(BaseModel):
  2.     first_name: str = Field(alias="firstName")
  3.     last_name: str = Field(alias="lastName")
  4.     email_address: str = Field(alias="emailAddress")
  5.     phone_number: str = Field(alias="phoneNumber")
  6.     password: str = Field(alias="password")
  7.     date_of_birth: str = Field(alias="dateOfBirth")
  8.     grade: int = Field(alias="grade")
  9.  
  10. class UserModel(UserBase):
  11.     id: int
  12.  
  13.     class Config:
  14.         from_attributes = True
  15.  
  16. class LoginBase(BaseModel):
  17.     email_address: str = Field(alias="emailAddress")
  18.     password: str = Field(alias="password")
  19.  
  20. class LoginModel(LoginBase):
  21.     id: int
  22.  
  23.     class Config:
  24.         from_attributes = True
  25.  
  26. @app.post("/create_account/")
  27. async def create_new_user(user: UserBase, db: db_dependency):
  28.     if not validate_email(user.email_address):
  29.         raise HTTPException(status_code=400, detail="Not A Valid Email")
  30.    
  31.     if not validate_password(user.password):
  32.         raise HTTPException(status_code=400, detail="Not A Valid Password")
  33.    
  34.     if not is_name_not_in_password(user.first_name, user.last_name, user.password):
  35.         raise HTTPException(status_code=400, detail="Name Cannot Be In Password")
  36.    
  37.     if not validate_universal_phone_number(user.phone_number):
  38.         raise HTTPException(status_code=400, detail="Invalid Phone Number")
  39.    
  40.     query = db.query(models.User.id).filter(models.User.email_address == user.email_address)
  41.     user_exists = db.query(query.exists()).scalar()
  42.  
  43.     if user_exists:
  44.         raise HTTPException(status_code=409, detail="User Already Exists")
  45.  
  46.     user = models.User(**user.model_dump())
  47.     login = models.Login(id = user.id, email_address = user.email_address, password = user.password)
  48.  
  49.     db.add(user)
  50.     db.add(login)
  51.     db.commit()
  52.     db.refresh(user)
  53.     return user
  54.  
  55. @app.get("/login/")
  56. async def check_user_login(user: LoginBase, db: db_dependency, skip: int=0, limit: int=100):
  57.     if not validate_email(user.email_address):
  58.         raise HTTPException(status_code=400, detail="Not A Valid Email")
  59.    
  60.     if not validate_password(user.password):
  61.         raise HTTPException(status_code=400, detail="Not A Valid Password")
  62.    
  63.     query_email = db.query(models.Login.id).filter(models.Login.email_address == user.email_address)
  64.     query_pw = db.query(models.Login.id).filter(models.Login.password == user.password)
  65.  
  66.     if not query_email or query_pw:
  67.         raise HTTPException(status_code=400, detail="Email/Password Does Not Exist")
  68.    
  69.     user = db.query(models.User).offset(skip).limit(limit).filter(models.Login.id)
  70.     return user
Advertisement
Add Comment
Please, Sign In to add comment