2020-08-01 19:04:35 +02:00
|
|
|
from fastapi import Depends
|
2020-04-28 20:49:13 +02:00
|
|
|
from datetime import datetime
|
2020-07-03 13:30:01 +02:00
|
|
|
from pydoc import locate
|
2020-06-05 01:09:55 +02:00
|
|
|
|
|
|
|
from app.schemas import *
|
2020-08-01 19:04:35 +02:00
|
|
|
from database import SessionLocal
|
|
|
|
from database.models import *
|
2020-06-05 01:09:55 +02:00
|
|
|
|
2020-04-15 00:29:59 +02:00
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
def get_db():
|
|
|
|
db = SessionLocal()
|
|
|
|
try:
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
2020-04-15 00:29:59 +02:00
|
|
|
|
2020-05-28 00:53:23 +02:00
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
def instantiate_model(schema, data):
|
2020-06-05 01:09:55 +02:00
|
|
|
model = locate("database.models." + schema)
|
2020-05-03 00:45:45 +02:00
|
|
|
instance = model(**data)
|
2020-05-28 00:53:23 +02:00
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
def insert_data(schema, data, db: Session = Depends(get_db)):
|
|
|
|
item = instantiate_model(schema=schema, data=data)
|
|
|
|
db.add(item)
|
|
|
|
db.commit()
|
2020-06-05 01:09:55 +02:00
|
|
|
|
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
# 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
|
2020-04-15 00:29:59 +02:00
|
|
|
|
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
def fetch_user(data, db: Session = Depends(get_db)):
|
|
|
|
result = db.query(Users).filter(Users.email == data.email).first()
|
|
|
|
return result
|
2020-04-15 00:29:59 +02:00
|
|
|
|
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
def fetch_otp(data: OTPSchema, db: Session = Depends(get_db)):
|
|
|
|
result = db.query(Users).filter(Users.access_key == data.access_key).first()
|
|
|
|
return result
|
2020-04-15 00:29:59 +02:00
|
|
|
|
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
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()
|
2020-04-19 21:30:41 +02:00
|
|
|
|
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
def activate_account(data: OTPSchema, db: Session = Depends(get_db)):
|
2020-04-19 21:30:41 +02:00
|
|
|
timestamp = datetime.now()
|
2020-08-01 19:04:35 +02:00
|
|
|
db.query(Users).filter(Users.access_key == data.access_key).update(
|
|
|
|
{Users.otp_valid_time: timestamp, Users.status: 1}
|
2020-04-19 21:30:41 +02:00
|
|
|
)
|
2020-08-01 19:04:35 +02:00
|
|
|
db.commit()
|
2020-04-19 21:30:41 +02:00
|
|
|
|
|
|
|
|
2020-08-01 19:04:35 +02:00
|
|
|
def verify_otp(data: OTPSchema):
|
2020-06-05 01:09:55 +02:00
|
|
|
user = fetch_otp(data=data)
|
2020-08-01 19:04:35 +02:00
|
|
|
if user.otp == data.otp:
|
|
|
|
activate_account(data)
|
2020-05-28 00:53:23 +02:00
|
|
|
return True
|
|
|
|
return False
|