old automation cleanup adn status feilds of planner udpate
This commit is contained in:
@@ -318,6 +318,12 @@ class GenerateContentFunction(BaseAIFunction):
|
||||
task.status = 'completed'
|
||||
task.save(update_fields=['status', 'updated_at'])
|
||||
|
||||
# NEW: Auto-sync idea status from task status
|
||||
if hasattr(task, 'idea') and task.idea:
|
||||
task.idea.status = 'completed'
|
||||
task.idea.save(update_fields=['status', 'updated_at'])
|
||||
logger.info(f"Updated related idea ID {task.idea.id} to completed")
|
||||
|
||||
return {
|
||||
'count': 1,
|
||||
'content_id': content_record.id,
|
||||
|
||||
@@ -227,6 +227,11 @@ class GenerateIdeasFunction(BaseAIFunction):
|
||||
sector=cluster.sector,
|
||||
)
|
||||
ideas_created += 1
|
||||
|
||||
# Update cluster status to 'mapped' after ideas are generated
|
||||
if cluster and cluster.status == 'new':
|
||||
cluster.status = 'mapped'
|
||||
cluster.save()
|
||||
|
||||
return {
|
||||
'count': ideas_created,
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
"""
|
||||
Automation business logic - AutomationRule, ScheduledTask models and services
|
||||
"""
|
||||
|
||||
@@ -1,143 +0,0 @@
|
||||
"""
|
||||
Automation Models
|
||||
Phase 2: Automation System
|
||||
"""
|
||||
from django.db import models
|
||||
from django.core.validators import MinValueValidator
|
||||
from igny8_core.auth.models import SiteSectorBaseModel, AccountBaseModel
|
||||
import json
|
||||
|
||||
|
||||
class AutomationRule(SiteSectorBaseModel):
|
||||
"""
|
||||
Automation Rule model for defining automated workflows.
|
||||
|
||||
Rules can be triggered by:
|
||||
- schedule: Time-based triggers (cron-like)
|
||||
- event: Event-based triggers (content created, keyword added, etc.)
|
||||
- manual: Manual execution only
|
||||
"""
|
||||
|
||||
TRIGGER_CHOICES = [
|
||||
('schedule', 'Schedule'),
|
||||
('event', 'Event'),
|
||||
('manual', 'Manual'),
|
||||
]
|
||||
|
||||
STATUS_CHOICES = [
|
||||
('active', 'Active'),
|
||||
('inactive', 'Inactive'),
|
||||
('paused', 'Paused'),
|
||||
]
|
||||
|
||||
name = models.CharField(max_length=255, help_text="Rule name")
|
||||
description = models.TextField(blank=True, null=True, help_text="Rule description")
|
||||
|
||||
# Trigger configuration
|
||||
trigger = models.CharField(max_length=50, choices=TRIGGER_CHOICES, default='manual')
|
||||
|
||||
# Schedule configuration (for schedule triggers)
|
||||
# Stored as cron-like string: "0 0 * * *" (daily at midnight)
|
||||
schedule = models.CharField(
|
||||
max_length=100,
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text="Cron-like schedule string (e.g., '0 0 * * *' for daily at midnight)"
|
||||
)
|
||||
|
||||
# Conditions (JSON field)
|
||||
# Format: [{"field": "content.status", "operator": "equals", "value": "draft"}, ...]
|
||||
conditions = models.JSONField(
|
||||
default=list,
|
||||
help_text="List of conditions that must be met for rule to execute"
|
||||
)
|
||||
|
||||
# Actions (JSON field)
|
||||
# Format: [{"type": "generate_content", "params": {...}}, ...]
|
||||
actions = models.JSONField(
|
||||
default=list,
|
||||
help_text="List of actions to execute when rule triggers"
|
||||
)
|
||||
|
||||
# Status
|
||||
is_active = models.BooleanField(default=True, help_text="Whether rule is active")
|
||||
status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='active')
|
||||
|
||||
# Execution tracking
|
||||
last_executed_at = models.DateTimeField(null=True, blank=True)
|
||||
execution_count = models.IntegerField(default=0, validators=[MinValueValidator(0)])
|
||||
|
||||
# Metadata
|
||||
metadata = models.JSONField(default=dict, help_text="Additional metadata")
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
app_label = 'automation'
|
||||
db_table = 'igny8_automation_rules'
|
||||
ordering = ['-created_at']
|
||||
verbose_name = 'Automation Rule'
|
||||
verbose_name_plural = 'Automation Rules'
|
||||
indexes = [
|
||||
models.Index(fields=['trigger', 'is_active']),
|
||||
models.Index(fields=['status']),
|
||||
models.Index(fields=['site', 'sector']),
|
||||
models.Index(fields=['trigger', 'is_active', 'status']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} ({self.get_trigger_display()})"
|
||||
|
||||
|
||||
class ScheduledTask(AccountBaseModel):
|
||||
"""
|
||||
Scheduled Task model for tracking scheduled automation rule executions.
|
||||
"""
|
||||
|
||||
STATUS_CHOICES = [
|
||||
('pending', 'Pending'),
|
||||
('running', 'Running'),
|
||||
('completed', 'Completed'),
|
||||
('failed', 'Failed'),
|
||||
('cancelled', 'Cancelled'),
|
||||
]
|
||||
|
||||
automation_rule = models.ForeignKey(
|
||||
AutomationRule,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='scheduled_tasks',
|
||||
help_text="The automation rule this task belongs to"
|
||||
)
|
||||
|
||||
scheduled_at = models.DateTimeField(help_text="When the task is scheduled to run")
|
||||
executed_at = models.DateTimeField(null=True, blank=True, help_text="When the task was actually executed")
|
||||
|
||||
status = models.CharField(max_length=50, choices=STATUS_CHOICES, default='pending')
|
||||
|
||||
# Execution results
|
||||
result = models.JSONField(default=dict, help_text="Execution result data")
|
||||
error_message = models.TextField(blank=True, null=True, help_text="Error message if execution failed")
|
||||
|
||||
# Metadata
|
||||
metadata = models.JSONField(default=dict, help_text="Additional metadata")
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
app_label = 'automation'
|
||||
db_table = 'igny8_scheduled_tasks'
|
||||
ordering = ['-scheduled_at']
|
||||
verbose_name = 'Scheduled Task'
|
||||
verbose_name_plural = 'Scheduled Tasks'
|
||||
indexes = [
|
||||
models.Index(fields=['automation_rule', 'status']),
|
||||
models.Index(fields=['scheduled_at', 'status']),
|
||||
models.Index(fields=['account', 'status']),
|
||||
models.Index(fields=['status', 'scheduled_at']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"Scheduled task for {self.automation_rule.name} at {self.scheduled_at}"
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
"""
|
||||
Automation services
|
||||
"""
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
"""
|
||||
Action Executor
|
||||
Executes rule actions
|
||||
"""
|
||||
import logging
|
||||
from igny8_core.business.planning.services.clustering_service import ClusteringService
|
||||
from igny8_core.business.planning.services.ideas_service import IdeasService
|
||||
from igny8_core.business.content.services.content_generation_service import ContentGenerationService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ActionExecutor:
|
||||
"""Executes rule actions"""
|
||||
|
||||
def __init__(self):
|
||||
self.clustering_service = ClusteringService()
|
||||
self.ideas_service = IdeasService()
|
||||
self.content_service = ContentGenerationService()
|
||||
|
||||
def execute(self, action, context, rule):
|
||||
"""
|
||||
Execute a single action.
|
||||
|
||||
Args:
|
||||
action: Action dict with 'type' and 'params'
|
||||
context: Context dict
|
||||
rule: AutomationRule instance
|
||||
|
||||
Returns:
|
||||
dict: Action execution result
|
||||
"""
|
||||
action_type = action.get('type')
|
||||
params = action.get('params', {})
|
||||
|
||||
if action_type == 'cluster_keywords':
|
||||
return self._execute_cluster_keywords(params, rule)
|
||||
elif action_type == 'generate_ideas':
|
||||
return self._execute_generate_ideas(params, rule)
|
||||
elif action_type == 'generate_content':
|
||||
return self._execute_generate_content(params, rule)
|
||||
else:
|
||||
logger.warning(f"Unknown action type: {action_type}")
|
||||
return {
|
||||
'success': False,
|
||||
'error': f'Unknown action type: {action_type}'
|
||||
}
|
||||
|
||||
def _execute_cluster_keywords(self, params, rule):
|
||||
"""Execute cluster keywords action"""
|
||||
keyword_ids = params.get('keyword_ids', [])
|
||||
sector_id = params.get('sector_id') or (rule.sector.id if rule.sector else None)
|
||||
|
||||
try:
|
||||
result = self.clustering_service.cluster_keywords(
|
||||
keyword_ids=keyword_ids,
|
||||
account=rule.account,
|
||||
sector_id=sector_id
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"Error clustering keywords: {str(e)}", exc_info=True)
|
||||
return {
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
def _execute_generate_ideas(self, params, rule):
|
||||
"""Execute generate ideas action"""
|
||||
cluster_ids = params.get('cluster_ids', [])
|
||||
|
||||
try:
|
||||
result = self.ideas_service.generate_ideas(
|
||||
cluster_ids=cluster_ids,
|
||||
account=rule.account
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating ideas: {str(e)}", exc_info=True)
|
||||
return {
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
def _execute_generate_content(self, params, rule):
|
||||
"""Execute generate content action"""
|
||||
task_ids = params.get('task_ids', [])
|
||||
|
||||
try:
|
||||
result = self.content_service.generate_content(
|
||||
task_ids=task_ids,
|
||||
account=rule.account
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"Error generating content: {str(e)}", exc_info=True)
|
||||
return {
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
@@ -1,141 +0,0 @@
|
||||
"""
|
||||
Automation Service
|
||||
Main service for executing automation rules
|
||||
"""
|
||||
import logging
|
||||
from django.utils import timezone
|
||||
from igny8_core.business.automation.models import AutomationRule, ScheduledTask
|
||||
from igny8_core.business.automation.services.rule_engine import RuleEngine
|
||||
from igny8_core.business.billing.services.credit_service import CreditService
|
||||
from igny8_core.business.billing.exceptions import InsufficientCreditsError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AutomationService:
|
||||
"""Service for executing automation rules"""
|
||||
|
||||
def __init__(self):
|
||||
self.rule_engine = RuleEngine()
|
||||
self.credit_service = CreditService()
|
||||
|
||||
def execute_rule(self, rule, context=None):
|
||||
"""
|
||||
Execute an automation rule.
|
||||
|
||||
Args:
|
||||
rule: AutomationRule instance
|
||||
context: Optional context dict for condition evaluation
|
||||
|
||||
Returns:
|
||||
dict: Execution result with status and data
|
||||
"""
|
||||
if not rule.is_active or rule.status != 'active':
|
||||
return {
|
||||
'status': 'skipped',
|
||||
'reason': 'Rule is inactive',
|
||||
'rule_id': rule.id
|
||||
}
|
||||
|
||||
# Check credits (estimate based on actions)
|
||||
estimated_credits = self._estimate_credits(rule)
|
||||
try:
|
||||
self.credit_service.check_credits_legacy(rule.account, estimated_credits)
|
||||
except InsufficientCreditsError as e:
|
||||
logger.warning(f"Rule {rule.id} skipped: {str(e)}")
|
||||
return {
|
||||
'status': 'skipped',
|
||||
'reason': f'Insufficient credits: {str(e)}',
|
||||
'rule_id': rule.id
|
||||
}
|
||||
|
||||
# Execute via rule engine
|
||||
try:
|
||||
result = self.rule_engine.execute(rule, context or {})
|
||||
|
||||
# Update rule tracking
|
||||
rule.last_executed_at = timezone.now()
|
||||
rule.execution_count += 1
|
||||
rule.save(update_fields=['last_executed_at', 'execution_count'])
|
||||
|
||||
return {
|
||||
'status': 'completed',
|
||||
'rule_id': rule.id,
|
||||
'result': result
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Error executing rule {rule.id}: {str(e)}", exc_info=True)
|
||||
return {
|
||||
'status': 'failed',
|
||||
'reason': str(e),
|
||||
'rule_id': rule.id
|
||||
}
|
||||
|
||||
def _estimate_credits(self, rule):
|
||||
"""Estimate credits needed for rule execution"""
|
||||
# Simple estimation based on action types
|
||||
estimated = 0
|
||||
for action in rule.actions:
|
||||
action_type = action.get('type', '')
|
||||
if 'cluster' in action_type:
|
||||
estimated += 10
|
||||
elif 'idea' in action_type:
|
||||
estimated += 15
|
||||
elif 'content' in action_type:
|
||||
estimated += 50 # Conservative estimate
|
||||
else:
|
||||
estimated += 5 # Default
|
||||
return max(estimated, 10) # Minimum 10 credits
|
||||
|
||||
def execute_scheduled_rules(self):
|
||||
"""
|
||||
Execute all scheduled rules that are due.
|
||||
Called by Celery Beat task.
|
||||
|
||||
Returns:
|
||||
dict: Summary of executions
|
||||
"""
|
||||
from django.utils import timezone
|
||||
now = timezone.now()
|
||||
|
||||
# Get active scheduled rules
|
||||
rules = AutomationRule.objects.filter(
|
||||
trigger='schedule',
|
||||
is_active=True,
|
||||
status='active'
|
||||
)
|
||||
|
||||
executed = 0
|
||||
skipped = 0
|
||||
failed = 0
|
||||
|
||||
for rule in rules:
|
||||
# Check if rule should execute based on schedule
|
||||
if self._should_execute_schedule(rule, now):
|
||||
result = self.execute_rule(rule)
|
||||
if result['status'] == 'completed':
|
||||
executed += 1
|
||||
elif result['status'] == 'skipped':
|
||||
skipped += 1
|
||||
else:
|
||||
failed += 1
|
||||
|
||||
return {
|
||||
'executed': executed,
|
||||
'skipped': skipped,
|
||||
'failed': failed,
|
||||
'total': len(rules)
|
||||
}
|
||||
|
||||
def _should_execute_schedule(self, rule, now):
|
||||
"""
|
||||
Check if a scheduled rule should execute now.
|
||||
Simple implementation - can be enhanced with proper cron parsing.
|
||||
"""
|
||||
if not rule.schedule:
|
||||
return False
|
||||
|
||||
# For now, simple check - can be enhanced with cron parser
|
||||
# This is a placeholder - proper implementation would parse cron string
|
||||
return True # Simplified for now
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
"""
|
||||
Condition Evaluator
|
||||
Evaluates rule conditions
|
||||
"""
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ConditionEvaluator:
|
||||
"""Evaluates rule conditions"""
|
||||
|
||||
OPERATORS = {
|
||||
'equals': lambda a, b: a == b,
|
||||
'not_equals': lambda a, b: a != b,
|
||||
'greater_than': lambda a, b: a > b,
|
||||
'greater_than_or_equal': lambda a, b: a >= b,
|
||||
'less_than': lambda a, b: a < b,
|
||||
'less_than_or_equal': lambda a, b: a <= b,
|
||||
'in': lambda a, b: a in b,
|
||||
'contains': lambda a, b: b in a if isinstance(a, str) else a in b,
|
||||
'is_empty': lambda a, b: not a or (isinstance(a, str) and not a.strip()),
|
||||
'is_not_empty': lambda a, b: a and (not isinstance(a, str) or a.strip()),
|
||||
}
|
||||
|
||||
def evaluate(self, conditions, context):
|
||||
"""
|
||||
Evaluate a list of conditions.
|
||||
|
||||
Args:
|
||||
conditions: List of condition dicts
|
||||
context: Context dict for field resolution
|
||||
|
||||
Returns:
|
||||
bool: True if all conditions are met
|
||||
"""
|
||||
if not conditions:
|
||||
return True
|
||||
|
||||
for condition in conditions:
|
||||
if not self._evaluate_condition(condition, context):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _evaluate_condition(self, condition, context):
|
||||
"""
|
||||
Evaluate a single condition.
|
||||
|
||||
Condition format:
|
||||
{
|
||||
"field": "content.status",
|
||||
"operator": "equals",
|
||||
"value": "draft"
|
||||
}
|
||||
"""
|
||||
field_path = condition.get('field')
|
||||
operator = condition.get('operator', 'equals')
|
||||
expected_value = condition.get('value')
|
||||
|
||||
if not field_path:
|
||||
logger.warning("Condition missing 'field'")
|
||||
return False
|
||||
|
||||
# Resolve field value from context
|
||||
actual_value = self._resolve_field(field_path, context)
|
||||
|
||||
# Get operator function
|
||||
op_func = self.OPERATORS.get(operator)
|
||||
if not op_func:
|
||||
logger.warning(f"Unknown operator: {operator}")
|
||||
return False
|
||||
|
||||
# Evaluate
|
||||
try:
|
||||
return op_func(actual_value, expected_value)
|
||||
except Exception as e:
|
||||
logger.error(f"Error evaluating condition: {str(e)}", exc_info=True)
|
||||
return False
|
||||
|
||||
def _resolve_field(self, field_path, context):
|
||||
"""
|
||||
Resolve a field path from context.
|
||||
|
||||
Examples:
|
||||
- "content.status" -> context['content']['status']
|
||||
- "count" -> context['count']
|
||||
"""
|
||||
parts = field_path.split('.')
|
||||
value = context
|
||||
|
||||
for part in parts:
|
||||
if isinstance(value, dict):
|
||||
value = value.get(part)
|
||||
elif hasattr(value, part):
|
||||
value = getattr(value, part)
|
||||
else:
|
||||
return None
|
||||
|
||||
if value is None:
|
||||
return None
|
||||
|
||||
return value
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
"""
|
||||
Rule Engine
|
||||
Orchestrates rule execution
|
||||
"""
|
||||
import logging
|
||||
from igny8_core.business.automation.services.condition_evaluator import ConditionEvaluator
|
||||
from igny8_core.business.automation.services.action_executor import ActionExecutor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RuleEngine:
|
||||
"""Orchestrates rule execution"""
|
||||
|
||||
def __init__(self):
|
||||
self.condition_evaluator = ConditionEvaluator()
|
||||
self.action_executor = ActionExecutor()
|
||||
|
||||
def execute(self, rule, context):
|
||||
"""
|
||||
Execute a rule by evaluating conditions and executing actions.
|
||||
|
||||
Args:
|
||||
rule: AutomationRule instance
|
||||
context: Context dict for evaluation
|
||||
|
||||
Returns:
|
||||
dict: Execution results
|
||||
"""
|
||||
# Evaluate conditions
|
||||
if rule.conditions:
|
||||
conditions_met = self.condition_evaluator.evaluate(rule.conditions, context)
|
||||
if not conditions_met:
|
||||
return {
|
||||
'success': False,
|
||||
'reason': 'Conditions not met'
|
||||
}
|
||||
|
||||
# Execute actions
|
||||
action_results = []
|
||||
for action in rule.actions:
|
||||
try:
|
||||
result = self.action_executor.execute(action, context, rule)
|
||||
action_results.append({
|
||||
'action': action,
|
||||
'success': True,
|
||||
'result': result
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Action execution failed: {str(e)}", exc_info=True)
|
||||
action_results.append({
|
||||
'action': action,
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
})
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'actions': action_results
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
"""
|
||||
Automation Celery Tasks
|
||||
"""
|
||||
from celery import shared_task
|
||||
import logging
|
||||
from igny8_core.business.automation.services.automation_service import AutomationService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@shared_task(name='igny8_core.business.automation.tasks.execute_scheduled_automation_rules')
|
||||
def execute_scheduled_automation_rules():
|
||||
"""
|
||||
Execute all scheduled automation rules.
|
||||
Called by Celery Beat.
|
||||
"""
|
||||
try:
|
||||
service = AutomationService()
|
||||
result = service.execute_scheduled_rules()
|
||||
logger.info(f"Executed scheduled automation rules: {result}")
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"Error executing scheduled automation rules: {str(e)}", exc_info=True)
|
||||
return {
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
@@ -5,12 +5,18 @@ from igny8_core.auth.models import SiteSectorBaseModel, SeedKeyword
|
||||
class Clusters(SiteSectorBaseModel):
|
||||
"""Clusters model for keyword grouping - pure topic clusters"""
|
||||
|
||||
STATUS_CHOICES = [
|
||||
('new', 'New'),
|
||||
('mapped', 'Mapped'),
|
||||
]
|
||||
|
||||
name = models.CharField(max_length=255, unique=True, 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, default='active')
|
||||
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)
|
||||
|
||||
@@ -37,9 +43,8 @@ class Keywords(SiteSectorBaseModel):
|
||||
"""
|
||||
|
||||
STATUS_CHOICES = [
|
||||
('active', 'Active'),
|
||||
('pending', 'Pending'),
|
||||
('archived', 'Archived'),
|
||||
('new', 'New'),
|
||||
('mapped', 'Mapped'),
|
||||
]
|
||||
|
||||
# Required: Link to global SeedKeyword
|
||||
@@ -75,7 +80,8 @@ class Keywords(SiteSectorBaseModel):
|
||||
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='pending')
|
||||
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)
|
||||
|
||||
@@ -142,8 +148,8 @@ class ContentIdeas(SiteSectorBaseModel):
|
||||
|
||||
STATUS_CHOICES = [
|
||||
('new', 'New'),
|
||||
('scheduled', 'Scheduled'),
|
||||
('published', 'Published'),
|
||||
('queued', 'Queued'),
|
||||
('completed', 'Completed'),
|
||||
]
|
||||
|
||||
CONTENT_TYPE_CHOICES = [
|
||||
@@ -193,6 +199,7 @@ class ContentIdeas(SiteSectorBaseModel):
|
||||
)
|
||||
# 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,
|
||||
|
||||
@@ -25,10 +25,6 @@ app.conf.beat_schedule = {
|
||||
'task': 'igny8_core.modules.billing.tasks.replenish_monthly_credits',
|
||||
'schedule': crontab(hour=0, minute=0, day_of_month=1), # First day of month at midnight
|
||||
},
|
||||
'execute-scheduled-automation-rules': {
|
||||
'task': 'igny8_core.business.automation.tasks.execute_scheduled_automation_rules',
|
||||
'schedule': crontab(minute='*/5'), # Every 5 minutes
|
||||
},
|
||||
# WordPress Publishing Tasks
|
||||
'process-pending-wordpress-publications': {
|
||||
'task': 'igny8_core.tasks.wordpress_publishing.process_pending_wordpress_publications',
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
"""
|
||||
Automation Module - API Layer
|
||||
Business logic is in business/automation/
|
||||
"""
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
"""
|
||||
Automation App Configuration
|
||||
"""
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AutomationConfig(AppConfig):
|
||||
"""Configuration for automation module"""
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'igny8_core.modules.automation'
|
||||
label = 'automation'
|
||||
verbose_name = 'Automation'
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
# Generated by Django 5.2.8 on 2025-11-20 23:27
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='AutomationRule',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(help_text='Rule name', max_length=255)),
|
||||
('description', models.TextField(blank=True, help_text='Rule description', null=True)),
|
||||
('trigger', models.CharField(choices=[('schedule', 'Schedule'), ('event', 'Event'), ('manual', 'Manual')], default='manual', max_length=50)),
|
||||
('schedule', models.CharField(blank=True, help_text="Cron-like schedule string (e.g., '0 0 * * *' for daily at midnight)", max_length=100, null=True)),
|
||||
('conditions', models.JSONField(default=list, help_text='List of conditions that must be met for rule to execute')),
|
||||
('actions', models.JSONField(default=list, help_text='List of actions to execute when rule triggers')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Whether rule is active')),
|
||||
('status', models.CharField(choices=[('active', 'Active'), ('inactive', 'Inactive'), ('paused', 'Paused')], default='active', max_length=50)),
|
||||
('last_executed_at', models.DateTimeField(blank=True, null=True)),
|
||||
('execution_count', models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(0)])),
|
||||
('metadata', models.JSONField(default=dict, help_text='Additional metadata')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Automation Rule',
|
||||
'verbose_name_plural': 'Automation Rules',
|
||||
'db_table': 'igny8_automation_rules',
|
||||
'ordering': ['-created_at'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ScheduledTask',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('scheduled_at', models.DateTimeField(help_text='When the task is scheduled to run')),
|
||||
('executed_at', models.DateTimeField(blank=True, help_text='When the task was actually executed', null=True)),
|
||||
('status', models.CharField(choices=[('pending', 'Pending'), ('running', 'Running'), ('completed', 'Completed'), ('failed', 'Failed'), ('cancelled', 'Cancelled')], default='pending', max_length=50)),
|
||||
('result', models.JSONField(default=dict, help_text='Execution result data')),
|
||||
('error_message', models.TextField(blank=True, help_text='Error message if execution failed', null=True)),
|
||||
('metadata', models.JSONField(default=dict, help_text='Additional metadata')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Scheduled Task',
|
||||
'verbose_name_plural': 'Scheduled Tasks',
|
||||
'db_table': 'igny8_scheduled_tasks',
|
||||
'ordering': ['-scheduled_at'],
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -1,74 +0,0 @@
|
||||
# Generated by Django 5.2.8 on 2025-11-20 23:27
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('automation', '0001_initial'),
|
||||
('igny8_core_auth', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='automationrule',
|
||||
name='account',
|
||||
field=models.ForeignKey(db_column='tenant_id', on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_set', to='igny8_core_auth.account'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='automationrule',
|
||||
name='sector',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_set', to='igny8_core_auth.sector'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='automationrule',
|
||||
name='site',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_set', to='igny8_core_auth.site'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='scheduledtask',
|
||||
name='account',
|
||||
field=models.ForeignKey(db_column='tenant_id', on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_set', to='igny8_core_auth.account'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='scheduledtask',
|
||||
name='automation_rule',
|
||||
field=models.ForeignKey(help_text='The automation rule this task belongs to', on_delete=django.db.models.deletion.CASCADE, related_name='scheduled_tasks', to='automation.automationrule'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='automationrule',
|
||||
index=models.Index(fields=['trigger', 'is_active'], name='igny8_autom_trigger_32979f_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='automationrule',
|
||||
index=models.Index(fields=['status'], name='igny8_autom_status_827c0d_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='automationrule',
|
||||
index=models.Index(fields=['site', 'sector'], name='igny8_autom_site_id_d0a51d_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='automationrule',
|
||||
index=models.Index(fields=['trigger', 'is_active', 'status'], name='igny8_autom_trigger_f3f3e2_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='scheduledtask',
|
||||
index=models.Index(fields=['automation_rule', 'status'], name='igny8_sched_automat_da6c85_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='scheduledtask',
|
||||
index=models.Index(fields=['scheduled_at', 'status'], name='igny8_sched_schedul_1e3342_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='scheduledtask',
|
||||
index=models.Index(fields=['account', 'status'], name='igny8_sched_tenant__7244a8_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='scheduledtask',
|
||||
index=models.Index(fields=['status', 'scheduled_at'], name='igny8_sched_status_21f32f_idx'),
|
||||
),
|
||||
]
|
||||
@@ -1,5 +0,0 @@
|
||||
# Backward compatibility alias - models moved to business/automation/
|
||||
from igny8_core.business.automation.models import AutomationRule, ScheduledTask
|
||||
|
||||
__all__ = ['AutomationRule', 'ScheduledTask']
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
"""
|
||||
Serializers for Automation Models
|
||||
"""
|
||||
from rest_framework import serializers
|
||||
from igny8_core.business.automation.models import AutomationRule, ScheduledTask
|
||||
|
||||
|
||||
class AutomationRuleSerializer(serializers.ModelSerializer):
|
||||
"""Serializer for AutomationRule model"""
|
||||
|
||||
class Meta:
|
||||
model = AutomationRule
|
||||
fields = [
|
||||
'id', 'name', 'description', 'trigger', 'schedule',
|
||||
'conditions', 'actions', 'is_active', 'status',
|
||||
'last_executed_at', 'execution_count',
|
||||
'metadata', 'created_at', 'updated_at',
|
||||
'account', 'site', 'sector'
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at', 'last_executed_at', 'execution_count']
|
||||
|
||||
|
||||
class ScheduledTaskSerializer(serializers.ModelSerializer):
|
||||
"""Serializer for ScheduledTask model"""
|
||||
automation_rule_name = serializers.CharField(source='automation_rule.name', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = ScheduledTask
|
||||
fields = [
|
||||
'id', 'automation_rule', 'automation_rule_name',
|
||||
'scheduled_at', 'executed_at', 'status',
|
||||
'result', 'error_message', 'metadata',
|
||||
'created_at', 'updated_at', 'account'
|
||||
]
|
||||
read_only_fields = ['id', 'created_at', 'updated_at', 'executed_at']
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
"""
|
||||
URL patterns for automation module.
|
||||
"""
|
||||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from .views import AutomationRuleViewSet, ScheduledTaskViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'rules', AutomationRuleViewSet, basename='automation-rule')
|
||||
router.register(r'scheduled-tasks', ScheduledTaskViewSet, basename='scheduled-task')
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
]
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
"""
|
||||
ViewSets for Automation Models
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
from rest_framework import viewsets, status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from rest_framework import filters
|
||||
from drf_spectacular.utils import extend_schema, extend_schema_view
|
||||
from igny8_core.api.base import SiteSectorModelViewSet, AccountModelViewSet
|
||||
from igny8_core.api.pagination import CustomPageNumberPagination
|
||||
from igny8_core.api.response import success_response, error_response
|
||||
from igny8_core.api.throttles import DebugScopedRateThrottle
|
||||
from igny8_core.api.permissions import IsAuthenticatedAndActive, IsViewerOrAbove
|
||||
from igny8_core.business.automation.models import AutomationRule, ScheduledTask
|
||||
from igny8_core.business.automation.services.automation_service import AutomationService
|
||||
from .serializers import AutomationRuleSerializer, ScheduledTaskSerializer
|
||||
|
||||
|
||||
@extend_schema_view(
|
||||
list=extend_schema(tags=['Automation']),
|
||||
create=extend_schema(tags=['Automation']),
|
||||
retrieve=extend_schema(tags=['Automation']),
|
||||
update=extend_schema(tags=['Automation']),
|
||||
partial_update=extend_schema(tags=['Automation']),
|
||||
destroy=extend_schema(tags=['Automation']),
|
||||
)
|
||||
class AutomationRuleViewSet(SiteSectorModelViewSet):
|
||||
"""
|
||||
ViewSet for managing automation rules
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
queryset = AutomationRule.objects.all()
|
||||
serializer_class = AutomationRuleSerializer
|
||||
permission_classes = [IsAuthenticatedAndActive, IsViewerOrAbove]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
throttle_scope = 'automation'
|
||||
throttle_classes = [DebugScopedRateThrottle]
|
||||
|
||||
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
|
||||
search_fields = ['name', 'description']
|
||||
ordering_fields = ['name', 'created_at', 'last_executed_at', 'execution_count']
|
||||
ordering = ['-created_at']
|
||||
filterset_fields = ['trigger', 'is_active', 'status']
|
||||
|
||||
@action(detail=True, methods=['post'], url_path='execute', url_name='execute')
|
||||
def execute(self, request, pk=None):
|
||||
"""Manually execute an automation rule"""
|
||||
rule = self.get_object()
|
||||
service = AutomationService()
|
||||
|
||||
try:
|
||||
result = service.execute_rule(rule, context=request.data.get('context', {}))
|
||||
return success_response(
|
||||
data=result,
|
||||
message='Rule executed successfully',
|
||||
request=request
|
||||
)
|
||||
except Exception as e:
|
||||
return error_response(
|
||||
error=str(e),
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
request=request
|
||||
)
|
||||
|
||||
|
||||
@extend_schema_view(
|
||||
list=extend_schema(tags=['Automation']),
|
||||
create=extend_schema(tags=['Automation']),
|
||||
retrieve=extend_schema(tags=['Automation']),
|
||||
update=extend_schema(tags=['Automation']),
|
||||
partial_update=extend_schema(tags=['Automation']),
|
||||
destroy=extend_schema(tags=['Automation']),
|
||||
)
|
||||
class ScheduledTaskViewSet(AccountModelViewSet):
|
||||
"""
|
||||
ViewSet for managing scheduled tasks
|
||||
Unified API Standard v1.0 compliant
|
||||
"""
|
||||
queryset = ScheduledTask.objects.select_related('automation_rule')
|
||||
serializer_class = ScheduledTaskSerializer
|
||||
permission_classes = [IsAuthenticatedAndActive, IsViewerOrAbove]
|
||||
pagination_class = CustomPageNumberPagination
|
||||
throttle_scope = 'automation'
|
||||
throttle_classes = [DebugScopedRateThrottle]
|
||||
|
||||
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
|
||||
ordering_fields = ['scheduled_at', 'executed_at', 'status', 'created_at']
|
||||
ordering = ['-scheduled_at']
|
||||
filterset_fields = ['automation_rule', 'status']
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
# Generated migration for unified status refactor
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def migrate_status_data(apps, schema_editor):
|
||||
"""Transform existing status data to new values"""
|
||||
Keywords = apps.get_model('planner', 'Keywords')
|
||||
Clusters = apps.get_model('planner', 'Clusters')
|
||||
ContentIdeas = apps.get_model('planner', 'ContentIdeas')
|
||||
|
||||
# Keywords: pending→new, active→mapped, archived→mapped+disabled=true
|
||||
Keywords.objects.filter(status='pending').update(status='new')
|
||||
Keywords.objects.filter(status='active').update(status='mapped')
|
||||
# Handle archived: set to mapped and mark as disabled
|
||||
archived_keywords = Keywords.objects.filter(status='archived')
|
||||
for kw in archived_keywords:
|
||||
kw.status = 'mapped'
|
||||
kw.disabled = True
|
||||
kw.save()
|
||||
|
||||
# Clusters: active (with ideas)→mapped, active (no ideas)→new
|
||||
# Check if cluster has any related ideas using the reverse relationship
|
||||
for cluster in Clusters.objects.all():
|
||||
if cluster.ideas.exists():
|
||||
cluster.status = 'mapped'
|
||||
else:
|
||||
cluster.status = 'new'
|
||||
cluster.save()
|
||||
|
||||
# ContentIdeas: scheduled→queued, published→completed, new stays new
|
||||
ContentIdeas.objects.filter(status='scheduled').update(status='queued')
|
||||
ContentIdeas.objects.filter(status='published').update(status='completed')
|
||||
|
||||
|
||||
def reverse_status_data(apps, schema_editor):
|
||||
"""Reverse migration: restore old status values"""
|
||||
Keywords = apps.get_model('planner', 'Keywords')
|
||||
Clusters = apps.get_model('planner', 'Clusters')
|
||||
ContentIdeas = apps.get_model('planner', 'ContentIdeas')
|
||||
|
||||
# Keywords: new→pending, mapped→active (or archived if disabled)
|
||||
Keywords.objects.filter(status='new').update(status='pending')
|
||||
Keywords.objects.filter(status='mapped', disabled=False).update(status='active')
|
||||
Keywords.objects.filter(status='mapped', disabled=True).update(status='archived', disabled=False)
|
||||
|
||||
# Clusters: all back to 'active'
|
||||
Clusters.objects.all().update(status='active')
|
||||
|
||||
# ContentIdeas: queued→scheduled, completed→published
|
||||
ContentIdeas.objects.filter(status='queued').update(status='scheduled')
|
||||
ContentIdeas.objects.filter(status='completed').update(status='published')
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('planner', '0005_field_rename_implementation'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
# Step 1: Add disabled field to all models (with default=False)
|
||||
migrations.AddField(
|
||||
model_name='clusters',
|
||||
name='disabled',
|
||||
field=models.BooleanField(default=False, help_text='Exclude from processes'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='keywords',
|
||||
name='disabled',
|
||||
field=models.BooleanField(default=False, help_text='Exclude from processes'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='contentideas',
|
||||
name='disabled',
|
||||
field=models.BooleanField(default=False, help_text='Exclude from processes'),
|
||||
),
|
||||
|
||||
# Step 2: Alter Keywords status field choices
|
||||
migrations.AlterField(
|
||||
model_name='keywords',
|
||||
name='status',
|
||||
field=models.CharField(
|
||||
choices=[('new', 'New'), ('mapped', 'Mapped')],
|
||||
default='new',
|
||||
max_length=50
|
||||
),
|
||||
),
|
||||
|
||||
# Step 3: Alter Clusters status field (add choices, change default)
|
||||
migrations.AlterField(
|
||||
model_name='clusters',
|
||||
name='status',
|
||||
field=models.CharField(
|
||||
choices=[('new', 'New'), ('mapped', 'Mapped')],
|
||||
default='new',
|
||||
max_length=50
|
||||
),
|
||||
),
|
||||
|
||||
# Step 4: Alter ContentIdeas status field choices
|
||||
migrations.AlterField(
|
||||
model_name='contentideas',
|
||||
name='status',
|
||||
field=models.CharField(
|
||||
choices=[('new', 'New'), ('queued', 'Queued'), ('completed', 'Completed')],
|
||||
default='new',
|
||||
max_length=50
|
||||
),
|
||||
),
|
||||
|
||||
# Step 5: Data migration - transform existing records
|
||||
migrations.RunPython(migrate_status_data, reverse_status_data),
|
||||
]
|
||||
@@ -560,7 +560,7 @@ class KeywordViewSet(SiteSectorModelViewSet):
|
||||
volume=int(row.get('volume', 0) or 0),
|
||||
difficulty=int(row.get('difficulty', 0) or 0),
|
||||
intent=row.get('intent', 'informational') or 'informational',
|
||||
status=row.get('status', 'pending') or 'pending',
|
||||
status=row.get('status', 'new') or 'new',
|
||||
site=site,
|
||||
sector=sector,
|
||||
account=account
|
||||
@@ -1080,8 +1080,8 @@ class ContentIdeasViewSet(SiteSectorModelViewSet):
|
||||
|
||||
created_tasks.append(task.id)
|
||||
|
||||
# Update idea status
|
||||
idea.status = 'scheduled'
|
||||
# Update idea status to queued
|
||||
idea.status = 'queued'
|
||||
idea.save()
|
||||
except Exception as e:
|
||||
errors.append({
|
||||
|
||||
@@ -52,7 +52,7 @@ INSTALLED_APPS = [
|
||||
'igny8_core.modules.writer.apps.WriterConfig',
|
||||
'igny8_core.modules.system.apps.SystemConfig',
|
||||
'igny8_core.modules.billing.apps.BillingConfig',
|
||||
'igny8_core.modules.automation.apps.AutomationConfig',
|
||||
# 'igny8_core.modules.automation.apps.AutomationConfig', # Removed - automation module disabled
|
||||
# 'igny8_core.business.site_building.apps.SiteBuildingConfig', # REMOVED: SiteBuilder/Blueprint deprecated
|
||||
'igny8_core.business.optimization.apps.OptimizationConfig',
|
||||
'igny8_core.business.publishing.apps.PublishingConfig',
|
||||
|
||||
@@ -42,7 +42,7 @@ urlpatterns = [
|
||||
# Site Builder module removed - legacy blueprint functionality deprecated
|
||||
path('api/v1/system/', include('igny8_core.modules.system.urls')),
|
||||
path('api/v1/billing/', include('igny8_core.modules.billing.urls')), # Billing endpoints
|
||||
path('api/v1/automation/', include('igny8_core.modules.automation.urls')), # Automation endpoints
|
||||
# path('api/v1/automation/', include('igny8_core.modules.automation.urls')), # Automation endpoints - REMOVED
|
||||
path('api/v1/linker/', include('igny8_core.modules.linker.urls')), # Linker endpoints
|
||||
path('api/v1/optimizer/', include('igny8_core.modules.optimizer.urls')), # Optimizer endpoints
|
||||
path('api/v1/publisher/', include('igny8_core.modules.publisher.urls')), # Publisher endpoints
|
||||
|
||||
Reference in New Issue
Block a user