django phase2.3.4.

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-14 15:10:41 +00:00
parent d161378bd9
commit eb88a0e12d
13 changed files with 2880 additions and 31 deletions

View File

@@ -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)

View File

@@ -33,7 +33,13 @@ class TasksAdmin(ExportMixin, SiteSectorAdminMixin, admin.ModelAdmin):
search_fields = ['title', 'description']
ordering = ['-created_at']
readonly_fields = ['created_at', 'updated_at']
actions = ['bulk_set_status_draft', 'bulk_set_status_in_progress', 'bulk_set_status_completed']
autocomplete_fields = ['cluster', 'site', 'sector']
actions = [
'bulk_set_status_draft',
'bulk_set_status_in_progress',
'bulk_set_status_completed',
'bulk_assign_cluster',
]
fieldsets = (
('Basic Info', {
@@ -69,6 +75,47 @@ class TasksAdmin(ExportMixin, SiteSectorAdminMixin, admin.ModelAdmin):
self.message_user(request, f'{updated} task(s) set to completed.', messages.SUCCESS)
bulk_set_status_completed.short_description = 'Set status to Completed'
def bulk_assign_cluster(self, request, queryset):
"""Assign selected tasks to a cluster - requires form input"""
from django import forms
from igny8_core.modules.planner.models import Clusters
# 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} task(s) assigned to cluster: {cluster.name}', messages.SUCCESS)
return
# Get first task's site/sector for filtering clusters
first_task = queryset.first()
if first_task:
clusters = Clusters.objects.filter(site=first_task.site, sector=first_task.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 task(s) to:"
)
if clusters.exists():
from django.shortcuts import render
return render(request, 'admin/bulk_action_form.html', {
'title': 'Assign Tasks to Cluster',
'queryset': queryset,
'form': ClusterForm(),
'action': 'bulk_assign_cluster',
})
else:
self.message_user(request, 'No clusters available for the selected tasks.', messages.WARNING)
bulk_assign_cluster.short_description = 'Assign to Cluster'
def get_site_display(self, obj):
"""Safely get site name"""
try:
@@ -143,8 +190,13 @@ class ContentAdmin(ExportMixin, SiteSectorAdminMixin, admin.ModelAdmin):
search_fields = ['title', 'content_html', 'external_url']
ordering = ['-created_at']
readonly_fields = ['created_at', 'updated_at', 'word_count', 'get_tags_display', 'get_categories_display']
autocomplete_fields = ['cluster', 'site', 'sector']
inlines = [ContentTaxonomyInline]
actions = ['bulk_set_status_published', 'bulk_set_status_draft']
actions = [
'bulk_set_status_published',
'bulk_set_status_draft',
'bulk_add_taxonomy',
]
fieldsets = (
('Basic Info', {
@@ -208,6 +260,55 @@ class ContentAdmin(ExportMixin, SiteSectorAdminMixin, admin.ModelAdmin):
self.message_user(request, f'{updated} content item(s) set to draft.', messages.SUCCESS)
bulk_set_status_draft.short_description = 'Set status to Draft'
def bulk_add_taxonomy(self, request, queryset):
"""Add taxonomy terms to selected content"""
from django import forms
from igny8_core.business.content.models import ContentTaxonomy, ContentTaxonomyRelation
# If this is the POST request with taxonomy selection
if 'apply' in request.POST:
taxonomy_ids = request.POST.getlist('taxonomies')
if taxonomy_ids:
count = 0
for content in queryset:
for tax_id in taxonomy_ids:
taxonomy = ContentTaxonomy.objects.get(pk=tax_id)
ContentTaxonomyRelation.objects.get_or_create(
content=content,
taxonomy=taxonomy
)
count += 1
self.message_user(request, f'Added {count} taxonomy relation(s) to {queryset.count()} content item(s).', messages.SUCCESS)
return
# Get first content's site/sector for filtering taxonomies
first_content = queryset.first()
if first_content:
taxonomies = ContentTaxonomy.objects.filter(site=first_content.site, sector=first_content.sector)
else:
taxonomies = ContentTaxonomy.objects.all()
# Create form for taxonomy selection
class TaxonomyForm(forms.Form):
taxonomies = forms.ModelMultipleChoiceField(
queryset=taxonomies,
label="Select Taxonomies",
help_text=f"Add taxonomy terms to {queryset.count()} selected content item(s)",
widget=forms.CheckboxSelectMultiple
)
if taxonomies.exists():
from django.shortcuts import render
return render(request, 'admin/bulk_action_form.html', {
'title': 'Add Taxonomies to Content',
'queryset': queryset,
'form': TaxonomyForm(),
'action': 'bulk_add_taxonomy',
})
else:
self.message_user(request, 'No taxonomies available for the selected content.', messages.WARNING)
bulk_add_taxonomy.short_description = 'Add Taxonomy Terms'
def get_site_display(self, obj):
"""Safely get site name"""
try: