68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
from fastapi import Depends
|
|
from datetime import datetime
|
|
from pydoc import locate
|
|
|
|
from app.schemas import *
|
|
from database import SessionLocal
|
|
from database.models import *
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def instantiate_model(schema, data):
|
|
model = locate("database.models." + schema)
|
|
instance = model(**data)
|
|
return instance
|
|
|
|
|
|
def insert_data(schema, data, db: Session = Depends(get_db)):
|
|
item = instantiate_model(schema=schema, data=data)
|
|
db.add(item)
|
|
db.commit()
|
|
|
|
|
|
# FIXME db.id has to be replaced with the table's UID
|
|
def delete_data(schema, data, db: Session = Depends(get_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)):
|
|
result = db.query(Users).filter(Users.email == data.email).first()
|
|
return result
|
|
|
|
|
|
def fetch_otp(data: OTPSchema, db: Session = Depends(get_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)):
|
|
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()
|
|
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)
|
|
return True
|
|
return False
|