49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
"""
|
|
Group model for managing collections of players
|
|
"""
|
|
|
|
from app.extensions import db
|
|
|
|
# Association table for many-to-many relationship between Group and Player
|
|
group_player = db.Table('group_player',
|
|
db.Column('group_id', db.Integer, db.ForeignKey('group.id'), primary_key=True),
|
|
db.Column('player_id', db.Integer, db.ForeignKey('player.id'), primary_key=True)
|
|
)
|
|
|
|
class Group(db.Model):
|
|
"""Group model for managing collections of players"""
|
|
|
|
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)
|
|
playlist_version = db.Column(db.Integer, default=0)
|
|
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
|
|
is_active = db.Column(db.Boolean, default=True)
|
|
|
|
# Relationships
|
|
players = db.relationship('Player', secondary=group_player, backref='groups')
|
|
|
|
def increment_playlist_version(self):
|
|
"""Increment playlist version for all players in group"""
|
|
self.playlist_version += 1
|
|
for player in self.players:
|
|
player.increment_playlist_version()
|
|
db.session.commit()
|
|
|
|
@property
|
|
def player_count(self):
|
|
"""Get the number of players in this group"""
|
|
return len(self.players)
|
|
|
|
def get_content(self):
|
|
"""Get all content from players in this group"""
|
|
from app.models.content import Content
|
|
if not self.players:
|
|
return []
|
|
|
|
player_ids = [player.id for player in self.players]
|
|
return Content.query.filter(Content.player_id.in_(player_ids)).order_by(Content.position).all()
|
|
|
|
def __repr__(self):
|
|
return f'<Group {self.name} ({self.player_count} players)>'
|