v0.2 planning and ux improvements
This commit is contained in:
+20
-19
@@ -23,20 +23,24 @@ def get_db() -> sqlite3.Connection:
|
||||
|
||||
|
||||
def close_db(_error=None) -> None:
|
||||
db = g.pop("db", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
database = g.pop("db", None)
|
||||
if database is not None:
|
||||
database.close()
|
||||
|
||||
|
||||
def apply_schema(database: sqlite3.Connection) -> None:
|
||||
schema_path = Path(__file__).with_name("schema.sql")
|
||||
database.executescript(schema_path.read_text(encoding="utf-8"))
|
||||
sync_dayparts(database)
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
database = get_db()
|
||||
schema_path = Path(__file__).with_name("schema.sql")
|
||||
database.executescript(schema_path.read_text(encoding="utf-8"))
|
||||
seed_dayparts(database)
|
||||
apply_schema(database)
|
||||
database.commit()
|
||||
|
||||
|
||||
def seed_dayparts(database: sqlite3.Connection) -> None:
|
||||
def sync_dayparts(database: sqlite3.Connection) -> None:
|
||||
for entry in DAYPARTS:
|
||||
database.execute(
|
||||
"""
|
||||
@@ -45,22 +49,19 @@ def seed_dayparts(database: sqlite3.Connection) -> None:
|
||||
""",
|
||||
(entry["slug"], entry["name"], entry["sort_order"]),
|
||||
)
|
||||
database.execute(
|
||||
"""
|
||||
UPDATE dayparts
|
||||
SET name = ?, sort_order = ?
|
||||
WHERE slug = ?
|
||||
""",
|
||||
(entry["name"], entry["sort_order"], entry["slug"]),
|
||||
)
|
||||
|
||||
|
||||
def init_db_if_needed(app: Flask) -> None:
|
||||
db_path = Path(app.config["DATABASE_PATH"])
|
||||
needs_init = not db_path.exists()
|
||||
with app.app_context():
|
||||
if needs_init:
|
||||
init_db()
|
||||
return
|
||||
|
||||
database = get_db()
|
||||
table = database.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'users'"
|
||||
).fetchone()
|
||||
if table is None:
|
||||
init_db()
|
||||
init_db()
|
||||
|
||||
|
||||
def user_count() -> int:
|
||||
|
||||
Reference in New Issue
Block a user