podcastrr/app/web/routes/main.py
2025-06-17 16:00:46 -07:00

78 lines
2.7 KiB
Python

"""
Main routes for the Podcastrr application.
"""
from flask import Blueprint, render_template, current_app, redirect, url_for, request
from app.models.podcast import Podcast
main_bp = Blueprint('main', __name__)
@main_bp.route('/')
def index():
"""
Redirect to the podcasts page or handle specific views.
"""
view = request.args.get('view')
if view in ['history', 'wanted', 'status']:
# Get recent podcasts for the template
recent_podcasts = Podcast.query.order_by(Podcast.last_updated.desc()).limit(5).all()
# For now, just render the index template with the view parameter
# In the future, these could be implemented as separate views
return render_template('index.html',
title=view.capitalize(),
view=view,
recent_podcasts=recent_podcasts)
# Default: redirect to podcasts page
return redirect(url_for('podcasts.index'))
@main_bp.route('/about')
def about():
"""
Render the about page.
"""
return render_template('about.html', title='About')
@main_bp.route('/dashboard')
def dashboard():
"""
Render the dashboard page.
"""
# Get statistics
total_podcasts = Podcast.query.count()
# Get episode statistics
from app.models.podcast import Episode
total_episodes = Episode.query.count()
downloaded_episodes = Episode.query.filter_by(downloaded=True).count()
not_downloaded_episodes = total_episodes - downloaded_episodes
# Calculate total storage used (in bytes)
from sqlalchemy import func
total_storage_bytes = Episode.query.filter_by(downloaded=True).with_entities(
func.sum(Episode.file_size)).scalar() or 0
# Format storage size in appropriate units
def format_size(size_bytes):
# Convert bytes to appropriate unit
if size_bytes < 1024:
return f"{size_bytes} B"
elif size_bytes < 1024 * 1024:
return f"{size_bytes / 1024:.2f} KB"
elif size_bytes < 1024 * 1024 * 1024:
return f"{size_bytes / (1024 * 1024):.2f} MB"
elif size_bytes < 1024 * 1024 * 1024 * 1024:
return f"{size_bytes / (1024 * 1024 * 1024):.2f} GB"
else:
return f"{size_bytes / (1024 * 1024 * 1024 * 1024):.2f} TB"
formatted_storage = format_size(total_storage_bytes)
return render_template('dashboard.html',
title='Dashboard',
total_podcasts=total_podcasts,
total_episodes=total_episodes,
downloaded_episodes=downloaded_episodes,
not_downloaded_episodes=not_downloaded_episodes,
formatted_storage=formatted_storage)