2
This commit is contained in:
@@ -49,6 +49,10 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
|
||||
return {"success": True, "message": "Already published", "external_id": content.external_id}
|
||||
|
||||
logger.info(f"[publish_content_to_wordpress] 📦 Preparing content payload...")
|
||||
logger.info(f"[publish_content_to_wordpress] Content title: '{content.title}'")
|
||||
logger.info(f"[publish_content_to_wordpress] Content status: '{content.status}'")
|
||||
logger.info(f"[publish_content_to_wordpress] Content type: '{content.content_type}'")
|
||||
|
||||
# Prepare content data for WordPress
|
||||
# Generate excerpt from content_html (Content model has no 'brief' field)
|
||||
excerpt = ''
|
||||
@@ -57,10 +61,14 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
|
||||
excerpt = strip_tags(content.content_html)[:150].strip()
|
||||
if len(content.content_html) > 150:
|
||||
excerpt += '...'
|
||||
logger.info(f"[publish_content_to_wordpress] Content HTML length: {len(content.content_html)} chars")
|
||||
else:
|
||||
logger.warning(f"[publish_content_to_wordpress] ⚠️ No content_html found!")
|
||||
|
||||
# Get taxonomy terms from ContentTaxonomyMap
|
||||
from igny8_core.business.content.models import ContentTaxonomyMap
|
||||
taxonomy_maps = ContentTaxonomyMap.objects.filter(content=content).select_related('taxonomy')
|
||||
logger.info(f"[publish_content_to_wordpress] Found {taxonomy_maps.count()} taxonomy mappings")
|
||||
|
||||
# Build categories and tags arrays from taxonomy mappings
|
||||
categories = []
|
||||
@@ -70,6 +78,7 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
|
||||
if tax:
|
||||
# Add taxonomy term name to categories (will be mapped in WordPress)
|
||||
categories.append(tax.name)
|
||||
logger.info(f"[publish_content_to_wordpress] 📁 Added category: '{tax.name}'")
|
||||
|
||||
# Get images from Images model
|
||||
from igny8_core.modules.writer.models import Images
|
||||
@@ -77,27 +86,40 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
|
||||
gallery_images = []
|
||||
|
||||
images = Images.objects.filter(content=content).order_by('position')
|
||||
logger.info(f"[publish_content_to_wordpress] Found {images.count()} images for content")
|
||||
|
||||
for image in images:
|
||||
if image.image_type == 'featured' and image.image_url:
|
||||
featured_image_url = image.image_url
|
||||
logger.info(f"[publish_content_to_wordpress] 🖼️ Featured image: {image.image_url[:100]}")
|
||||
elif image.image_type == 'in_article' and image.image_url:
|
||||
gallery_images.append({
|
||||
'url': image.image_url,
|
||||
'alt': image.alt_text or '',
|
||||
'position': image.position
|
||||
})
|
||||
|
||||
# Add primary and secondary keywords as tags
|
||||
if content.primary_keyword:
|
||||
tags.append(content.primary_keyword)
|
||||
logger.info(f"[publish_content_to_wordpress] 🏷️ Primary keyword (tag): '{content.primary_keyword}'")
|
||||
else:
|
||||
logger.info(f"[publish_content_to_wordpress] No primary keyword found")
|
||||
|
||||
if content.secondary_keywords:
|
||||
if isinstance(content.secondary_keywords, list):
|
||||
tags.extend(content.secondary_keywords)
|
||||
logger.info(f"[publish_content_to_wordpress] 🏷️ Added {len(content.secondary_keywords)} secondary keywords as tags")
|
||||
elif isinstance(content.secondary_keywords, str):
|
||||
import json
|
||||
try:
|
||||
keywords = json.loads(content.secondary_keywords)
|
||||
if isinstance(keywords, list):
|
||||
tags.extend(keywords)
|
||||
logger.info(f"[publish_content_to_wordpress] 🏷️ Added {len(keywords)} secondary keywords as tags (from JSON)")
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
logger.warning(f"[publish_content_to_wordpress] Failed to parse secondary_keywords as JSON")
|
||||
else:
|
||||
logger.info(f"[publish_content_to_wordpress] No secondary keywords found")
|
||||
|
||||
logger.info(f"[publish_content_to_wordpress] 📊 TOTAL: {len(categories)} categories, {len(tags)} tags")ords = json.loads(content.secondary_keywords)
|
||||
if isinstance(keywords, list):
|
||||
tags.extend(keywords)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
@@ -132,21 +154,36 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
|
||||
# Keep for backward compatibility
|
||||
'sectors': [],
|
||||
'clusters': []
|
||||
}
|
||||
|
||||
# Call WordPress REST API
|
||||
wordpress_url = f"{site_integration.site_url}/wp-json/igny8/v1/publish-content/"
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-IGNY8-API-KEY': site_integration.api_key,
|
||||
}
|
||||
|
||||
logger.info(f"[publish_content_to_wordpress] 🚀 POSTing to WordPress: {wordpress_url}")
|
||||
logger.info(f"[publish_content_to_wordpress] 📦 Payload summary:")
|
||||
logger.info(f" - Categories: {categories}")
|
||||
logger.info(f" - Tags: {tags}")
|
||||
logger.info(f" - Featured image: {'Yes' if featured_image_url else 'No'}")
|
||||
logger.info(f" - Gallery images: {len(gallery_images)}")
|
||||
logger.info(f" - SEO title: {'Yes' if content_data.get('seo_title') else 'No'}")
|
||||
logger.info(f" - SEO description: {'Yes' if content_data.get('seo_description') else 'No'}")
|
||||
|
||||
response = requests.post(
|
||||
wordpress_url,
|
||||
json=content_data,
|
||||
headers=headers,
|
||||
timeout=30
|
||||
# Update external_id and external_url for unified Content model
|
||||
old_status = content.status
|
||||
content.external_id = wp_data.get('post_id')
|
||||
content.external_url = wp_data.get('post_url')
|
||||
content.status = 'published'
|
||||
content.save(update_fields=[
|
||||
'external_id', 'external_url', 'status', 'updated_at'
|
||||
])
|
||||
logger.info(f"[publish_content_to_wordpress] 💾 Content model updated:")
|
||||
logger.info(f" - Status: '{old_status}' → 'published'")
|
||||
logger.info(f" - External ID: {content.external_id}")
|
||||
logger.info(f" - External URL: {content.external_url}")
|
||||
wordpress_url,
|
||||
json=content_data,
|
||||
headers=headers,
|
||||
timeout=30
|
||||
)
|
||||
logger.info(f"[publish_content_to_wordpress] 📬 WordPress response: status={response.status_code}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user