updated logs and players page
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
from models import Player, Group, Content
|
||||
from extensions import db
|
||||
from utils.logger import log_group_created, log_group_edited, log_group_deleted
|
||||
from utils.logger import log_player_created, log_player_edited, log_player_deleted
|
||||
from utils.logger import (
|
||||
log_group_created, log_group_edited, log_group_deleted,
|
||||
log_player_created, log_player_edited, log_player_deleted,
|
||||
log_player_added_to_group, log_player_removed_from_group,
|
||||
log_player_unlocked, log_content_reordered,
|
||||
log_content_duration_changed, log_content_added
|
||||
)
|
||||
|
||||
def create_group(name, player_ids):
|
||||
"""
|
||||
@@ -35,6 +40,7 @@ def edit_group(group_id, name, player_ids):
|
||||
Handles locking/unlocking players appropriately.
|
||||
"""
|
||||
group = Group.query.get_or_404(group_id)
|
||||
old_name = group.name # Store old name in case it changes
|
||||
group.name = name
|
||||
|
||||
# Get current players in the group
|
||||
@@ -56,6 +62,9 @@ def edit_group(group_id, name, player_ids):
|
||||
|
||||
# Lock to group
|
||||
player.locked_to_group_id = group.id
|
||||
|
||||
# Log this action
|
||||
log_player_added_to_group(player.username, name)
|
||||
|
||||
# Handle players to remove
|
||||
for player_id in players_to_remove:
|
||||
@@ -66,9 +75,19 @@ def edit_group(group_id, name, player_ids):
|
||||
|
||||
# Unlock from group
|
||||
player.locked_to_group_id = None
|
||||
|
||||
# Log this action
|
||||
log_player_removed_from_group(player.username, name)
|
||||
log_player_unlocked(player.username)
|
||||
|
||||
db.session.commit()
|
||||
log_group_edited(group.name)
|
||||
|
||||
# Log the group edit
|
||||
if old_name != name:
|
||||
log_group_edited(f"{old_name} → {name}")
|
||||
else:
|
||||
log_group_edited(name)
|
||||
|
||||
return group
|
||||
|
||||
def delete_group(group_id):
|
||||
@@ -81,6 +100,7 @@ def delete_group(group_id):
|
||||
# Unlock all players in the group
|
||||
for player in group.players:
|
||||
player.locked_to_group_id = None
|
||||
log_player_unlocked(player.username)
|
||||
|
||||
db.session.delete(group)
|
||||
db.session.commit()
|
||||
@@ -146,16 +166,190 @@ def delete_player(player_id):
|
||||
|
||||
def get_group_content(group_id):
|
||||
"""
|
||||
Get unique content for a group.
|
||||
Get content for all players in a group, ordered by position.
|
||||
"""
|
||||
from models import Group, Content
|
||||
|
||||
group = Group.query.get_or_404(group_id)
|
||||
|
||||
# Get unique media files for the group
|
||||
content = (
|
||||
db.session.query(Content.id, Content.file_name, db.func.min(Content.duration).label('duration'))
|
||||
.filter(Content.player_id.in_([player.id for player in group.players]))
|
||||
.group_by(Content.file_name)
|
||||
.all()
|
||||
)
|
||||
# Get all player IDs in the group
|
||||
player_ids = [player.id for player in group.players]
|
||||
|
||||
return content
|
||||
# Get unique content based on file_name, preserving position
|
||||
unique_content = {}
|
||||
|
||||
# For each player, get their content
|
||||
for player_id in player_ids:
|
||||
# Get content for this player, ordered by position
|
||||
player_content = Content.query.filter_by(player_id=player_id).order_by(Content.position).all()
|
||||
|
||||
for content in player_content:
|
||||
if content.file_name not in unique_content:
|
||||
unique_content[content.file_name] = content
|
||||
|
||||
# Sort the unique content by position
|
||||
return sorted(unique_content.values(), key=lambda c: c.position)
|
||||
|
||||
def get_player_content(player_id):
|
||||
"""
|
||||
Get content for a specific player, ordered by position.
|
||||
"""
|
||||
from models import Content
|
||||
return Content.query.filter_by(player_id=player_id).order_by(Content.position).all()
|
||||
|
||||
def update_player_content_order(player_id, items):
|
||||
"""
|
||||
Update the order of content items for a player.
|
||||
|
||||
Args:
|
||||
player_id (int): ID of the player
|
||||
items (list): List of items with id and position
|
||||
|
||||
Returns:
|
||||
tuple: (success, error_message, new_version)
|
||||
"""
|
||||
from models import Player, Content
|
||||
from extensions import db
|
||||
|
||||
player = Player.query.get_or_404(player_id)
|
||||
|
||||
try:
|
||||
# Update the position field for each content item
|
||||
for item in items:
|
||||
content_id = int(item['id'])
|
||||
position = int(item['position'])
|
||||
content = Content.query.get_or_404(content_id)
|
||||
if content.player_id != player_id:
|
||||
continue # Skip if not for this player
|
||||
content.position = position
|
||||
|
||||
# Force increment the playlist version to trigger client refresh
|
||||
player.playlist_version = (player.playlist_version or 0) + 1
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Log the reordering action
|
||||
log_content_reordered("player", player.username)
|
||||
|
||||
return True, None, player.playlist_version
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return False, str(e), None
|
||||
|
||||
def update_group_content_order(group_id, items):
|
||||
"""
|
||||
Update the order of content items for all players in a group.
|
||||
|
||||
Args:
|
||||
group_id (int): ID of the group
|
||||
items (list): List of items with id and position
|
||||
|
||||
Returns:
|
||||
tuple: (success, error_message)
|
||||
"""
|
||||
from models import Group, Content
|
||||
from extensions import db
|
||||
|
||||
group = Group.query.get_or_404(group_id)
|
||||
|
||||
try:
|
||||
# Get file names corresponding to the content IDs
|
||||
content_files = {}
|
||||
for item in items:
|
||||
content_id = int(item['id'])
|
||||
position = int(item['position'])
|
||||
content = Content.query.get_or_404(content_id)
|
||||
content_files[content.file_name] = position
|
||||
|
||||
# Update all content items for all players in this group
|
||||
for player in group.players:
|
||||
for content in Content.query.filter_by(player_id=player.id).all():
|
||||
if content.file_name in content_files:
|
||||
content.position = content_files[content.file_name]
|
||||
|
||||
# Force increment the playlist version to trigger client refresh
|
||||
player.playlist_version = (player.playlist_version or 0) + 1
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Log the reordering action
|
||||
log_content_reordered("group", group.name)
|
||||
|
||||
return True, None
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return False, str(e)
|
||||
|
||||
def edit_group_media(group_id, content_id, new_duration):
|
||||
"""
|
||||
Update the duration for all instances of a media item across all players in a group.
|
||||
|
||||
Args:
|
||||
group_id (int): ID of the group
|
||||
content_id (int): ID of the content item
|
||||
new_duration (int): New duration in seconds
|
||||
|
||||
Returns:
|
||||
bool: Success or failure
|
||||
"""
|
||||
from models import Group, Content
|
||||
from extensions import db
|
||||
|
||||
group = Group.query.get_or_404(group_id)
|
||||
content = Content.query.get(content_id)
|
||||
file_name = content.file_name
|
||||
old_duration = content.duration
|
||||
|
||||
try:
|
||||
# Update the duration for all players in the group
|
||||
for player in group.players:
|
||||
content = Content.query.filter_by(player_id=player.id, file_name=file_name).first()
|
||||
if content:
|
||||
content.duration = new_duration
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Log the duration change
|
||||
log_content_duration_changed(file_name, old_duration, new_duration, "group", group.name)
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return False
|
||||
|
||||
def delete_group_media(group_id, content_id):
|
||||
"""
|
||||
Delete a media item from all players in a group.
|
||||
|
||||
Args:
|
||||
group_id (int): ID of the group
|
||||
content_id (int): ID of the content item
|
||||
|
||||
Returns:
|
||||
bool: Success or failure
|
||||
"""
|
||||
from models import Group, Content
|
||||
from extensions import db
|
||||
|
||||
group = Group.query.get_or_404(group_id)
|
||||
content = Content.query.get(content_id)
|
||||
file_name = content.file_name
|
||||
|
||||
try:
|
||||
# Delete the media for all players in the group
|
||||
count = 0
|
||||
for player in group.players:
|
||||
content = Content.query.filter_by(player_id=player.id, file_name=file_name).first()
|
||||
if content:
|
||||
db.session.delete(content)
|
||||
count += 1
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Log the content deletion
|
||||
log_content_deleted(file_name, "group", group.name)
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return False
|
||||
Reference in New Issue
Block a user