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:
17
app/models/__init__.py
Normal file
17
app/models/__init__.py
Normal file
@@ -0,0 +1,17 @@
|
||||
"""Models package for digiserver-v2."""
|
||||
from app.models.user import User
|
||||
from app.models.player import Player
|
||||
from app.models.group import Group, group_content
|
||||
from app.models.content import Content
|
||||
from app.models.server_log import ServerLog
|
||||
from app.models.player_feedback import PlayerFeedback
|
||||
|
||||
__all__ = [
|
||||
'User',
|
||||
'Player',
|
||||
'Group',
|
||||
'Content',
|
||||
'ServerLog',
|
||||
'PlayerFeedback',
|
||||
'group_content',
|
||||
]
|
||||
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'
|
||||
70
app/models/group.py
Normal file
70
app/models/group.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""Group model for organizing players and content."""
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
# Association table for many-to-many relationship between groups and content
|
||||
group_content = db.Table('group_content',
|
||||
db.Column('group_id', db.Integer, db.ForeignKey('group.id'), primary_key=True),
|
||||
db.Column('content_id', db.Integer, db.ForeignKey('content.id'), primary_key=True)
|
||||
)
|
||||
|
||||
|
||||
class Group(db.Model):
|
||||
"""Group model for organizing players with shared content.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
name: Unique group name
|
||||
description: Optional group description
|
||||
created_at: Group creation timestamp
|
||||
updated_at: Last modification timestamp
|
||||
"""
|
||||
__tablename__ = 'group'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(100), nullable=False, unique=True, index=True)
|
||||
description = db.Column(db.Text, nullable=True)
|
||||
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)
|
||||
|
||||
# Relationships
|
||||
players = db.relationship('Player', back_populates='group', lazy='dynamic')
|
||||
contents = db.relationship('Content', secondary=group_content,
|
||||
back_populates='groups', lazy='dynamic')
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of Group."""
|
||||
return f'<Group {self.name} (ID={self.id})>'
|
||||
|
||||
@property
|
||||
def player_count(self) -> int:
|
||||
"""Get number of players in this group."""
|
||||
return self.players.count()
|
||||
|
||||
@property
|
||||
def content_count(self) -> int:
|
||||
"""Get number of content items in this group."""
|
||||
return self.contents.count()
|
||||
|
||||
def add_player(self, player) -> None:
|
||||
"""Add a player to this group.
|
||||
|
||||
Args:
|
||||
player: Player instance to add
|
||||
"""
|
||||
player.group_id = self.id
|
||||
self.updated_at = datetime.utcnow()
|
||||
|
||||
def remove_player(self, player) -> None:
|
||||
"""Remove a player from this group.
|
||||
|
||||
Args:
|
||||
player: Player instance to remove
|
||||
"""
|
||||
if player.group_id == self.id:
|
||||
player.group_id = None
|
||||
self.updated_at = datetime.utcnow()
|
||||
56
app/models/player.py
Normal file
56
app/models/player.py
Normal file
@@ -0,0 +1,56 @@
|
||||
"""Player model for digital signage players."""
|
||||
from datetime import datetime
|
||||
from typing import Optional, List
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
class Player(db.Model):
|
||||
"""Player model representing a digital signage device.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
name: Display name for the player
|
||||
location: Physical location description
|
||||
auth_code: Authentication code for API access
|
||||
group_id: Foreign key to assigned group
|
||||
status: Current player status (online, offline, error)
|
||||
last_seen: Last activity timestamp
|
||||
created_at: Player creation timestamp
|
||||
"""
|
||||
__tablename__ = 'player'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
location = db.Column(db.String(255), nullable=True)
|
||||
auth_code = db.Column(db.String(255), unique=True, nullable=False, index=True)
|
||||
group_id = db.Column(db.Integer, db.ForeignKey('group.id'), nullable=True, index=True)
|
||||
status = db.Column(db.String(50), default='offline', index=True)
|
||||
last_seen = db.Column(db.DateTime, nullable=True, index=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
# Relationships
|
||||
group = db.relationship('Group', back_populates='players')
|
||||
feedback = db.relationship('PlayerFeedback', back_populates='player',
|
||||
cascade='all, delete-orphan', lazy='dynamic')
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of Player."""
|
||||
return f'<Player {self.name} (ID={self.id}, Status={self.status})>'
|
||||
|
||||
@property
|
||||
def is_online(self) -> bool:
|
||||
"""Check if player is online (seen in last 5 minutes)."""
|
||||
if not self.last_seen:
|
||||
return False
|
||||
delta = datetime.utcnow() - self.last_seen
|
||||
return delta.total_seconds() < 300 # 5 minutes
|
||||
|
||||
def update_status(self, status: str) -> None:
|
||||
"""Update player status and last seen timestamp.
|
||||
|
||||
Args:
|
||||
status: New status value
|
||||
"""
|
||||
self.status = status
|
||||
self.last_seen = datetime.utcnow()
|
||||
64
app/models/player_feedback.py
Normal file
64
app/models/player_feedback.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""Player feedback model for tracking player status and errors."""
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
class PlayerFeedback(db.Model):
|
||||
"""Player feedback model for tracking player status updates.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
player_id: Foreign key to player
|
||||
status: Current status (playing, paused, error, unknown)
|
||||
current_content_id: ID of currently playing content
|
||||
message: Optional status message
|
||||
error: Optional error message
|
||||
timestamp: Feedback timestamp
|
||||
"""
|
||||
__tablename__ = 'player_feedback'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
player_id = db.Column(db.Integer, db.ForeignKey('player.id'),
|
||||
nullable=False, index=True)
|
||||
status = db.Column(db.String(50), nullable=False, default='unknown')
|
||||
current_content_id = db.Column(db.Integer, db.ForeignKey('content.id'),
|
||||
nullable=True)
|
||||
message = db.Column(db.Text, nullable=True)
|
||||
error = db.Column(db.Text, nullable=True)
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow,
|
||||
nullable=False, index=True)
|
||||
|
||||
# Relationships
|
||||
player = db.relationship('Player', back_populates='feedback')
|
||||
content = db.relationship('Content', backref='feedback_entries')
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of PlayerFeedback."""
|
||||
return f'<PlayerFeedback Player={self.player_id} Status={self.status}>'
|
||||
|
||||
@property
|
||||
def is_error(self) -> bool:
|
||||
"""Check if feedback indicates an error."""
|
||||
return self.status == 'error' or self.error is not None
|
||||
|
||||
@property
|
||||
def age_seconds(self) -> float:
|
||||
"""Get age of feedback in seconds."""
|
||||
delta = datetime.utcnow() - self.timestamp
|
||||
return delta.total_seconds()
|
||||
|
||||
@classmethod
|
||||
def get_latest_for_player(cls, player_id: int) -> Optional['PlayerFeedback']:
|
||||
"""Get most recent feedback for a player.
|
||||
|
||||
Args:
|
||||
player_id: Player ID to query
|
||||
|
||||
Returns:
|
||||
Latest PlayerFeedback instance or None
|
||||
"""
|
||||
return cls.query.filter_by(player_id=player_id)\
|
||||
.order_by(cls.timestamp.desc())\
|
||||
.first()
|
||||
72
app/models/server_log.py
Normal file
72
app/models/server_log.py
Normal file
@@ -0,0 +1,72 @@
|
||||
"""Server log model for audit trail."""
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
class ServerLog(db.Model):
|
||||
"""Server log model for tracking system events.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
level: Log level (info, warning, error)
|
||||
message: Log message content
|
||||
timestamp: Event timestamp
|
||||
"""
|
||||
__tablename__ = 'server_log'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
level = db.Column(db.String(20), nullable=False, index=True, default='info')
|
||||
message = db.Column(db.Text, nullable=False)
|
||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow,
|
||||
nullable=False, index=True)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of ServerLog."""
|
||||
return f'<ServerLog [{self.level.upper()}] {self.message[:50]}>'
|
||||
|
||||
@classmethod
|
||||
def log_info(cls, message: str) -> 'ServerLog':
|
||||
"""Create an info level log entry.
|
||||
|
||||
Args:
|
||||
message: Log message
|
||||
|
||||
Returns:
|
||||
ServerLog instance
|
||||
"""
|
||||
log = cls(level='info', message=message)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
return log
|
||||
|
||||
@classmethod
|
||||
def log_warning(cls, message: str) -> 'ServerLog':
|
||||
"""Create a warning level log entry.
|
||||
|
||||
Args:
|
||||
message: Log message
|
||||
|
||||
Returns:
|
||||
ServerLog instance
|
||||
"""
|
||||
log = cls(level='warning', message=message)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
return log
|
||||
|
||||
@classmethod
|
||||
def log_error(cls, message: str) -> 'ServerLog':
|
||||
"""Create an error level log entry.
|
||||
|
||||
Args:
|
||||
message: Log message
|
||||
|
||||
Returns:
|
||||
ServerLog instance
|
||||
"""
|
||||
log = cls(level='error', message=message)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
return log
|
||||
43
app/models/user.py
Normal file
43
app/models/user.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""User model for authentication and authorization."""
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from flask_login import UserMixin
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
class User(db.Model, UserMixin):
|
||||
"""User model for application authentication.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
username: Unique username for login
|
||||
password: Bcrypt hashed password
|
||||
role: User role (user or admin)
|
||||
theme: UI theme preference (light or dark)
|
||||
created_at: Account creation timestamp
|
||||
last_login: Last successful login timestamp
|
||||
"""
|
||||
__tablename__ = 'user'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(80), unique=True, nullable=False, index=True)
|
||||
password = db.Column(db.String(120), nullable=False)
|
||||
role = db.Column(db.String(20), nullable=False, default='user', index=True)
|
||||
theme = db.Column(db.String(20), default='light')
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
|
||||
last_login = db.Column(db.DateTime, nullable=True)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of User."""
|
||||
return f'<User {self.username} (role={self.role})>'
|
||||
|
||||
@property
|
||||
def is_admin(self) -> bool:
|
||||
"""Check if user has admin role."""
|
||||
return self.role == 'admin'
|
||||
|
||||
def update_last_login(self) -> None:
|
||||
"""Update last login timestamp."""
|
||||
self.last_login = datetime.utcnow()
|
||||
Reference in New Issue
Block a user