Add user login and replace pipenv with Nix
This commit is contained in:
@@ -4,12 +4,25 @@ from app.models import *
|
||||
from app.schema import *
|
||||
from marshmallow import ValidationError
|
||||
from pydoc import locate
|
||||
from werkzeug.security import check_password_hash
|
||||
|
||||
|
||||
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)
|
||||
model = locate("app.models." + schema)
|
||||
instance = model(**data)
|
||||
return instance
|
||||
|
||||
|
||||
def insert_data(schema, data):
|
||||
validate_data(schema=schema, data=data)
|
||||
model = locate("app.models." + schema)
|
||||
instance = model(**data)
|
||||
instance = validate_json(schema, data)
|
||||
db.session.add(instance)
|
||||
db.session.commit()
|
||||
|
||||
@@ -24,23 +37,13 @@ def save_otp(mobile, otp):
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def validate_data(schema, data):
|
||||
schema_name = schema + "Schema"
|
||||
validation_schema = locate("app.schema." + schema_name)
|
||||
instance = validation_schema()
|
||||
try:
|
||||
instance.load(data)
|
||||
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_account(mobile):
|
||||
def activate_account(mobile):
|
||||
timestamp = datetime.now()
|
||||
db.session.query(table="Users").filter_by(mobile=mobile).update(
|
||||
dict(otp_valid_time=timestamp)
|
||||
@@ -51,6 +54,20 @@ def validate_account(mobile):
|
||||
def verify_otp(mobile, otp):
|
||||
stored_otp = fetch_stored_otp(mobile=mobile)
|
||||
if stored_otp == otp:
|
||||
validate_account(mobile=mobile)
|
||||
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"]):
|
||||
return True
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user