updated for beter media management
This commit is contained in:
@@ -19,15 +19,88 @@ content_bp = Blueprint('content', __name__, url_prefix='/content')
|
||||
def content_list():
|
||||
"""Main playlist management page."""
|
||||
playlists = Playlist.query.order_by(Playlist.created_at.desc()).all()
|
||||
media_files = Content.query.order_by(Content.uploaded_at.desc()).all()
|
||||
media_files = Content.query.order_by(Content.uploaded_at.desc()).limit(3).all() # Only last 3
|
||||
total_media_count = Content.query.count() # Total count for display
|
||||
players = Player.query.order_by(Player.name).all()
|
||||
|
||||
return render_template('content/content_list_new.html',
|
||||
playlists=playlists,
|
||||
media_files=media_files,
|
||||
total_media_count=total_media_count,
|
||||
players=players)
|
||||
|
||||
|
||||
@content_bp.route('/media-library')
|
||||
@login_required
|
||||
def media_library():
|
||||
"""View all media files in the library."""
|
||||
media_files = Content.query.order_by(Content.uploaded_at.desc()).all()
|
||||
|
||||
# Group by content type
|
||||
images = [m for m in media_files if m.content_type == 'image']
|
||||
videos = [m for m in media_files if m.content_type == 'video']
|
||||
pdfs = [m for m in media_files if m.content_type == 'pdf']
|
||||
presentations = [m for m in media_files if m.content_type == 'pptx']
|
||||
others = [m for m in media_files if m.content_type not in ['image', 'video', 'pdf', 'pptx']]
|
||||
|
||||
return render_template('content/media_library.html',
|
||||
media_files=media_files,
|
||||
images=images,
|
||||
videos=videos,
|
||||
pdfs=pdfs,
|
||||
presentations=presentations,
|
||||
others=others)
|
||||
|
||||
|
||||
@content_bp.route('/media/<int:media_id>/delete', methods=['POST'])
|
||||
@login_required
|
||||
def delete_media(media_id: int):
|
||||
"""Delete a media file and remove it from all playlists."""
|
||||
try:
|
||||
media = Content.query.get_or_404(media_id)
|
||||
filename = media.filename
|
||||
|
||||
# Get all playlists containing this media
|
||||
affected_playlists = list(media.playlists.all())
|
||||
|
||||
# Delete physical file
|
||||
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], media.filename)
|
||||
if os.path.exists(file_path):
|
||||
os.remove(file_path)
|
||||
log_action('info', f'Deleted physical file: {filename}')
|
||||
|
||||
# Remove from all playlists (this will cascade properly)
|
||||
db.session.delete(media)
|
||||
|
||||
# Increment version for all affected playlists
|
||||
for playlist in affected_playlists:
|
||||
playlist.version += 1
|
||||
log_action('info', f'Playlist "{playlist.name}" version updated to {playlist.version} (media removed)')
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Clear cache for affected playlists
|
||||
from app.blueprints.players import get_player_playlist
|
||||
from app.extensions import cache
|
||||
for playlist in affected_playlists:
|
||||
for player in playlist.players:
|
||||
cache.delete_memoized(get_player_playlist, player.id)
|
||||
|
||||
if affected_playlists:
|
||||
flash(f'Deleted "{filename}" and removed from {len(affected_playlists)} playlist(s). Playlist versions updated.', 'success')
|
||||
else:
|
||||
flash(f'Deleted "{filename}" successfully.', 'success')
|
||||
|
||||
log_action('info', f'Media deleted: {filename} (affected {len(affected_playlists)} playlists)')
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
log_action('error', f'Error deleting media: {str(e)}')
|
||||
flash(f'Error deleting media: {str(e)}', 'danger')
|
||||
|
||||
return redirect(url_for('content.media_library'))
|
||||
|
||||
|
||||
@content_bp.route('/playlist/create', methods=['POST'])
|
||||
@login_required
|
||||
def create_playlist():
|
||||
|
||||
Reference in New Issue
Block a user