SimpleAES / database.py
SFM2001's picture
maybe it works
14bc5c6
from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
from sql_model import Base
DATABASE_URL = "sqlite:///./instance/users.db"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
from passlib.context import CryptContext
# Add password hashing context
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True)
nickname = Column(String, unique=True)
hashed_password = Column(String) # Changed from password to hashed_password
def __init__(self, email: str, nickname: str, password: str):
self.email = email
self.nickname = nickname
self.hashed_password = AuthService.get_password_hash(password)
def verify_password(self, plain_password: str):
return pwd_context.verify(plain_password, self.hashed_password)
class History(Base):
__tablename__ = "history"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id"))
topic = Column(String)
essay = Column(String)
score_tr = Column(Integer)
score_cc = Column(Integer)
score_lr = Column(Integer)
score_gra = Column(Integer)
created_at = Column(DateTime, default=datetime.utcnow)
# Add other fields as needed