from django.contrib import admin from django.utils.html import format_html from .models import Platform, ContentDeliverable, Strategy @admin.register(Platform) class PlatformAdmin(admin.ModelAdmin): list_display = ( 'name', 'platform_type', 'status', 'primary_content_type', 'total_deliverables', 'completion_rate_display', 'created_at' ) list_filter = ('platform_type', 'status', 'created_at') search_fields = ('name', 'description', 'primary_content_type') readonly_fields = ('created_at', 'updated_at') fieldsets = ( ('Basic Information', { 'fields': ('name', 'platform_type', 'status', 'primary_content_type') }), ('Details', { 'fields': ('description', 'target_audience', 'posting_frequency') }), ('Timestamps', { 'fields': ('created_at', 'updated_at'), 'classes': ('collapse',) }) ) def completion_rate_display(self, obj): rate = obj.completion_rate color = 'green' if rate >= 70 else 'orange' if rate >= 40 else 'red' return format_html( '{:.1f}%', color, rate ) completion_rate_display.short_description = 'Completion Rate' def total_deliverables(self, obj): return obj.total_deliverables total_deliverables.short_description = 'Total Content' class ContentDeliverableInline(admin.TabularInline): model = ContentDeliverable extra = 0 fields = ('title', 'content_type', 'status', 'completion_percentage', 'target_date') readonly_fields = () class StrategyInline(admin.TabularInline): model = Strategy extra = 0 fields = ('title', 'priority', 'is_active', 'start_date', 'end_date') @admin.register(ContentDeliverable) class ContentDeliverableAdmin(admin.ModelAdmin): list_display = ( 'title', 'platform', 'content_type', 'status_display', 'completion_percentage_display', 'target_date', 'is_overdue_display' ) list_filter = ('status', 'content_type', 'platform', 'target_date', 'created_at') search_fields = ('title', 'description', 'tags', 'platform__name') readonly_fields = ('created_at', 'updated_at', 'days_until_due') date_hierarchy = 'target_date' fieldsets = ( ('Basic Information', { 'fields': ('platform', 'title', 'content_type', 'status') }), ('Content Details', { 'fields': ('description', 'completion_percentage', 'tags') }), ('Dates', { 'fields': ('target_date', 'actual_date', 'days_until_due') }), ('Additional Info', { 'fields': ('url', 'notes') }), ('Timestamps', { 'fields': ('created_at', 'updated_at'), 'classes': ('collapse',) }) ) def status_display(self, obj): status_colors = { 'committed': 'blue', 'drafted': 'orange', 'published': 'green', 'cancelled': 'red' } color = status_colors.get(obj.status, 'black') return format_html( '{}', color, obj.get_status_display() ) status_display.short_description = 'Status' def completion_percentage_display(self, obj): percentage = obj.completion_percentage if percentage == 100: color = 'green' elif percentage >= 50: color = 'orange' else: color = 'red' return format_html( '