Files
digiserver-v2/app/models/playlist.py
DigiServer Developer 8e43f2bd42 Add edit_on_player_enabled feature for playlist content
- Added edit_on_player_enabled column to playlist_content table
- Updated playlist model to track edit enablement per content item
- Added UI checkbox on upload media page to enable/disable editing
- Added toggle column on manage playlist page for existing content
- Updated API endpoint to return edit_on_player_enabled flag to players
- Fixed docker-entrypoint.sh to use production config
- Supports PDF, Images, and PPTX content types
2025-12-05 21:31:04 +02:00

104 lines
4.1 KiB
Python

"""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