podcastrr/app/web/routes/main.py
2025-06-16 22:55:39 -07:00

69 lines
2.3 KiB
Python

"""
Main routes for the Podcastrr application.
"""
from flask import Blueprint, render_template, current_app
from app.models.podcast import Podcast
main_bp = Blueprint('main', __name__)
@main_bp.route('/')
def index():
"""
Render the home page.
"""
# Get recent podcasts
recent_podcasts = Podcast.query.order_by(Podcast.last_updated.desc()).limit(5).all()
return render_template('index.html',
title='Home',
recent_podcasts=recent_podcasts)
@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)