aprhodite/database.py

45 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
database.py SQLAlchemy + Flask-Migrate initialisation for SexyChat.
Import the `db` object everywhere you need ORM access.
Call `init_db(app)` inside the Flask app factory.
"""
import os
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
db = SQLAlchemy()
migrate = Migrate()
def init_db(app: "Flask") -> None: # noqa: F821
"""Bind SQLAlchemy and Migrate to the Flask application and create tables."""
db.init_app(app)
migrate.init_app(app, db)
with app.app_context():
# Import models so SQLAlchemy knows about them before create_all()
import models # noqa: F401
db.create_all()
_seed_ai_bot()
def _seed_ai_bot() -> None:
"""Ensure the SexyAI virtual user exists (used as AI-message recipient in DB)."""
from models import User
if User.query.filter_by(username="Violet").first():
return
import bcrypt, os as _os
bot = User(
username="Violet",
password_hash=bcrypt.hashpw(_os.urandom(32), bcrypt.gensalt()).decode(),
has_ai_access=True,
ai_messages_used=0,
)
db.session.add(bot)
db.session.commit()