odapi/database/crud.py

53 lines
1.2 KiB
Python

from datetime import datetime
from app import db
from app.models import *
from app.schema import *
from marshmallow import ValidationError
def insert_data(schema, data):
instance = validate_data(schema=schema, data=data)
db.session.add(instance)
db.session.commit()
def delete_data(data):
db.session.delete(data)
db.session.commit()
def save_otp(mobile, otp):
db.session.query(table="Users").filter_by(mobile=mobile).update(dict(otp=otp))
db.session.commit()
def validate_data(schema, data):
validation_schema = schema + "Schema()"
instance = validation_schema
try:
output = instance.load(data).data
return output
except ValidationError as err:
print(err.messages)
def fetch_stored_otp(mobile):
user = db.session.query(table="Users").filter_by(mobile=mobile)
otp = user.otp
return otp
def validate_otp(mobile):
timestamp = datetime.now()
db.session.query(table="Users").filter_by(mobile=mobile).update(
dict(otp_valid_time=timestamp)
)
def verify_otp(mobile, otp):
stored_otp = fetch_stored_otp(mobile=mobile)
if stored_otp == otp:
validate_otp(mobile=mobile)
return True
return False