Replace async SQL queries with standard ones

This commit is contained in:
coolneng 2020-08-01 19:04:35 +02:00
parent cfb1321e1e
commit 893d618a31
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
3 changed files with 41 additions and 53 deletions

View File

@ -1,13 +1,9 @@
from databases import Database
from sqlalchemy import MetaData, create_engine
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from constants import DB
metadata = MetaData()
engine = create_engine(DB, connect_args={"check_same_thread": False})
metadata.create_all(engine)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
database = Database(DB)

View File

@ -1,73 +1,67 @@
from fastapi import Depends
from datetime import datetime
from pydoc import locate
from app import app
from app.schemas import *
from database import database
from database import SessionLocal
from database.models import *
@app.on_event("startup")
async def startup():
await database.connect()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
async def instantiate_model(schema, data):
def instantiate_model(schema, data):
model = locate("database.models." + schema)
instance = model(**data)
return instance
async def insert_data(schema, data):
instance = instantiate_model(schema, data)
query = instance.insert()
await database.execute(query=query, values=data)
def insert_data(schema, data, db: Session = Depends(get_db)):
item = instantiate_model(schema=schema, data=data)
db.add(item)
db.commit()
# FIXME instance.id has to be replaced with the table's UID
async def delete_data(schema, data):
instance = instantiate_model(schema, data)
query = instance.delete().where(instance.id == data.id)
await database.execute(query=query)
# 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
async def fetch_user(data):
instance = instantiate_model(schema="Users", data=data)
query = instance.select().where(instance.email == data.email)
return await database.fetch_one(query=query)
def fetch_user(data, db: Session = Depends(get_db)):
result = db.query(Users).filter(Users.email == data.email).first()
return result
async def fetch_otp(data: OTPSchema):
instance = instantiate_model(schema="Users", data=data)
query = instance.select().where(instance.mobile == data.mobile)
return await database.fetch_one(query=query)
def fetch_otp(data: OTPSchema, db: Session = Depends(get_db)):
result = db.query(Users).filter(Users.access_key == data.access_key).first()
return result
async def save_otp(data: OTPSchema):
instance = instantiate_model(schema="Users", data=data)
query = instance.update().where(instance.mobile == data.mobile).values(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)
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}
)
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)
stored_otp = user.otp
if stored_otp == data.otp:
activate_account(mobile=data.mobile)
if user.otp == data.otp:
activate_account(data)
return True
return False

View File

@ -10,8 +10,6 @@ mkShell {
python38Packages.pydantic
python38Packages.email_validator
python38Packages.sqlalchemy
python38Packages.aiosqlite
python38Packages.databases
python38Packages.alembic
python38Packages.pytest
python38Packages.twilio