2020-04-28 20:49:13 +02:00
|
|
|
from datetime import datetime
|
2020-04-15 00:29:59 +02:00
|
|
|
from app import db
|
2020-04-28 20:49:13 +02:00
|
|
|
from app.models import *
|
|
|
|
from app.schema import *
|
2020-04-15 00:29:59 +02:00
|
|
|
from marshmallow import ValidationError
|
2020-05-03 00:45:45 +02:00
|
|
|
from pydoc import locate
|
2020-05-28 00:53:23 +02:00
|
|
|
from werkzeug.security import check_password_hash
|
2020-04-15 00:29:59 +02:00
|
|
|
|
|
|
|
|
2020-05-28 00:53:23 +02:00
|
|
|
def validate_schema(schema, data):
|
|
|
|
schema_name = schema + "Schema"
|
|
|
|
validation_schema = locate("app.schema." + schema_name)
|
|
|
|
instance = validation_schema()
|
|
|
|
instance.load(data)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_json(schema, data):
|
|
|
|
validate_schema(schema=schema, data=data)
|
2020-05-03 00:45:45 +02:00
|
|
|
model = locate("app.models." + schema)
|
|
|
|
instance = model(**data)
|
2020-05-28 00:53:23 +02:00
|
|
|
return instance
|
|
|
|
|
|
|
|
|
|
|
|
def insert_data(schema, data):
|
|
|
|
instance = validate_json(schema, data)
|
2020-04-15 00:29:59 +02:00
|
|
|
db.session.add(instance)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
2020-04-19 21:30:41 +02:00
|
|
|
def delete_data(data):
|
2020-04-15 00:29:59 +02:00
|
|
|
db.session.delete(data)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
2020-04-19 21:30:41 +02:00
|
|
|
def save_otp(mobile, otp):
|
|
|
|
db.session.query(table="Users").filter_by(mobile=mobile).update(dict(otp=otp))
|
2020-04-15 00:29:59 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
2020-04-19 21:30:41 +02:00
|
|
|
def fetch_stored_otp(mobile):
|
|
|
|
user = db.session.query(table="Users").filter_by(mobile=mobile)
|
|
|
|
otp = user.otp
|
|
|
|
return otp
|
|
|
|
|
|
|
|
|
2020-05-28 00:53:23 +02:00
|
|
|
def activate_account(mobile):
|
2020-04-19 21:30:41 +02:00
|
|
|
timestamp = datetime.now()
|
|
|
|
db.session.query(table="Users").filter_by(mobile=mobile).update(
|
|
|
|
dict(otp_valid_time=timestamp)
|
|
|
|
)
|
2020-05-03 00:45:45 +02:00
|
|
|
db.session.query(table="Users").filter_by(mobile=mobile).update(dict(status=1))
|
2020-04-19 21:30:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def verify_otp(mobile, otp):
|
|
|
|
stored_otp = fetch_stored_otp(mobile=mobile)
|
|
|
|
if stored_otp == otp:
|
2020-05-28 00:53:23 +02:00
|
|
|
activate_account(mobile=mobile)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_user(data):
|
|
|
|
user = db.session.query(table="Users").filter_by(email=data["email"])
|
|
|
|
email = user.email
|
|
|
|
password = user.password
|
|
|
|
return email, password
|
|
|
|
|
|
|
|
|
|
|
|
def verify_login(data):
|
|
|
|
user, password = fetch_user(data)
|
|
|
|
if user == data["email"] and check_password_hash(password, data["password"]):
|
2020-04-19 21:30:41 +02:00
|
|
|
return True
|
|
|
|
return False
|