From 893d618a31680e5469bb19d30e3e045e04ef4d5e Mon Sep 17 00:00:00 2001 From: coolneng Date: Sat, 1 Aug 2020 19:04:35 +0200 Subject: [PATCH] Replace async SQL queries with standard ones --- database/__init__.py | 6 +--- database/crud.py | 86 +++++++++++++++++++++----------------------- shell.nix | 2 -- 3 files changed, 41 insertions(+), 53 deletions(-) diff --git a/database/__init__.py b/database/__init__.py index baf448a..5b860bf 100644 --- a/database/__init__.py +++ b/database/__init__.py @@ -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) diff --git a/database/crud.py b/database/crud.py index e264bcb..e9c50b0 100644 --- a/database/crud.py +++ b/database/crud.py @@ -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 diff --git a/shell.nix b/shell.nix index 5b25c02..87b176a 100644 --- a/shell.nix +++ b/shell.nix @@ -10,8 +10,6 @@ mkShell { python38Packages.pydantic python38Packages.email_validator python38Packages.sqlalchemy - python38Packages.aiosqlite - python38Packages.databases python38Packages.alembic python38Packages.pytest python38Packages.twilio