django phase2.3.4.
This commit is contained in:
@@ -1,6 +1,18 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib import messages
|
||||
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)
|
||||
@@ -9,6 +21,7 @@ class ClustersAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
||||
list_filter = ['status', 'site', 'sector']
|
||||
search_fields = ['name']
|
||||
ordering = ['name']
|
||||
autocomplete_fields = ['site', 'sector']
|
||||
|
||||
def get_site_display(self, obj):
|
||||
"""Safely get site name"""
|
||||
@@ -27,11 +40,18 @@ class ClustersAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
||||
|
||||
|
||||
@admin.register(Keywords)
|
||||
class KeywordsAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
||||
class KeywordsAdmin(ExportMixin, SiteSectorAdminMixin, admin.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"""
|
||||
@@ -55,6 +75,58 @@ class KeywordsAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user