Add models and utils with type hints and optimizations
Models (6 + 1 association table): - User: Authentication with bcrypt, admin role check, last_login tracking - Player: Digital signage devices with auth codes, status tracking, online detection - Group: Player/content organization with statistics properties - Content: Media files with type detection, file size helpers, position ordering - ServerLog: Audit trail with class methods for logging levels - PlayerFeedback: Player status updates with error tracking - group_content: Many-to-many association table for groups and content Model improvements: - Added type hints to all methods and properties - Added database indexes on frequently queried columns (username, auth_code, group_id, player_id, position, level, timestamp, status) - Added comprehensive docstrings - Added helper properties (is_online, is_admin, file_size_mb, etc.) - Added relationship back_populates for bidirectional navigation - Added timestamps (created_at, updated_at, last_seen, uploaded_at) Utils (4 modules): - logger.py: Logging utility with level-based functions (info, warning, error) - uploads.py: File upload handling with progress tracking, video optimization - group_player_management.py: Player/group status tracking and bulk operations - pptx_converter.py: PowerPoint to PDF conversion using LibreOffice Utils improvements: - Full type hints on all functions - Comprehensive error handling - Progress tracking for long-running operations - Video optimization (H.264, 30fps, max 1080p, 8Mbps) - Helper functions for time formatting and statistics - Proper logging of all operations Performance optimizations: - Database indexes on all foreign keys and frequently filtered columns - Lazy loading for relationships where appropriate - Efficient queries with proper ordering - Helper properties to avoid repeated calculations Ready for template migration and testing
This commit is contained in:
64
app/models/content.py
Normal file
64
app/models/content.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""Content model for media files."""
|
||||
from datetime import datetime
|
||||
from typing import Optional, List
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.group import group_content
|
||||
|
||||
|
||||
class Content(db.Model):
|
||||
"""Content model representing media files for display.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
filename: Original filename
|
||||
content_type: Type of content (image, video, pdf, presentation, other)
|
||||
duration: Display duration in seconds
|
||||
file_size: File size in bytes
|
||||
description: Optional content description
|
||||
position: Display order position
|
||||
uploaded_at: Upload timestamp
|
||||
"""
|
||||
__tablename__ = 'content'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
filename = db.Column(db.String(255), nullable=False, index=True)
|
||||
content_type = db.Column(db.String(50), nullable=False, index=True)
|
||||
duration = db.Column(db.Integer, default=10, nullable=True)
|
||||
file_size = db.Column(db.BigInteger, nullable=True)
|
||||
description = db.Column(db.Text, nullable=True)
|
||||
position = db.Column(db.Integer, default=0, index=True)
|
||||
uploaded_at = db.Column(db.DateTime, default=datetime.utcnow,
|
||||
nullable=False, index=True)
|
||||
|
||||
# Relationships
|
||||
groups = db.relationship('Group', secondary=group_content,
|
||||
back_populates='contents', lazy='dynamic')
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of Content."""
|
||||
return f'<Content {self.filename} (Type={self.content_type})>'
|
||||
|
||||
@property
|
||||
def file_size_mb(self) -> float:
|
||||
"""Get file size in megabytes."""
|
||||
if self.file_size:
|
||||
return round(self.file_size / (1024 * 1024), 2)
|
||||
return 0.0
|
||||
|
||||
@property
|
||||
def group_count(self) -> int:
|
||||
"""Get number of groups containing this content."""
|
||||
return self.groups.count()
|
||||
|
||||
def is_image(self) -> bool:
|
||||
"""Check if content is an image."""
|
||||
return self.content_type == 'image'
|
||||
|
||||
def is_video(self) -> bool:
|
||||
"""Check if content is a video."""
|
||||
return self.content_type == 'video'
|
||||
|
||||
def is_pdf(self) -> bool:
|
||||
"""Check if content is a PDF."""
|
||||
return self.content_type == 'pdf'
|
||||
Reference in New Issue
Block a user