Files
igny8/backend/igny8_core/modules/planner/admin.py
2025-12-14 17:30:10 +00:00

191 lines
7.1 KiB
Python

from django.contrib import admin
from django.contrib import messages
from unfold.admin import ModelAdmin
from igny8_core.admin.base import SiteSectorAdminMixin
from .models import Keywords, Clusters, ContentIdeas
from import_export.admin import ExportMixin
from import_export import resources
class KeywordsResource(resources.ModelResource):
"""Resource class for exporting Keywords"""
class Meta:
model = Keywords
fields = ('id', 'keyword', 'seed_keyword__keyword', 'site__name', 'sector__name',
'cluster__name', 'volume', 'difficulty', 'intent', 'status', 'created_at')
export_order = fields
@admin.register(Clusters)
class ClustersAdmin(SiteSectorAdminMixin, ModelAdmin):
list_display = ['name', 'site', 'sector', 'keywords_count', 'volume', 'status', 'created_at']
list_filter = ['status', 'site', 'sector']
search_fields = ['name']
ordering = ['name']
autocomplete_fields = ['site', 'sector']
def get_site_display(self, obj):
"""Safely get site name"""
try:
return obj.site.name if obj.site else '-'
except:
return '-'
get_site_display.short_description = 'Site'
def get_sector_display(self, obj):
"""Safely get sector name"""
try:
return obj.sector.name if obj.sector else '-'
except:
return '-'
@admin.register(Keywords)
class KeywordsAdmin(ExportMixin, SiteSectorAdminMixin, ModelAdmin):
resource_class = KeywordsResource
list_display = ['keyword', 'seed_keyword', 'site', 'sector', 'cluster', 'volume', 'difficulty', 'intent', 'status', 'created_at']
list_filter = ['status', 'seed_keyword__intent', 'site', 'sector', 'seed_keyword__industry', 'seed_keyword__sector']
search_fields = ['seed_keyword__keyword']
ordering = ['-created_at']
autocomplete_fields = ['cluster', 'site', 'sector', 'seed_keyword']
actions = [
'bulk_assign_cluster',
'bulk_set_status_active',
'bulk_set_status_inactive',
]
def get_site_display(self, obj):
"""Safely get site name"""
try:
return obj.site.name if obj.site else '-'
except:
return '-'
get_site_display.short_description = 'Site'
def get_sector_display(self, obj):
"""Safely get sector name"""
try:
return obj.sector.name if obj.sector else '-'
except:
return '-'
def get_cluster_display(self, obj):
"""Safely get cluster name"""
try:
return obj.cluster.name if obj.cluster else '-'
except:
return '-'
get_cluster_display.short_description = 'Cluster'
def bulk_assign_cluster(self, request, queryset):
"""Assign selected keywords to a cluster"""
from django import forms
# If this is the POST request with cluster selection
if 'apply' in request.POST:
cluster_id = request.POST.get('cluster')
if cluster_id:
cluster = Clusters.objects.get(pk=cluster_id)
updated = queryset.update(cluster=cluster)
self.message_user(request, f'{updated} keyword(s) assigned to cluster: {cluster.name}', messages.SUCCESS)
return
# Get first keyword's site/sector for filtering clusters
first_keyword = queryset.first()
if first_keyword:
clusters = Clusters.objects.filter(site=first_keyword.site, sector=first_keyword.sector)
else:
clusters = Clusters.objects.all()
# Create form for cluster selection
class ClusterForm(forms.Form):
cluster = forms.ModelChoiceField(
queryset=clusters,
label="Select Cluster",
help_text=f"Assign {queryset.count()} selected keyword(s) to:"
)
if clusters.exists():
from django.shortcuts import render
return render(request, 'admin/bulk_action_form.html', {
'title': 'Assign Keywords to Cluster',
'queryset': queryset,
'form': ClusterForm(),
'action': 'bulk_assign_cluster',
})
else:
self.message_user(request, 'No clusters available for the selected keywords.', messages.WARNING)
bulk_assign_cluster.short_description = 'Assign to Cluster'
def bulk_set_status_active(self, request, queryset):
"""Set selected keywords to active status"""
updated = queryset.update(status='active')
self.message_user(request, f'{updated} keyword(s) set to active.', messages.SUCCESS)
bulk_set_status_active.short_description = 'Set status to Active'
def bulk_set_status_inactive(self, request, queryset):
"""Set selected keywords to inactive status"""
updated = queryset.update(status='inactive')
self.message_user(request, f'{updated} keyword(s) set to inactive.', messages.SUCCESS)
bulk_set_status_inactive.short_description = 'Set status to Inactive'
@admin.register(ContentIdeas)
class ContentIdeasAdmin(SiteSectorAdminMixin, ModelAdmin):
list_display = ['idea_title', 'site', 'sector', 'description_preview', 'content_type', 'content_structure', 'status', 'keyword_cluster', 'estimated_word_count', 'created_at']
list_filter = ['status', 'content_type', 'content_structure', 'site', 'sector']
search_fields = ['idea_title', 'target_keywords', 'description']
ordering = ['-created_at']
readonly_fields = ['created_at', 'updated_at']
fieldsets = (
('Basic Info', {
'fields': ('idea_title', 'description', 'status', 'site', 'sector')
}),
('Content Planning', {
'fields': ('content_type', 'content_structure', 'estimated_word_count')
}),
('Keywords & Clustering', {
'fields': ('keyword_cluster', 'target_keywords', 'taxonomy')
}),
('Timestamps', {
'fields': ('created_at', 'updated_at'),
'classes': ('collapse',)
}),
)
def description_preview(self, obj):
"""Show a truncated preview of the description"""
if not obj.description:
return '-'
# Truncate to 100 characters
preview = obj.description[:100]
if len(obj.description) > 100:
preview += '...'
return preview
description_preview.short_description = 'Description'
def get_site_display(self, obj):
"""Safely get site name"""
try:
return obj.site.name if obj.site else '-'
except:
return '-'
get_site_display.short_description = 'Site'
def get_sector_display(self, obj):
"""Safely get sector name"""
try:
return obj.sector.name if obj.sector else '-'
except:
return '-'
def get_keyword_cluster_display(self, obj):
"""Safely get cluster name"""
try:
return obj.keyword_cluster.name if obj.keyword_cluster else '-'
except:
return '-'
get_keyword_cluster_display.short_description = 'Cluster'