lotr-sut/sut/backend/models/user.py
Fellowship Scholar f6a5823439 init commit
2026-03-29 20:07:56 +00:00

42 lines
1.6 KiB
Python

"""User model for authentication."""
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from typing import Dict, Any
# Shared db instance for all models
db = SQLAlchemy()
class User(db.Model):
"""User model for authentication."""
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False, index=True)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(255), nullable=False)
role = db.Column(db.String(50), nullable=False) # Fellowship member name
gold = db.Column(db.Integer, nullable=False, default=500)
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
def set_password(self, password: str) -> None:
"""Hash and set password."""
self.password_hash = generate_password_hash(password, method='pbkdf2:sha256')
def check_password(self, password: str) -> bool:
"""Check if provided password matches hash."""
return check_password_hash(self.password_hash, password)
def to_dict(self) -> Dict[str, Any]:
"""Convert user to dictionary."""
return {
'id': self.id,
'username': self.username,
'email': self.email,
'role': self.role,
'gold': self.gold,
'created_at': self.created_at.isoformat() if self.created_at else None
}
def __repr__(self) -> str:
return f'<User {self.username}>'