from flask import Blueprint, render_template, flash, redirect, url_for, jsonify from functions.database import get_db_connection, Militante, Pagamento, CotaMensal, MaterialVendido, AssinaturaAnual, TipoPagamento from functions.decorators import require_login from datetime import datetime from sqlalchemy import func from services.dashboard_service import DashboardService from services.cache_service import cache_service, CacheKeys from flask_login import current_user import logging logger = logging.getLogger(__name__) home_bp = Blueprint('home', __name__) @home_bp.route("/") @require_login def index(): """Rota principal""" return redirect(url_for('home.dashboard')) @home_bp.route("/dashboard") @home_bp.route("/home") @require_login def dashboard(): """Página inicial do sistema com dashboard""" try: # Get dashboard stats from cached service stats = DashboardService.get_dashboard_stats() # Get tipos de pagamento for the modal db = get_db_connection() try: tipos_pagamento = db.query(TipoPagamento).all() finally: db.close() return render_template('home.html', nome_usuario=current_user.nome or current_user.username, data_atual=datetime.now().strftime("%d/%m/%Y"), total_militantes=stats.get('total_militantes', 0), total_cotas=stats.get('total_cotas', "0.00"), total_materiais=stats.get('total_materiais', 0), total_assinaturas=stats.get('total_assinaturas', 0), ultimos_militantes=stats.get('ultimos_militantes', []), ultimos_pagamentos=stats.get('ultimos_pagamentos', []), tipos_pagamento=tipos_pagamento, Militante=Militante, cache_timestamp=stats.get('cache_timestamp')) except Exception as e: logger.error(f"Erro na página inicial: {e}") import traceback traceback.print_exc() flash('Erro ao carregar a página inicial', 'danger') return render_template('home.html', nome_usuario="Usuário", data_atual=datetime.now().strftime("%d/%m/%Y"), total_militantes=0, total_cotas="0.00", total_materiais=0, total_assinaturas=0, ultimos_militantes=[], ultimos_pagamentos=[], Militante=Militante) @home_bp.route('/check_session') def check_session(): """Verifica se a sessão ainda é válida""" if current_user.is_authenticated: if current_user.is_session_expired(): return jsonify({'valid': False, 'message': 'Sessão expirada'}) return jsonify({'valid': True}) return jsonify({'valid': False, 'message': 'Usuário não autenticado'}) @home_bp.route('/api/dashboard/stats') @require_login def api_dashboard_stats(): """API endpoint for dashboard statistics""" try: stats = DashboardService.get_dashboard_stats() return jsonify({ 'success': True, 'data': stats }) except Exception as e: logger.error(f"Erro ao obter estatísticas do dashboard: {e}") return jsonify({ 'success': False, 'error': 'Erro ao obter estatísticas' }), 500 @home_bp.route('/api/dashboard/militante-stats') @require_login def api_militante_stats(): """API endpoint for militante statistics""" try: stats = DashboardService.get_militante_stats() return jsonify({ 'success': True, 'data': stats }) except Exception as e: logger.error(f"Erro ao obter estatísticas de militantes: {e}") return jsonify({ 'success': False, 'error': 'Erro ao obter estatísticas de militantes' }), 500 @home_bp.route('/api/dashboard/financial-stats') @require_login def api_financial_stats(): """API endpoint for financial statistics""" try: stats = DashboardService.get_financial_stats() return jsonify({ 'success': True, 'data': stats }) except Exception as e: logger.error(f"Erro ao obter estatísticas financeiras: {e}") return jsonify({ 'success': False, 'error': 'Erro ao obter estatísticas financeiras' }), 500 @home_bp.route('/api/cache/clear') @require_login def clear_cache(): """Clear all cache (admin only)""" if not current_user.is_admin: return jsonify({ 'success': False, 'error': 'Acesso negado' }), 403 try: cache_service.clear_all() # Invalidate dashboard cache DashboardService.invalidate_dashboard_cache() logger.info(f"Cache limpo por {current_user.username}") return jsonify({ 'success': True, 'message': 'Cache limpo com sucesso' }) except Exception as e: logger.error(f"Erro ao limpar cache: {e}") return jsonify({ 'success': False, 'error': 'Erro ao limpar cache' }), 500 @home_bp.route('/api/cache/status') @require_login def cache_status(): """Get cache status (admin only)""" if not current_user.is_admin: return jsonify({ 'success': False, 'error': 'Acesso negado' }), 403 try: # Check if Redis is connected is_connected = cache_service._is_connected() # Get some cache statistics stats = { 'connected': is_connected, 'dashboard_stats_cached': cache_service.exists(CacheKeys.DASHBOARD_STATS), 'dashboard_stats_ttl': cache_service.ttl(CacheKeys.DASHBOARD_STATS) if is_connected else -1 } return jsonify({ 'success': True, 'data': stats }) except Exception as e: logger.error(f"Erro ao obter status do cache: {e}") return jsonify({ 'success': False, 'error': 'Erro ao obter status do cache' }), 500