Files
lego-instructions-manager/app/models/instruction.py

61 lines
2.2 KiB
Python

from datetime import datetime
from app import db
class Instruction(db.Model):
"""Model for instruction files (PDFs or images)."""
__tablename__ = 'instructions'
id = db.Column(db.Integer, primary_key=True)
set_id = db.Column(db.Integer, db.ForeignKey('sets.id'), nullable=False, index=True)
# File information
file_type = db.Column(db.String(10), nullable=False) # 'PDF' or 'IMAGE'
file_path = db.Column(db.String(500), nullable=False)
file_name = db.Column(db.String(200), nullable=False)
file_size = db.Column(db.Integer) # Size in bytes
thumbnail_path = db.Column(db.String(500)) # Thumbnail preview (especially for PDFs)
# For image sequences
page_number = db.Column(db.Integer, default=1)
# Metadata
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
uploaded_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
def __repr__(self):
return f'<Instruction {self.file_name} for Set {self.set_id}>'
def to_dict(self):
"""Convert instruction to dictionary."""
# Ensure forward slashes for web URLs
clean_path = self.file_path.replace('\\', '/')
thumbnail_clean = self.thumbnail_path.replace('\\', '/') if self.thumbnail_path else None
return {
'id': self.id,
'set_id': self.set_id,
'file_type': self.file_type,
'file_name': self.file_name,
'file_size': self.file_size,
'page_number': self.page_number,
'file_url': f'/static/uploads/{clean_path}',
'thumbnail_url': f'/static/uploads/{thumbnail_clean}' if thumbnail_clean else None,
'uploaded_at': self.uploaded_at.isoformat()
}
@property
def file_size_mb(self):
"""Return file size in MB."""
if self.file_size:
return round(self.file_size / (1024 * 1024), 2)
return 0
@staticmethod
def allowed_file(filename):
"""Check if file extension is allowed."""
from flask import current_app
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in current_app.config['ALLOWED_EXTENSIONS']