add aurweb.db.session

+ Added Session class and global session object to aurweb.db,
  these are sessions created by sqlalchemy ORM's sessionmaker
  and will allow us to use declarative/imperative models.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-03-29 15:20:23 -07:00
parent 7c65604dad
commit 4238a9fc68
6 changed files with 260 additions and 32 deletions

View file

@ -7,30 +7,36 @@ from starlette.middleware.sessions import SessionMiddleware
import aurweb.config
from aurweb.db import get_engine
from aurweb.routers import html, sso
routes = set()
# Setup the FastAPI app.
app = FastAPI()
app.mount("/static/css",
StaticFiles(directory="web/html/css"),
name="static_css")
app.mount("/static/js",
StaticFiles(directory="web/html/js"),
name="static_js")
app.mount("/static/images",
StaticFiles(directory="web/html/images"),
name="static_images")
session_secret = aurweb.config.get("fastapi", "session_secret")
if not session_secret:
raise Exception("[fastapi] session_secret must not be empty")
app.add_middleware(SessionMiddleware, secret_key=session_secret)
@app.on_event("startup")
async def app_startup():
session_secret = aurweb.config.get("fastapi", "session_secret")
if not session_secret:
raise Exception("[fastapi] session_secret must not be empty")
app.include_router(sso.router)
app.include_router(html.router)
app.mount("/static/css",
StaticFiles(directory="web/html/css"),
name="static_css")
app.mount("/static/js",
StaticFiles(directory="web/html/js"),
name="static_js")
app.mount("/static/images",
StaticFiles(directory="web/html/images"),
name="static_images")
app.add_middleware(SessionMiddleware, secret_key=session_secret)
app.include_router(sso.router)
app.include_router(html.router)
get_engine()
# NOTE: Always keep this dictionary updated with all routes
# that the application contains. We use this to check for

View file

@ -25,6 +25,13 @@ def _get_parser():
return _parser
def rehash():
""" Globally rehash the configuration parser. """
global _parser
_parser = None
_get_parser()
def get(section, option):
return _get_parser().get(section, option)

View file

@ -1,19 +1,15 @@
import math
try:
import mysql.connector
except ImportError:
pass
try:
import sqlite3
except ImportError:
pass
import aurweb.config
engine = None # See get_engine
# ORM Session class.
Session = None
# Global ORM Session object.
session = None
def get_sqlalchemy_url():
"""
@ -49,14 +45,15 @@ def get_engine():
`engine` global variable for the next calls.
"""
from sqlalchemy import create_engine
global engine
from sqlalchemy.orm import sessionmaker
global engine, session, Session
if engine is None:
connect_args = dict()
if aurweb.config.get("database", "backend") == "sqlite":
# check_same_thread is for a SQLite technicality
# https://fastapi.tiangolo.com/tutorial/sql-databases/#note
connect_args["check_same_thread"] = False
engine = create_engine(get_sqlalchemy_url(), connect_args=connect_args)
engine = create_engine(get_sqlalchemy_url(),
# check_same_thread is for a SQLite technicality
# https://fastapi.tiangolo.com/tutorial/sql-databases/#note
connect_args={"check_same_thread": False})
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
session = Session()
@ -82,6 +79,7 @@ class Connection:
aur_db_backend = aurweb.config.get('database', 'backend')
if aur_db_backend == 'mysql':
import mysql.connector
aur_db_host = aurweb.config.get('database', 'host')
aur_db_name = aurweb.config.get('database', 'name')
aur_db_user = aurweb.config.get('database', 'user')
@ -95,6 +93,7 @@ class Connection:
buffered=True)
self._paramstyle = mysql.connector.paramstyle
elif aur_db_backend == 'sqlite':
import sqlite3
aur_db_name = aurweb.config.get('database', 'name')
self._conn = sqlite3.connect(aur_db_name)
self._conn.create_function("POWER", 2, math.pow)