Initial commit for deployment
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"""
|
||||
Chat models for the application.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from app.database.db import db
|
||||
|
||||
class Chat(db.Model):
|
||||
"""Chat model representing a chat session."""
|
||||
|
||||
__tablename__ = 'chats'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
title = db.Column(db.String(100), nullable=True)
|
||||
is_team_chat = db.Column(db.Boolean, default=False)
|
||||
model_name = db.Column(db.String(50), nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
# Foreign keys
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||
|
||||
# Relationships
|
||||
messages = db.relationship('Message', backref='chat', lazy='dynamic', cascade='all, delete-orphan')
|
||||
team_members = db.relationship('TeamChatMember', backref='chat', lazy='dynamic', cascade='all, delete-orphan')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Chat {self.id}: {self.title or "Untitled"}>'
|
||||
|
||||
|
||||
class Message(db.Model):
|
||||
"""Message model representing a single message in a chat."""
|
||||
|
||||
__tablename__ = 'messages'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
content = db.Column(db.Text, nullable=False)
|
||||
is_user_message = db.Column(db.Boolean, default=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
# Foreign keys
|
||||
chat_id = db.Column(db.Integer, db.ForeignKey('chats.id'), nullable=False)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Message {self.id}: {self.content[:20]}...>'
|
||||
|
||||
|
||||
class TeamChatMember(db.Model):
|
||||
"""Model representing a member of a team chat."""
|
||||
|
||||
__tablename__ = 'team_chat_members'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
joined_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
# Foreign keys
|
||||
chat_id = db.Column(db.Integer, db.ForeignKey('chats.id'), nullable=False)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||
|
||||
# Ensure a user can only be added to a team chat once
|
||||
__table_args__ = (
|
||||
db.UniqueConstraint('chat_id', 'user_id', name='uq_team_chat_member'),
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<TeamChatMember chat_id={self.chat_id}, user_id={self.user_id}>'
|
||||
@@ -0,0 +1,59 @@
|
||||
"""
|
||||
Document models for the application.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
import json
|
||||
from app.database.db import db
|
||||
|
||||
class Document(db.Model):
|
||||
"""Document model representing a document in the library."""
|
||||
|
||||
__tablename__ = 'documents'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
title = db.Column(db.String(255), nullable=False)
|
||||
description = db.Column(db.Text, nullable=True)
|
||||
file_path = db.Column(db.String(255), nullable=True)
|
||||
content_type = db.Column(db.String(50), nullable=False)
|
||||
status = db.Column(db.String(20), default='pending') # pending, processing, completed, error
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
# Foreign keys
|
||||
uploaded_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||
|
||||
# Relationships
|
||||
chunks = db.relationship('DocumentChunk', backref='document', lazy='dynamic', cascade='all, delete-orphan')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Document {self.id}: {self.title}>'
|
||||
|
||||
|
||||
class DocumentChunk(db.Model):
|
||||
"""Model representing a chunk of a document for embedding."""
|
||||
|
||||
__tablename__ = 'document_chunks'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
content = db.Column(db.Text, nullable=False)
|
||||
chunk_index = db.Column(db.Integer, nullable=False)
|
||||
embedding_id = db.Column(db.String(100), nullable=True) # ID in Pinecone
|
||||
meta_data = db.Column(db.Text, nullable=True) # JSON string of metadata
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
# Foreign keys
|
||||
document_id = db.Column(db.Integer, db.ForeignKey('documents.id'), nullable=False)
|
||||
|
||||
def set_metadata(self, metadata_dict):
|
||||
"""Set metadata as JSON string."""
|
||||
self.meta_data = json.dumps(metadata_dict)
|
||||
|
||||
def get_metadata(self):
|
||||
"""Get metadata as dictionary."""
|
||||
if self.meta_data:
|
||||
return json.loads(self.meta_data)
|
||||
return {}
|
||||
|
||||
def __repr__(self):
|
||||
return f'<DocumentChunk {self.id}: doc_id={self.document_id}, index={self.chunk_index}>'
|
||||
@@ -0,0 +1,24 @@
|
||||
"""
|
||||
User model for the application.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from app.database.db import db
|
||||
|
||||
class User(db.Model):
|
||||
"""User model representing application users."""
|
||||
|
||||
__tablename__ = 'users'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(64), unique=True, nullable=False, index=True)
|
||||
email = db.Column(db.String(120), unique=True, nullable=False, index=True)
|
||||
password_hash = db.Column(db.String(128), nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
chats = db.relationship('Chat', backref='user', lazy='dynamic')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<User {self.username}>'
|
||||
Reference in New Issue
Block a user