from datetime import datetime, timedelta 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(model, data): table = eval(model) instance = table(**data.dict()) return instance 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): model = schema.replace('"', "") result = db.query(model).filter(model.email == data.email).delete() return result def fetch_user(data, db): result = db.query(Users).filter(Users.email == data.email).first() return result def fetch_otp(data: OTPVerify, db): result = db.query(Users).filter(Users.access_key == data.access_key).first() return result 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: 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: 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