from django.shortcuts import render from django.db import models from django.db.models import Count, Q from datetime import date from .models import Platform, ContentDeliverable, Strategy def dashboard_view(request): """Main dashboard view with statistics and platform cards""" # Overall statistics total_platforms = Platform.objects.filter(status='active').count() total_deliverables = ContentDeliverable.objects.count() committed_count = ContentDeliverable.objects.filter(status='committed').count() drafted_count = ContentDeliverable.objects.filter(status='drafted').count() published_count = ContentDeliverable.objects.filter(status='published').count() # Calculate overall completion rate if total_deliverables > 0: overall_completion_rate = round((published_count / total_deliverables) * 100, 1) else: overall_completion_rate = 0 # Platform cards with detailed stats platforms = Platform.objects.filter(status='active').prefetch_related( 'contentdeliverable_set', 'strategies' ).annotate( total_content=Count('contentdeliverable'), committed_content=Count('contentdeliverable', filter=Q(contentdeliverable__status='committed')), drafted_content=Count('contentdeliverable', filter=Q(contentdeliverable__status='drafted')), published_content=Count('contentdeliverable', filter=Q(contentdeliverable__status='published')), total_strategies=Count('strategies', filter=Q(strategies__is_active=True)) ) # Add calculated fields to each platform for platform in platforms: if platform.total_content > 0: platform.completion_percentage = round( (platform.published_content / platform.total_content) * 100, 1 ) else: platform.completion_percentage = 0 # Recent deliverables (last 10) recent_deliverables = ContentDeliverable.objects.select_related( 'platform' ).order_by('-updated_at')[:10] # Overdue deliverables overdue_deliverables = ContentDeliverable.objects.filter( target_date__lt=date.today(), status__in=['committed', 'drafted'] ).select_related('platform').order_by('target_date') # Active strategies count total_active_strategies = Strategy.objects.filter(is_active=True).count() # Calculate percentages for progress bar if total_deliverables > 0: published_percentage = round((published_count / total_deliverables) * 100, 1) drafted_percentage = round((drafted_count / total_deliverables) * 100, 1) committed_percentage = round((committed_count / total_deliverables) * 100, 1) else: published_percentage = 0 drafted_percentage = 0 committed_percentage = 0 context = { # Overall stats 'total_platforms': total_platforms, 'total_deliverables': total_deliverables, 'committed_count': committed_count, 'drafted_count': drafted_count, 'published_count': published_count, 'overall_completion_rate': overall_completion_rate, 'total_active_strategies': total_active_strategies, # Platform data 'platforms': platforms, # Recent activity 'recent_deliverables': recent_deliverables, 'overdue_deliverables': overdue_deliverables, # Status for progress bars 'status_data': { 'committed': committed_count, 'drafted': drafted_count, 'published': published_count, }, # Percentages for chart 'published_percentage': published_percentage, 'drafted_percentage': drafted_percentage, 'committed_percentage': committed_percentage, } return render(request, 'dashboard/dashboard.html', context) def platform_detail_view(request, platform_id): """Detailed view for a specific platform""" platform = Platform.objects.get(id=platform_id) # Platform deliverables deliverables = ContentDeliverable.objects.filter( platform=platform ).order_by('-created_at') # Platform strategies strategies = Strategy.objects.filter( platform=platform, is_active=True ).order_by('priority') context = { 'platform': platform, 'deliverables': deliverables, 'strategies': strategies, } return render(request, 'dashboard/platform_detail.html', context)