Move database dependency from crud to routes
This commit is contained in:
parent
2d701ba473
commit
89d691f51d
|
@ -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.schemas import *
|
||||||
from app.twilio import send_otp
|
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 = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.post("/register")
|
@router.post("/register")
|
||||||
async def create_user(request: RegisterSchema):
|
async def create_user(request: UserCreate, db: Session = Depends(get_db)):
|
||||||
insert_data(schema="Users", data=request)
|
insert_data(model="Users", data=request, db=db)
|
||||||
send_otp(receiver=request.mobile)
|
send_otp(receiver=request.mobile)
|
||||||
return {"message": "User created, pending OTP verification"}
|
return {"message": "User created, pending OTP verification"}
|
||||||
|
|
||||||
|
|
||||||
# FIXME Use OAuth2 for verification
|
# FIXME Use OAuth2 for verification
|
||||||
@router.post("/login")
|
@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"}
|
return {"message": "Logged in successfully"}
|
||||||
# response.status_code = status.HTTP_400_BAD_REQUEST
|
# response.status_code = status.HTTP_400_BAD_REQUEST
|
||||||
# return {"message": "The email/password combination is not correct"}
|
# return {"message": "The email/password combination is not correct"}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/otpVerification")
|
@router.post("/otpVerification")
|
||||||
async def validate_otp(request: OTPSchema, response: Response):
|
async def validate_otp(
|
||||||
if verify_otp(data=request):
|
request: OTPVerify, response: Response, db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
if verify_otp(data=request, db=db):
|
||||||
return {"message": "The OTP has been verified successfully"}
|
return {"message": "The OTP has been verified successfully"}
|
||||||
raise HTTPException(status_code=400, detail="The OTP is not correct")
|
raise HTTPException(status_code=400, detail="The OTP is not correct")
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
from fastapi import Depends
|
from datetime import datetime, timedelta
|
||||||
from datetime import datetime
|
|
||||||
from pydoc import locate
|
|
||||||
|
|
||||||
from app.schemas import *
|
from app.schemas import *
|
||||||
from database import SessionLocal
|
from database import SessionLocal
|
||||||
|
@ -15,53 +13,57 @@ def get_db():
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
def instantiate_model(schema, data):
|
def instantiate_model(model, data):
|
||||||
model = locate("database.models." + schema)
|
table = eval(model)
|
||||||
instance = model(**data)
|
instance = table(**data.dict())
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
def insert_data(schema, data, db: Session = Depends(get_db)):
|
def insert_data(model, data, db):
|
||||||
item = instantiate_model(schema=schema, data=data)
|
item = instantiate_model(model=model, data=data)
|
||||||
db.add(item)
|
db.add(item)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
db.refresh(item)
|
||||||
|
|
||||||
|
|
||||||
# FIXME db.id has to be replaced with the table's UID
|
# 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('"', "")
|
model = schema.replace('"', "")
|
||||||
result = db.query(model).filter(model.email == data.email).delete()
|
result = db.query(model).filter(model.email == data.email).delete()
|
||||||
return result
|
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()
|
result = db.query(Users).filter(Users.email == data.email).first()
|
||||||
return result
|
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()
|
result = db.query(Users).filter(Users.access_key == data.access_key).first()
|
||||||
return result
|
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(
|
db.query(Users).filter(Users.access_key == data.access_key).update(
|
||||||
{Users.otp: data.otp}
|
{Users.otp: data.otp}
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
def activate_account(data: OTPSchema, db: Session = Depends(get_db)):
|
def activate_account(data: OTPVerify, db):
|
||||||
timestamp = datetime.now()
|
timestamp = datetime.now() + timedelta(minutes=10)
|
||||||
db.query(Users).filter(Users.access_key == data.access_key).update(
|
db.query(Users).filter(Users.access_key == data.access_key).update(
|
||||||
{Users.otp_valid_time: timestamp, Users.status: 1}
|
{Users.otp_valid_time: timestamp, Users.status: 1}
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
def verify_otp(data: OTPSchema):
|
def verify_otp(data: OTPVerify, db):
|
||||||
user = fetch_otp(data=data)
|
user = fetch_otp(data=data, db=db)
|
||||||
if user.otp == data.otp:
|
same_otp = user.otp == data.otp
|
||||||
activate_account(data)
|
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 True
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Reference in New Issue