Replace async SQL queries with standard ones
This commit is contained in:
parent
cfb1321e1e
commit
893d618a31
|
@ -1,13 +1,9 @@
|
||||||
from databases import Database
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy import MetaData, create_engine
|
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
from constants import DB
|
from constants import DB
|
||||||
|
|
||||||
metadata = MetaData()
|
|
||||||
engine = create_engine(DB, connect_args={"check_same_thread": False})
|
engine = create_engine(DB, connect_args={"check_same_thread": False})
|
||||||
metadata.create_all(engine)
|
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
database = Database(DB)
|
|
||||||
|
|
|
@ -1,73 +1,67 @@
|
||||||
|
from fastapi import Depends
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pydoc import locate
|
from pydoc import locate
|
||||||
|
|
||||||
from app import app
|
|
||||||
from app.schemas import *
|
from app.schemas import *
|
||||||
from database import database
|
from database import SessionLocal
|
||||||
|
from database.models import *
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
def get_db():
|
||||||
async def startup():
|
db = SessionLocal()
|
||||||
await database.connect()
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("shutdown")
|
def instantiate_model(schema, data):
|
||||||
async def shutdown():
|
|
||||||
await database.disconnect()
|
|
||||||
|
|
||||||
|
|
||||||
async def instantiate_model(schema, data):
|
|
||||||
model = locate("database.models." + schema)
|
model = locate("database.models." + schema)
|
||||||
instance = model(**data)
|
instance = model(**data)
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
async def insert_data(schema, data):
|
def insert_data(schema, data, db: Session = Depends(get_db)):
|
||||||
instance = instantiate_model(schema, data)
|
item = instantiate_model(schema=schema, data=data)
|
||||||
query = instance.insert()
|
db.add(item)
|
||||||
await database.execute(query=query, values=data)
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
# FIXME instance.id has to be replaced with the table's UID
|
# FIXME db.id has to be replaced with the table's UID
|
||||||
async def delete_data(schema, data):
|
def delete_data(schema, data, db: Session = Depends(get_db)):
|
||||||
instance = instantiate_model(schema, data)
|
model = schema.replace('"', "")
|
||||||
query = instance.delete().where(instance.id == data.id)
|
result = db.query(model).filter(model.email == data.email).delete()
|
||||||
await database.execute(query=query)
|
return result
|
||||||
|
|
||||||
|
|
||||||
async def fetch_user(data):
|
def fetch_user(data, db: Session = Depends(get_db)):
|
||||||
instance = instantiate_model(schema="Users", data=data)
|
result = db.query(Users).filter(Users.email == data.email).first()
|
||||||
query = instance.select().where(instance.email == data.email)
|
return result
|
||||||
return await database.fetch_one(query=query)
|
|
||||||
|
|
||||||
|
|
||||||
async def fetch_otp(data: OTPSchema):
|
def fetch_otp(data: OTPSchema, db: Session = Depends(get_db)):
|
||||||
instance = instantiate_model(schema="Users", data=data)
|
result = db.query(Users).filter(Users.access_key == data.access_key).first()
|
||||||
query = instance.select().where(instance.mobile == data.mobile)
|
return result
|
||||||
return await database.fetch_one(query=query)
|
|
||||||
|
|
||||||
|
|
||||||
async def save_otp(data: OTPSchema):
|
def save_otp(data: OTPSchema, db: Session = Depends(get_db)):
|
||||||
instance = instantiate_model(schema="Users", data=data)
|
db.query(Users).filter(Users.access_key == data.access_key).update(
|
||||||
query = instance.update().where(instance.mobile == data.mobile).values(otp=data.otp)
|
{Users.otp: data.otp}
|
||||||
await database.execute(query=query)
|
|
||||||
|
|
||||||
|
|
||||||
async def activate_account(data: OTPSchema):
|
|
||||||
timestamp = datetime.now()
|
|
||||||
instance = instantiate_model(schema="Users", data=data)
|
|
||||||
query = (
|
|
||||||
instance.update()
|
|
||||||
.where(instance.mobile == data.mobile)
|
|
||||||
.values(mobile=instance.data, otp_valid_time=timestamp, status=1)
|
|
||||||
)
|
)
|
||||||
await database.execute(query=query)
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
async def verify_otp(data: OTPSchema):
|
def activate_account(data: OTPSchema, db: Session = Depends(get_db)):
|
||||||
|
timestamp = datetime.now()
|
||||||
|
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: OTPSchema):
|
||||||
user = fetch_otp(data=data)
|
user = fetch_otp(data=data)
|
||||||
stored_otp = user.otp
|
if user.otp == data.otp:
|
||||||
if stored_otp == data.otp:
|
activate_account(data)
|
||||||
activate_account(mobile=data.mobile)
|
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -10,8 +10,6 @@ mkShell {
|
||||||
python38Packages.pydantic
|
python38Packages.pydantic
|
||||||
python38Packages.email_validator
|
python38Packages.email_validator
|
||||||
python38Packages.sqlalchemy
|
python38Packages.sqlalchemy
|
||||||
python38Packages.aiosqlite
|
|
||||||
python38Packages.databases
|
|
||||||
python38Packages.alembic
|
python38Packages.alembic
|
||||||
python38Packages.pytest
|
python38Packages.pytest
|
||||||
python38Packages.twilio
|
python38Packages.twilio
|
||||||
|
|
Loading…
Reference in New Issue