diff --git a/server.py b/server.py index 0617a4e..94b0b05 100644 --- a/server.py +++ b/server.py @@ -260,14 +260,14 @@ def upload_file(): if channel and channel.is_active: # Get next order number for this channel - max_order = db.session.query(db.func.max(ChannelContent.order)).filter_by(channel_id=channel_id).scalar() or 0 + max_order = db.session.query(db.func.max(ChannelContent.order_index)).filter_by(channel_id=channel_id).scalar() or 0 # Add to channel channel_content = ChannelContent( channel_id=channel_id, media_file_id=media_file.id, - duration=int(display_duration), - order=max_order + 1 + display_duration=int(display_duration), + order_index=max_order + 1 ) db.session.add(channel_content) db.session.commit() @@ -353,6 +353,139 @@ def add_channel(): return redirect(url_for('view_channels')) +@app.route('/delete_channel/') +@login_required +def delete_channel(channel_id): + channel = StreamingChannel.query.get_or_404(channel_id) + + # Check permissions + if current_user.role != 'admin' and channel.created_by != current_user.id: + flash('Access denied. You can only delete your own channels.', 'error') + return redirect(url_for('view_channels')) + + # Prevent deleting default channel + if channel.is_default: + flash('Cannot delete the default channel.', 'error') + return redirect(url_for('view_channels')) + + # Check if any players are assigned to this channel + assigned_players = Player.query.filter_by(channel_id=channel_id).count() + if assigned_players > 0: + flash(f'Cannot delete channel. {assigned_players} player(s) are assigned to this channel.', 'error') + return redirect(url_for('view_channels')) + + channel_name = channel.name + db.session.delete(channel) + db.session.commit() + + log_activity('Channel deleted', f'Deleted channel: {channel_name}') + flash(f'Channel "{channel_name}" deleted successfully!', 'success') + return redirect(url_for('view_channels')) + +@app.route('/edit_channel/', methods=['GET', 'POST']) +@login_required +def edit_channel(channel_id): + channel = StreamingChannel.query.get_or_404(channel_id) + + # Check permissions + if current_user.role != 'admin' and channel.created_by != current_user.id: + flash('Access denied. You can only edit your own channels.', 'error') + return redirect(url_for('view_channels')) + + if request.method == 'POST': + try: + # If setting as default, unset other defaults + is_default = 'is_default' in request.form + if is_default and not channel.is_default: + StreamingChannel.query.filter_by(is_default=True).update({'is_default': False}) + + channel.name = request.form['name'] + channel.description = request.form.get('description', '') + channel.is_default = is_default + channel.is_active = 'is_active' in request.form + + db.session.commit() + + log_activity('Channel updated', f'Updated channel: {channel.name}') + flash(f'Channel "{channel.name}" updated successfully!', 'success') + except Exception as e: + flash(f'Error updating channel: {str(e)}', 'error') + + return redirect(url_for('view_channels')) + + return render_template('edit_channel.html', channel=channel) + +@app.route('/manage_content/') +@login_required +def manage_content(channel_id): + channel = StreamingChannel.query.get_or_404(channel_id) + + # Check permissions + if current_user.role != 'admin' and channel.created_by != current_user.id: + flash('Access denied. You can only manage content of your own channels.', 'error') + return redirect(url_for('view_channels')) + + # Get all available media files + available_media = MediaFile.query.filter_by(is_active=True).order_by(MediaFile.upload_date.desc()).all() + + # Get current channel content with media file details + channel_content = ChannelContent.query.filter_by(channel_id=channel_id).order_by(ChannelContent.order_index).all() + + return render_template('manage_content.html', channel=channel, available_media=available_media, channel_content=channel_content) + +@app.route('/add_content_to_channel', methods=['POST']) +@login_required +def add_content_to_channel(): + channel_id = request.form['channel_id'] + media_file_id = request.form['media_file_id'] + duration = request.form.get('duration', 10) + + channel = StreamingChannel.query.get_or_404(channel_id) + + # Check permissions + if current_user.role != 'admin' and channel.created_by != current_user.id: + flash('Access denied.', 'error') + return redirect(url_for('view_channels')) + + # Get next order number + max_order = db.session.query(db.func.max(ChannelContent.order_index)).filter_by(channel_id=channel_id).scalar() or 0 + + # Add content + content = ChannelContent( + channel_id=channel_id, + media_file_id=media_file_id, + display_duration=int(duration), + order_index=max_order + 1 + ) + + db.session.add(content) + db.session.commit() + + media_file = MediaFile.query.get(media_file_id) + log_activity('Content added to channel', f'Added {media_file.original_name} to {channel.name}') + flash('Content added to channel successfully!', 'success') + + return redirect(url_for('manage_content', channel_id=channel_id)) + +@app.route('/remove_content_from_channel/') +@login_required +def remove_content_from_channel(content_id): + content = ChannelContent.query.get_or_404(content_id) + channel = content.channel + + # Check permissions + if current_user.role != 'admin' and channel.created_by != current_user.id: + flash('Access denied.', 'error') + return redirect(url_for('view_channels')) + + db.session.delete(content) + db.session.commit() + + log_activity('Content removed from channel', f'Removed content from {channel.name}') + flash('Content removed from channel successfully!', 'success') + + return redirect(url_for('manage_content', channel_id=channel.id)) + # User Management Routes @app.route('/add_user', methods=['POST']) @login_required diff --git a/templates/channels.html b/templates/channels.html index 7e56435..c2514a1 100644 --- a/templates/channels.html +++ b/templates/channels.html @@ -57,7 +57,11 @@ {{ current_user.username }}