Spaces:
Paused
Paused
| """ | |
| Database models for Life Coach Web Application | |
| """ | |
| from datetime import datetime | |
| from flask_login import UserMixin | |
| from flask_sqlalchemy import SQLAlchemy | |
| from werkzeug.security import generate_password_hash, check_password_hash | |
| # This will be set by app.py | |
| db = SQLAlchemy() | |
| class User(UserMixin, db.Model): | |
| """User model for authentication and profile.""" | |
| __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, index=True) | |
| password_hash = db.Column(db.String(255), nullable=False) | |
| created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) | |
| # Relationship with conversations | |
| conversations = db.relationship('Conversation', backref='user', lazy=True, cascade='all, delete-orphan') | |
| def set_password(self, password): | |
| """Hash and set the user's password.""" | |
| self.password_hash = generate_password_hash(password) | |
| def check_password(self, password): | |
| """Verify the user's password.""" | |
| return check_password_hash(self.password_hash, password) | |
| def __repr__(self): | |
| return f'<User {self.username}>' | |
| class Conversation(db.Model): | |
| """Conversation model - represents a chat session.""" | |
| __tablename__ = 'conversations' | |
| id = db.Column(db.Integer, primary_key=True) | |
| user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, index=True) | |
| title = db.Column(db.String(200), default='New Conversation') | |
| created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) | |
| updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) | |
| # Relationship with messages | |
| messages = db.relationship('Message', backref='conversation', lazy=True, cascade='all, delete-orphan', order_by='Message.timestamp') | |
| def get_message_history(self): | |
| """Get conversation history formatted for the model.""" | |
| return [ | |
| { | |
| 'role': msg.role, | |
| 'content': msg.content | |
| } | |
| for msg in self.messages | |
| ] | |
| def __repr__(self): | |
| return f'<Conversation {self.id}: {self.title}>' | |
| class Message(db.Model): | |
| """Message model - individual messages in a conversation.""" | |
| __tablename__ = 'messages' | |
| id = db.Column(db.Integer, primary_key=True) | |
| conversation_id = db.Column(db.Integer, db.ForeignKey('conversations.id'), nullable=False, index=True) | |
| role = db.Column(db.String(20), nullable=False) # 'user' or 'assistant' | |
| content = db.Column(db.Text, nullable=False) | |
| timestamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) | |
| def to_dict(self): | |
| """Convert message to dictionary for JSON serialization.""" | |
| return { | |
| 'id': self.id, | |
| 'role': self.role, | |
| 'content': self.content, | |
| 'timestamp': self.timestamp.isoformat() | |
| } | |
| def __repr__(self): | |
| return f'<Message {self.id}: {self.role}>' | |