131 lines
3.5 KiB
Python
131 lines
3.5 KiB
Python
"""
|
|
Database Models for the Imageboard Application
|
|
|
|
This module defines the SQLAlchemy models for the imageboard application:
|
|
- Board: Represents discussion boards (e.g., /g/, /v/, /b/)
|
|
- Thread: Topics within boards
|
|
- Post: Individual messages/replies with optional file attachments
|
|
"""
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from datetime import datetime
|
|
|
|
# Initialize the database instance
|
|
db = SQLAlchemy()
|
|
|
|
|
|
class Board(db.Model):
|
|
"""
|
|
Board Model
|
|
|
|
Represents discussion boards (e.g., /g/, /v/, /b/)
|
|
"""
|
|
|
|
__tablename__ = 'boards'
|
|
|
|
# Primary key
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
# Board attributes
|
|
name = db.Column(
|
|
db.String(50),
|
|
unique=True,
|
|
nullable=False,
|
|
index=True) # e.g., 'g', 'v', 'b'
|
|
# e.g., 'Technology', 'Video Games', 'Random'
|
|
title = db.Column(db.String(100), nullable=False)
|
|
description = db.Column(db.Text)
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow, index=True)
|
|
|
|
def __repr__(self):
|
|
"""String representation of the Board object."""
|
|
return f'<Board {self.name}>'
|
|
|
|
|
|
class Thread(db.Model):
|
|
"""
|
|
Thread Model
|
|
|
|
Represents topics within boards
|
|
"""
|
|
|
|
__tablename__ = 'threads'
|
|
|
|
# Primary key
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
# Foreign key linking to Board
|
|
board_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey('boards.id'),
|
|
nullable=False,
|
|
index=True)
|
|
|
|
# Thread attributes
|
|
# Optional subject/title
|
|
subject = db.Column(db.String(200))
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow, index=True)
|
|
bumped_at = db.Column(
|
|
db.DateTime,
|
|
default=datetime.utcnow,
|
|
index=True) # Last post time
|
|
is_locked = db.Column(db.Boolean, default=False, index=True)
|
|
is_deleted = db.Column(db.Boolean, default=False, index=True)
|
|
|
|
# Relationship
|
|
board = db.relationship('Board', backref=db.backref('threads', lazy=True))
|
|
|
|
def __repr__(self):
|
|
"""String representation of the Thread object."""
|
|
return f'<Thread {self.id} on Board {self.board.name}>'
|
|
|
|
|
|
class Post(db.Model):
|
|
"""
|
|
Post Model
|
|
|
|
Represents individual messages/replies with optional file attachments
|
|
"""
|
|
|
|
__tablename__ = 'posts'
|
|
|
|
# Primary key
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
# Foreign key linking to Thread
|
|
thread_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey('threads.id'),
|
|
nullable=False,
|
|
index=True)
|
|
|
|
# Post content and metadata
|
|
content = db.Column(db.Text, nullable=False)
|
|
author_name = db.Column(
|
|
db.String(100),
|
|
index=True) # Anonymous name
|
|
# Generated from password
|
|
tripcode = db.Column(db.String(20))
|
|
ip_address = db.Column(
|
|
db.String(45),
|
|
index=True) # Store anonymized IP
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow, index=True)
|
|
is_deleted = db.Column(db.Boolean, default=False, index=True)
|
|
|
|
# File information (optional)
|
|
# Original filename
|
|
filename = db.Column(db.String(200))
|
|
# Path to stored file
|
|
file_path = db.Column(db.String(300))
|
|
# Size in bytes
|
|
file_size = db.Column(db.Integer)
|
|
# SHA-256 hash for duplicate detection
|
|
file_hash = db.Column(db.String(64), index=True)
|
|
|
|
# Relationships
|
|
thread = db.relationship('Thread', backref=db.backref('posts', lazy=True))
|
|
|
|
def __repr__(self):
|
|
"""String representation of the Post object."""
|
|
return f'<Post {self.id} in Thread {self.thread_id}>'
|