Initial commit: enterprise digital platform with portal SSO, DigiServer, IT Assets, NetworkView, Server Monitor
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
"""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.playlist import Playlist, playlist_content
|
||||
from app.models.content import Content
|
||||
from app.models.server_log import ServerLog
|
||||
from app.models.player_feedback import PlayerFeedback
|
||||
from app.models.player_edit import PlayerEdit
|
||||
from app.models.player_user import PlayerUser
|
||||
from app.models.https_config import HTTPSConfig
|
||||
|
||||
__all__ = [
|
||||
'User',
|
||||
'Player',
|
||||
'Group',
|
||||
'Playlist',
|
||||
'Content',
|
||||
'ServerLog',
|
||||
'PlayerFeedback',
|
||||
'PlayerEdit',
|
||||
'PlayerUser',
|
||||
'HTTPSConfig',
|
||||
'group_content',
|
||||
'playlist_content',
|
||||
]
|
||||
@@ -0,0 +1,63 @@
|
||||
"""Content model for media files."""
|
||||
from datetime import datetime
|
||||
from typing import Optional, List
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
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: Default display duration in seconds
|
||||
file_size: File size in bytes
|
||||
description: Optional content description
|
||||
uploaded_at: Upload timestamp
|
||||
"""
|
||||
__tablename__ = 'content'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
filename = db.Column(db.String(255), nullable=False, unique=True, 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)
|
||||
uploaded_at = db.Column(db.DateTime, default=datetime.utcnow,
|
||||
nullable=False, index=True)
|
||||
|
||||
# Relationships - many-to-many with playlists
|
||||
playlists = db.relationship('Playlist', secondary='playlist_content',
|
||||
back_populates='contents', lazy='dynamic')
|
||||
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'
|
||||
@@ -0,0 +1,66 @@
|
||||
"""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
|
||||
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 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()
|
||||
@@ -0,0 +1,104 @@
|
||||
"""HTTPS Configuration model."""
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
class HTTPSConfig(db.Model):
|
||||
"""HTTPS configuration model for managing secure connections.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
https_enabled: Whether HTTPS is enabled
|
||||
hostname: Server hostname (e.g., 'digiserver')
|
||||
domain: Full domain name (e.g., 'digiserver.sibiusb.harting.intra')
|
||||
ip_address: IP address for direct access
|
||||
email: Email address for SSL certificate notifications
|
||||
port: HTTPS port (default 443)
|
||||
created_at: Creation timestamp
|
||||
updated_at: Last update timestamp
|
||||
updated_by: User who made the last update
|
||||
"""
|
||||
__tablename__ = 'https_config'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
https_enabled = db.Column(db.Boolean, default=False, nullable=False)
|
||||
hostname = db.Column(db.String(255), nullable=True)
|
||||
domain = db.Column(db.String(255), nullable=True)
|
||||
ip_address = db.Column(db.String(45), nullable=True) # Support IPv6
|
||||
email = db.Column(db.String(255), nullable=True)
|
||||
port = db.Column(db.Integer, default=443, nullable=False)
|
||||
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)
|
||||
updated_by = db.Column(db.String(255), nullable=True)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of HTTPSConfig."""
|
||||
status = 'ENABLED' if self.https_enabled else 'DISABLED'
|
||||
return f'<HTTPSConfig [{status}] {self.domain or "N/A"}>'
|
||||
|
||||
@classmethod
|
||||
def get_config(cls) -> Optional['HTTPSConfig']:
|
||||
"""Get the current HTTPS configuration.
|
||||
|
||||
Returns:
|
||||
HTTPSConfig instance or None if not configured
|
||||
"""
|
||||
return cls.query.first()
|
||||
|
||||
@classmethod
|
||||
def create_or_update(cls, https_enabled: bool, hostname: str = None,
|
||||
domain: str = None, ip_address: str = None,
|
||||
email: str = None, port: int = 443,
|
||||
updated_by: str = None) -> 'HTTPSConfig':
|
||||
"""Create or update HTTPS configuration.
|
||||
|
||||
Args:
|
||||
https_enabled: Whether HTTPS is enabled
|
||||
hostname: Server hostname
|
||||
domain: Full domain name
|
||||
ip_address: IP address
|
||||
email: Email for SSL certificates
|
||||
port: HTTPS port
|
||||
updated_by: Username of who made the update
|
||||
|
||||
Returns:
|
||||
HTTPSConfig instance
|
||||
"""
|
||||
config = cls.get_config()
|
||||
if not config:
|
||||
config = cls()
|
||||
|
||||
config.https_enabled = https_enabled
|
||||
config.hostname = hostname
|
||||
config.domain = domain
|
||||
config.ip_address = ip_address
|
||||
config.email = email
|
||||
config.port = port
|
||||
config.updated_by = updated_by
|
||||
config.updated_at = datetime.utcnow()
|
||||
|
||||
db.session.add(config)
|
||||
db.session.commit()
|
||||
return config
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert configuration to dictionary.
|
||||
|
||||
Returns:
|
||||
Dictionary representation of config
|
||||
"""
|
||||
return {
|
||||
'id': self.id,
|
||||
'https_enabled': self.https_enabled,
|
||||
'hostname': self.hostname,
|
||||
'domain': self.domain,
|
||||
'ip_address': self.ip_address,
|
||||
'email': self.email,
|
||||
'port': self.port,
|
||||
'created_at': self.created_at.isoformat() if self.created_at else None,
|
||||
'updated_at': self.updated_at.isoformat() if self.updated_at else None,
|
||||
'updated_by': self.updated_by,
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
"""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
|
||||
hostname: Unique hostname/identifier for the player
|
||||
location: Physical location description
|
||||
auth_code: Authentication code for API access (legacy)
|
||||
password_hash: Hashed password for player authentication
|
||||
quickconnect_code: Hashed quick connect code for easy pairing
|
||||
orientation: Display orientation (Landscape/Portrait)
|
||||
status: Current player status (online, offline, error)
|
||||
last_seen: Last activity timestamp
|
||||
playlist_version: Version number for playlist synchronization
|
||||
created_at: Player creation timestamp
|
||||
"""
|
||||
__tablename__ = 'player'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
hostname = db.Column(db.String(255), unique=True, nullable=False, index=True)
|
||||
location = db.Column(db.String(255), nullable=True)
|
||||
auth_code = db.Column(db.String(255), unique=True, nullable=False, index=True)
|
||||
password_hash = db.Column(db.String(255), nullable=False)
|
||||
quickconnect_code = db.Column(db.String(255), nullable=True)
|
||||
orientation = db.Column(db.String(16), default='Landscape', nullable=False)
|
||||
status = db.Column(db.String(50), default='offline', index=True)
|
||||
last_seen = db.Column(db.DateTime, nullable=True, index=True)
|
||||
last_heartbeat = db.Column(db.DateTime, nullable=True, index=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
# Playlist assignment
|
||||
playlist_id = db.Column(db.Integer, db.ForeignKey('playlist.id', ondelete='SET NULL'),
|
||||
nullable=True, index=True)
|
||||
|
||||
# Relationships
|
||||
playlist = db.relationship('Playlist', 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()
|
||||
|
||||
def set_password(self, password: str) -> None:
|
||||
"""Set player password with bcrypt hashing.
|
||||
|
||||
Args:
|
||||
password: Plain text password
|
||||
"""
|
||||
from app.extensions import bcrypt
|
||||
self.password_hash = bcrypt.generate_password_hash(password).decode('utf-8')
|
||||
|
||||
def check_password(self, password: str) -> bool:
|
||||
"""Verify player password.
|
||||
|
||||
Args:
|
||||
password: Plain text password to check
|
||||
|
||||
Returns:
|
||||
True if password matches, False otherwise
|
||||
"""
|
||||
from app.extensions import bcrypt
|
||||
return bcrypt.check_password_hash(self.password_hash, password)
|
||||
|
||||
def set_quickconnect_code(self, code: str) -> None:
|
||||
"""Set quick connect code with bcrypt hashing.
|
||||
|
||||
Args:
|
||||
code: Plain text quick connect code
|
||||
"""
|
||||
from app.extensions import bcrypt
|
||||
self.quickconnect_code = bcrypt.generate_password_hash(code).decode('utf-8')
|
||||
|
||||
def check_quickconnect_code(self, code: str) -> bool:
|
||||
"""Verify quick connect code.
|
||||
|
||||
Args:
|
||||
code: Plain text code to check
|
||||
|
||||
Returns:
|
||||
True if code matches, False otherwise
|
||||
"""
|
||||
if not self.quickconnect_code:
|
||||
return False
|
||||
from app.extensions import bcrypt
|
||||
return bcrypt.check_password_hash(self.quickconnect_code, code)
|
||||
|
||||
@staticmethod
|
||||
def authenticate(hostname: str, password: str = None, quickconnect_code: str = None) -> Optional['Player']:
|
||||
"""Authenticate a player by hostname and password or quickconnect code.
|
||||
|
||||
Args:
|
||||
hostname: Player hostname
|
||||
password: Player password (optional if using quickconnect)
|
||||
quickconnect_code: Quick connect code (optional if using password)
|
||||
|
||||
Returns:
|
||||
Player instance if authentication successful, None otherwise
|
||||
"""
|
||||
player = Player.query.filter_by(hostname=hostname).first()
|
||||
if not player:
|
||||
return None
|
||||
|
||||
# Try password authentication first
|
||||
if password and player.check_password(password):
|
||||
return player
|
||||
|
||||
# Try quickconnect code authentication
|
||||
if quickconnect_code and player.check_quickconnect_code(quickconnect_code):
|
||||
return player
|
||||
|
||||
return None
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Player edit model for tracking media edited on players."""
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
class PlayerEdit(db.Model):
|
||||
"""Player edit model for tracking media files edited on player devices.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
player_id: Foreign key to player
|
||||
content_id: Foreign key to content that was edited
|
||||
original_name: Original filename
|
||||
new_name: New filename after editing
|
||||
version: Edit version number (v1, v2, etc.)
|
||||
user: User who made the edit (from player)
|
||||
time_of_modification: When the edit was made
|
||||
metadata_path: Path to the metadata JSON file
|
||||
edited_file_path: Path to the edited file
|
||||
created_at: Record creation timestamp
|
||||
"""
|
||||
__tablename__ = 'player_edit'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
player_id = db.Column(db.Integer, db.ForeignKey('player.id', ondelete='CASCADE'), nullable=False, index=True)
|
||||
content_id = db.Column(db.Integer, db.ForeignKey('content.id', ondelete='CASCADE'), nullable=False, index=True)
|
||||
original_name = db.Column(db.String(255), nullable=False)
|
||||
new_name = db.Column(db.String(255), nullable=False)
|
||||
version = db.Column(db.Integer, default=1, nullable=False)
|
||||
user = db.Column(db.String(255), nullable=True)
|
||||
time_of_modification = db.Column(db.DateTime, nullable=True)
|
||||
metadata_path = db.Column(db.String(512), nullable=True)
|
||||
edited_file_path = db.Column(db.String(512), nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False, index=True)
|
||||
|
||||
# Relationships
|
||||
player = db.relationship('Player', backref=db.backref('edits', lazy='dynamic', cascade='all, delete-orphan'))
|
||||
content = db.relationship('Content', backref=db.backref('edits', lazy='dynamic', cascade='all, delete-orphan'))
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of PlayerEdit."""
|
||||
return f'<PlayerEdit {self.original_name} v{self.version} by {self.user or "unknown"}>'
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert to dictionary for API responses."""
|
||||
return {
|
||||
'id': self.id,
|
||||
'player_id': self.player_id,
|
||||
'player_name': self.player.name if self.player else None,
|
||||
'content_id': self.content_id,
|
||||
'original_name': self.original_name,
|
||||
'new_name': self.new_name,
|
||||
'version': self.version,
|
||||
'user': self.user,
|
||||
'time_of_modification': self.time_of_modification.isoformat() if self.time_of_modification else None,
|
||||
'created_at': self.created_at.isoformat() if self.created_at else None,
|
||||
'edited_file_path': self.edited_file_path
|
||||
}
|
||||
@@ -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()
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Player user model for managing user codes and names."""
|
||||
from datetime import datetime
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
class PlayerUser(db.Model):
|
||||
"""Player user model for managing user codes and names globally.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
user_code: User code received from player (unique)
|
||||
user_name: Display name for the user
|
||||
created_at: Record creation timestamp
|
||||
updated_at: Record update timestamp
|
||||
"""
|
||||
__tablename__ = 'player_user'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
user_code = db.Column(db.String(255), nullable=False, unique=True, index=True)
|
||||
user_name = db.Column(db.String(255), 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)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of PlayerUser."""
|
||||
return f'<PlayerUser {self.user_code} -> {self.user_name or "Unnamed"}>'
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Convert to dictionary for API responses."""
|
||||
return {
|
||||
'id': self.id,
|
||||
'user_code': self.user_code,
|
||||
'user_name': self.user_name,
|
||||
'created_at': self.created_at.isoformat() if self.created_at else None,
|
||||
'updated_at': self.updated_at.isoformat() if self.updated_at else None,
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
"""Playlist model for managing content collections."""
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
from app.extensions import db
|
||||
|
||||
|
||||
# Association table for many-to-many relationship between playlists and content
|
||||
playlist_content = db.Table('playlist_content',
|
||||
db.Column('playlist_id', db.Integer, db.ForeignKey('playlist.id', ondelete='CASCADE'), primary_key=True),
|
||||
db.Column('content_id', db.Integer, db.ForeignKey('content.id', ondelete='CASCADE'), primary_key=True),
|
||||
db.Column('position', db.Integer, default=0),
|
||||
db.Column('duration', db.Integer, default=10),
|
||||
db.Column('muted', db.Boolean, default=True),
|
||||
db.Column('edit_on_player_enabled', db.Boolean, default=False)
|
||||
)
|
||||
|
||||
|
||||
class Playlist(db.Model):
|
||||
"""Playlist model representing a collection of content.
|
||||
|
||||
Attributes:
|
||||
id: Primary key
|
||||
name: Unique playlist name
|
||||
description: Optional playlist description
|
||||
version: Version number for synchronization
|
||||
is_active: Whether playlist is active
|
||||
created_at: Playlist creation timestamp
|
||||
updated_at: Last modification timestamp
|
||||
"""
|
||||
__tablename__ = 'playlist'
|
||||
|
||||
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)
|
||||
orientation = db.Column(db.String(20), default='Landscape', nullable=False)
|
||||
version = db.Column(db.Integer, default=1, nullable=False)
|
||||
is_active = db.Column(db.Boolean, default=True, nullable=False)
|
||||
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='playlist', lazy='dynamic')
|
||||
contents = db.relationship('Content', secondary=playlist_content,
|
||||
back_populates='playlists', lazy='dynamic')
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""String representation of Playlist."""
|
||||
return f'<Playlist {self.name} (ID={self.id}, Version={self.version})>'
|
||||
|
||||
@property
|
||||
def player_count(self) -> int:
|
||||
"""Get number of players assigned to this playlist."""
|
||||
return self.players.count()
|
||||
|
||||
@property
|
||||
def content_count(self) -> int:
|
||||
"""Get number of content items in this playlist."""
|
||||
return self.contents.count()
|
||||
|
||||
@property
|
||||
def total_duration(self) -> int:
|
||||
"""Calculate total duration of all content in seconds."""
|
||||
total = 0
|
||||
for content in self.contents:
|
||||
total += content.duration or 10
|
||||
return total
|
||||
|
||||
def increment_version(self) -> None:
|
||||
"""Increment playlist version for sync detection."""
|
||||
self.version += 1
|
||||
self.updated_at = datetime.utcnow()
|
||||
|
||||
def get_content_ordered(self) -> List:
|
||||
"""Get content items ordered by position."""
|
||||
# Query through association table to get position
|
||||
from sqlalchemy import select
|
||||
stmt = select(playlist_content.c.content_id,
|
||||
playlist_content.c.position,
|
||||
playlist_content.c.duration,
|
||||
playlist_content.c.muted,
|
||||
playlist_content.c.edit_on_player_enabled).where(
|
||||
playlist_content.c.playlist_id == self.id
|
||||
).order_by(playlist_content.c.position)
|
||||
|
||||
results = db.session.execute(stmt).fetchall()
|
||||
|
||||
ordered_content = []
|
||||
for row in results:
|
||||
content = db.session.get(Content, row.content_id)
|
||||
if content:
|
||||
content._playlist_position = row.position
|
||||
content._playlist_duration = row.duration
|
||||
content._playlist_muted = row.muted if len(row) > 3 else True
|
||||
content._playlist_edit_on_player_enabled = row.edit_on_player_enabled if len(row) > 4 else False
|
||||
ordered_content.append(content)
|
||||
|
||||
return ordered_content
|
||||
|
||||
|
||||
# Import Content here to avoid circular import
|
||||
from app.models.content import Content
|
||||
@@ -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
|
||||
@@ -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