- Updated AutomationService to include estimated_word_count. - Increased stage_1_batch_size from 20 to 50 in AutomationViewSet. - Changed Keywords model to replace 'intent' property with 'country'. - Adjusted ClusteringService to allow a maximum of 50 keywords for clustering. - Modified admin and management commands to remove 'intent' and use 'country' instead. - Updated serializers to reflect the change from 'intent' to 'country'. - Adjusted views and filters to use 'country' instead of 'intent'. - Updated frontend forms, filters, and pages to replace 'intent' with 'country'. - Added migration to remove 'intent' field and add 'country' field to SeedKeyword model.
281 lines
10 KiB
Python
281 lines
10 KiB
Python
from django.db import models
|
|
from igny8_core.auth.models import SiteSectorBaseModel, SeedKeyword
|
|
from igny8_core.common.soft_delete import SoftDeletableModel, SoftDeleteManager
|
|
|
|
|
|
class Clusters(SoftDeletableModel, SiteSectorBaseModel):
|
|
"""Clusters model for keyword grouping - pure topic clusters"""
|
|
|
|
STATUS_CHOICES = [
|
|
('new', 'New'),
|
|
('mapped', 'Mapped'),
|
|
]
|
|
|
|
name = models.CharField(max_length=255, db_index=True)
|
|
description = models.TextField(blank=True, null=True)
|
|
keywords_count = models.IntegerField(default=0)
|
|
volume = models.IntegerField(default=0)
|
|
mapped_pages = models.IntegerField(default=0)
|
|
status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='new')
|
|
disabled = models.BooleanField(default=False, help_text="Exclude from processes")
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
app_label = 'planner'
|
|
db_table = 'igny8_clusters'
|
|
ordering = ['name']
|
|
verbose_name = 'Cluster'
|
|
verbose_name_plural = 'Clusters'
|
|
unique_together = [['name', 'site', 'sector']] # Unique per site/sector
|
|
indexes = [
|
|
models.Index(fields=['name']),
|
|
models.Index(fields=['status']),
|
|
models.Index(fields=['site', 'sector']),
|
|
]
|
|
|
|
objects = SoftDeleteManager()
|
|
all_objects = models.Manager()
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
|
|
class Keywords(SoftDeletableModel, SiteSectorBaseModel):
|
|
"""
|
|
Keywords model for SEO keyword management.
|
|
Site-specific instances that reference global SeedKeywords.
|
|
"""
|
|
|
|
STATUS_CHOICES = [
|
|
('new', 'New'),
|
|
('mapped', 'Mapped'),
|
|
]
|
|
|
|
# Required: Link to global SeedKeyword
|
|
seed_keyword = models.ForeignKey(
|
|
SeedKeyword,
|
|
on_delete=models.PROTECT, # Prevent deletion if Keywords reference it
|
|
related_name='site_keywords',
|
|
help_text="Reference to the global seed keyword"
|
|
)
|
|
|
|
# Site-specific overrides (optional)
|
|
volume_override = models.IntegerField(
|
|
null=True,
|
|
blank=True,
|
|
help_text="Site-specific volume override (uses seed_keyword.volume if not set)"
|
|
)
|
|
difficulty_override = models.IntegerField(
|
|
null=True,
|
|
blank=True,
|
|
help_text="Site-specific difficulty override (uses seed_keyword.difficulty if not set)"
|
|
)
|
|
attribute_values = models.JSONField(
|
|
default=list,
|
|
blank=True,
|
|
help_text="Optional attribute metadata (e.g., product specs, service modifiers)"
|
|
)
|
|
|
|
cluster = models.ForeignKey(
|
|
'Clusters',
|
|
on_delete=models.SET_NULL,
|
|
null=True,
|
|
blank=True,
|
|
related_name='keywords',
|
|
limit_choices_to={'sector': models.F('sector')} # Cluster must be in same sector
|
|
)
|
|
status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='new')
|
|
disabled = models.BooleanField(default=False, help_text="Exclude from processes")
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
app_label = 'planner'
|
|
db_table = 'igny8_keywords'
|
|
ordering = ['-created_at']
|
|
verbose_name = 'Keyword'
|
|
verbose_name_plural = 'Keywords'
|
|
unique_together = [['seed_keyword', 'site', 'sector']] # One keyword per site/sector
|
|
indexes = [
|
|
models.Index(fields=['seed_keyword']),
|
|
models.Index(fields=['status']),
|
|
models.Index(fields=['cluster']),
|
|
models.Index(fields=['site', 'sector']),
|
|
models.Index(fields=['seed_keyword', 'site', 'sector']),
|
|
]
|
|
|
|
objects = SoftDeleteManager()
|
|
all_objects = models.Manager()
|
|
|
|
def soft_delete(self, user=None, reason=None, retention_days=None):
|
|
"""Override soft_delete to clear seed_keyword FK to prevent PROTECT issues"""
|
|
# Clear the seed_keyword FK before soft delete to prevent cascade protection issues
|
|
# This allows SeedKeywords to be managed independently after Keywords are deleted
|
|
self.seed_keyword = None
|
|
super().soft_delete(user=user, reason=reason, retention_days=retention_days)
|
|
|
|
@property
|
|
def keyword(self):
|
|
"""Get keyword text from seed_keyword"""
|
|
return self.seed_keyword.keyword if self.seed_keyword else ''
|
|
|
|
@property
|
|
def volume(self):
|
|
"""Get volume from override or seed_keyword"""
|
|
return self.volume_override if self.volume_override is not None else (self.seed_keyword.volume if self.seed_keyword else 0)
|
|
|
|
@property
|
|
def difficulty(self):
|
|
"""Get difficulty from override or seed_keyword"""
|
|
return self.difficulty_override if self.difficulty_override is not None else (self.seed_keyword.difficulty if self.seed_keyword else 0)
|
|
|
|
@property
|
|
def country(self):
|
|
"""Get country from seed_keyword"""
|
|
return self.seed_keyword.country if self.seed_keyword else 'US'
|
|
|
|
def save(self, *args, **kwargs):
|
|
"""Validate that seed_keyword's industry/sector matches site's industry/sector"""
|
|
if self.seed_keyword and self.site and self.sector:
|
|
# Validate industry match
|
|
if self.site.industry != self.seed_keyword.industry:
|
|
from django.core.exceptions import ValidationError
|
|
raise ValidationError(
|
|
f"SeedKeyword industry ({self.seed_keyword.industry.name}) must match site industry ({self.site.industry.name})"
|
|
)
|
|
|
|
# Validate sector match (site sector's industry_sector must match seed_keyword's sector)
|
|
if self.sector.industry_sector != self.seed_keyword.sector:
|
|
from django.core.exceptions import ValidationError
|
|
raise ValidationError(
|
|
f"SeedKeyword sector ({self.seed_keyword.sector.name}) must match site sector's industry sector ({self.sector.industry_sector.name if self.sector.industry_sector else 'None'})"
|
|
)
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
def __str__(self):
|
|
return self.keyword
|
|
|
|
|
|
class ContentIdeas(SoftDeletableModel, SiteSectorBaseModel):
|
|
"""Content Ideas model for planning content based on keyword clusters"""
|
|
|
|
STATUS_CHOICES = [
|
|
('new', 'New'),
|
|
('queued', 'Queued'),
|
|
('completed', 'Completed'),
|
|
]
|
|
|
|
CONTENT_TYPE_CHOICES = [
|
|
('post', 'Post'),
|
|
('page', 'Page'),
|
|
('product', 'Product'),
|
|
('taxonomy', 'Taxonomy'),
|
|
]
|
|
|
|
CONTENT_STRUCTURE_CHOICES = [
|
|
# Post structures
|
|
('article', 'Article'),
|
|
('guide', 'Guide'),
|
|
('comparison', 'Comparison'),
|
|
('review', 'Review'),
|
|
('listicle', 'Listicle'),
|
|
# Page structures
|
|
('landing_page', 'Landing Page'),
|
|
('business_page', 'Business Page'),
|
|
('service_page', 'Service Page'),
|
|
('general', 'General'),
|
|
('cluster_hub', 'Cluster Hub'),
|
|
# Product structures
|
|
('product_page', 'Product Page'),
|
|
# Taxonomy structures
|
|
('category_archive', 'Category Archive'),
|
|
('tag_archive', 'Tag Archive'),
|
|
('attribute_archive', 'Attribute Archive'),
|
|
]
|
|
|
|
idea_title = models.CharField(max_length=255, db_index=True)
|
|
description = models.TextField(blank=True, null=True)
|
|
target_keywords = models.CharField(max_length=500, blank=True) # Comma-separated keywords (legacy)
|
|
keyword_objects = models.ManyToManyField(
|
|
'Keywords',
|
|
blank=True,
|
|
related_name='content_ideas',
|
|
help_text="Individual keywords linked to this content idea"
|
|
)
|
|
keyword_cluster = models.ForeignKey(
|
|
Clusters,
|
|
on_delete=models.SET_NULL,
|
|
null=True,
|
|
blank=True,
|
|
related_name='ideas',
|
|
limit_choices_to={'sector': models.F('sector')}
|
|
)
|
|
# REMOVED: taxonomy FK to SiteBlueprintTaxonomy (legacy blueprint functionality)
|
|
status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='new')
|
|
disabled = models.BooleanField(default=False, help_text="Exclude from processes")
|
|
estimated_word_count = models.IntegerField(default=1000)
|
|
content_type = models.CharField(
|
|
max_length=50,
|
|
choices=CONTENT_TYPE_CHOICES,
|
|
default='post',
|
|
help_text="Content type: post, page, product, taxonomy"
|
|
)
|
|
content_structure = models.CharField(
|
|
max_length=50,
|
|
choices=CONTENT_STRUCTURE_CHOICES,
|
|
default='article',
|
|
help_text="Content structure/format based on content type"
|
|
)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
app_label = 'planner'
|
|
db_table = 'igny8_content_ideas'
|
|
ordering = ['-created_at']
|
|
verbose_name = 'Content Idea'
|
|
verbose_name_plural = 'Content Ideas'
|
|
indexes = [
|
|
models.Index(fields=['idea_title']),
|
|
models.Index(fields=['status']),
|
|
models.Index(fields=['keyword_cluster']),
|
|
models.Index(fields=['content_type']),
|
|
models.Index(fields=['content_structure']),
|
|
models.Index(fields=['site', 'sector']),
|
|
]
|
|
|
|
objects = SoftDeleteManager()
|
|
all_objects = models.Manager()
|
|
|
|
def save(self, *args, **kwargs):
|
|
"""Track content ideas usage when creating new ideas"""
|
|
is_new = self.pk is None
|
|
super().save(*args, **kwargs)
|
|
|
|
# Increment usage for new content ideas
|
|
if is_new:
|
|
from igny8_core.business.billing.services.limit_service import LimitService
|
|
try:
|
|
account = self.site.account if self.site else self.account
|
|
if account:
|
|
LimitService.increment_usage(
|
|
account=account,
|
|
limit_type='content_ideas',
|
|
amount=1,
|
|
metadata={
|
|
'idea_id': self.id,
|
|
'idea_title': self.idea_title,
|
|
'site_id': self.site.id if self.site else None,
|
|
}
|
|
)
|
|
except Exception as e:
|
|
import logging
|
|
logger = logging.getLogger(__name__)
|
|
logger.error(f"Error incrementing content ideas usage for idea {self.id}: {str(e)}")
|
|
|
|
def __str__(self):
|
|
return self.idea_title
|
|
|