From 89d691f51d223a931b5817a01cacba7002d84391 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 11 Sep 2020 00:00:48 +0200 Subject: [PATCH] Move database dependency from crud to routes --- app/routes.py | 17 ++++++++++------- database/crud.py | 38 ++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/app/routes.py b/app/routes.py index b78139a..eeed3d8 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,29 +1,32 @@ -from fastapi import APIRouter, HTTPException, Response +from fastapi import APIRouter, Depends, HTTPException, Response +from sqlalchemy.orm import Session from app.schemas import * from app.twilio import send_otp -from database.crud import insert_data, verify_otp +from database.crud import get_db, insert_data, verify_otp router = APIRouter() @router.post("/register") -async def create_user(request: RegisterSchema): - insert_data(schema="Users", data=request) +async def create_user(request: UserCreate, db: Session = Depends(get_db)): + insert_data(model="Users", data=request, db=db) send_otp(receiver=request.mobile) return {"message": "User created, pending OTP verification"} # FIXME Use OAuth2 for verification @router.post("/login") -async def log_in(request: LoginSchema, response: Response): +async def log_in(request: UserLogin, response: Response, db: Session = Depends(get_db)): return {"message": "Logged in successfully"} # response.status_code = status.HTTP_400_BAD_REQUEST # return {"message": "The email/password combination is not correct"} @router.post("/otpVerification") -async def validate_otp(request: OTPSchema, response: Response): - if verify_otp(data=request): +async def validate_otp( + request: OTPVerify, response: Response, db: Session = Depends(get_db) +): + if verify_otp(data=request, db=db): return {"message": "The OTP has been verified successfully"} raise HTTPException(status_code=400, detail="The OTP is not correct") diff --git a/database/crud.py b/database/crud.py index e9c50b0..673db36 100644 --- a/database/crud.py +++ b/database/crud.py @@ -1,6 +1,4 @@ -from fastapi import Depends -from datetime import datetime -from pydoc import locate +from datetime import datetime, timedelta from app.schemas import * from database import SessionLocal @@ -15,53 +13,57 @@ def get_db(): db.close() -def instantiate_model(schema, data): - model = locate("database.models." + schema) - instance = model(**data) +def instantiate_model(model, data): + table = eval(model) + instance = table(**data.dict()) return instance -def insert_data(schema, data, db: Session = Depends(get_db)): - item = instantiate_model(schema=schema, data=data) +def insert_data(model, data, db): + item = instantiate_model(model=model, data=data) db.add(item) db.commit() + db.refresh(item) # FIXME db.id has to be replaced with the table's UID -def delete_data(schema, data, db: Session = Depends(get_db)): +def delete_data(schema, data, db): model = schema.replace('"', "") result = db.query(model).filter(model.email == data.email).delete() return result -def fetch_user(data, db: Session = Depends(get_db)): +def fetch_user(data, db): result = db.query(Users).filter(Users.email == data.email).first() return result -def fetch_otp(data: OTPSchema, db: Session = Depends(get_db)): +def fetch_otp(data: OTPVerify, db): result = db.query(Users).filter(Users.access_key == data.access_key).first() return result -def save_otp(data: OTPSchema, db: Session = Depends(get_db)): +def save_otp(data: OTPVerify, db): db.query(Users).filter(Users.access_key == data.access_key).update( {Users.otp: data.otp} ) db.commit() -def activate_account(data: OTPSchema, db: Session = Depends(get_db)): - timestamp = datetime.now() +def activate_account(data: OTPVerify, db): + timestamp = datetime.now() + timedelta(minutes=10) db.query(Users).filter(Users.access_key == data.access_key).update( {Users.otp_valid_time: timestamp, Users.status: 1} ) db.commit() -def verify_otp(data: OTPSchema): - user = fetch_otp(data=data) - if user.otp == data.otp: - activate_account(data) +def verify_otp(data: OTPVerify, db): + user = fetch_otp(data=data, db=db) + same_otp = user.otp == data.otp + valid_time = datetime.now() <= user.otp_valid_time + valid_otp = same_otp and valid_time + if valid_otp: + activate_account(data=data, db=db) return True return False