fix fix fix
This commit is contained in:
@@ -187,45 +187,136 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
|
||||
)
|
||||
logger.info(f"[publish_content_to_wordpress] 📬 WordPress response: status={response.status_code}")
|
||||
|
||||
# Track start time for duration measurement
|
||||
import time
|
||||
from igny8_core.business.integration.models import SyncEvent
|
||||
start_time = time.time()
|
||||
|
||||
if response.status_code == 201:
|
||||
# Success
|
||||
wp_data = response.json().get('data', {})
|
||||
logger.info(f"[publish_content_to_wordpress] ✅ WordPress post created successfully: post_id={wp_data.get('post_id')}")
|
||||
wp_status = wp_data.get('post_status', 'publish')
|
||||
logger.info(f"[publish_content_to_wordpress] ✅ WordPress post created successfully: post_id={wp_data.get('post_id')}, status={wp_status}")
|
||||
|
||||
# Update external_id and external_url for unified Content model
|
||||
content.external_id = wp_data.get('post_id')
|
||||
# Update external_id, external_url, and wordpress_status in Content model
|
||||
content.external_id = str(wp_data.get('post_id'))
|
||||
content.external_url = wp_data.get('post_url')
|
||||
content.status = 'published'
|
||||
|
||||
# Add wordpress_status field to Content model metadata
|
||||
if not hasattr(content, 'metadata') or content.metadata is None:
|
||||
content.metadata = {}
|
||||
content.metadata['wordpress_status'] = wp_status
|
||||
|
||||
content.save(update_fields=[
|
||||
'external_id', 'external_url', 'status', 'updated_at'
|
||||
'external_id', 'external_url', 'status', 'metadata', 'updated_at'
|
||||
])
|
||||
logger.info(f"[publish_content_to_wordpress] 💾 Content model updated: external_id={content.external_id}, status=published")
|
||||
logger.info(f"[publish_content_to_wordpress] 💾 Content model updated:")
|
||||
logger.info(f" - External ID: {content.external_id}")
|
||||
logger.info(f" - External URL: {content.external_url}")
|
||||
logger.info(f" - Status: published")
|
||||
logger.info(f" - WordPress Status: {wp_status}")
|
||||
|
||||
# Log sync event
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
SyncEvent.objects.create(
|
||||
integration=site_integration,
|
||||
site=content.site,
|
||||
account=content.account,
|
||||
event_type='publish',
|
||||
action='content_publish',
|
||||
description=f"Published content '{content.title}' to WordPress",
|
||||
success=True,
|
||||
content_id=content.id,
|
||||
external_id=str(content.external_id),
|
||||
details={
|
||||
'post_url': content.external_url,
|
||||
'wordpress_status': wp_status,
|
||||
'categories': categories,
|
||||
'tags': tags,
|
||||
'has_featured_image': bool(featured_image_url),
|
||||
'gallery_images_count': len(gallery_images),
|
||||
},
|
||||
duration_ms=duration_ms
|
||||
)
|
||||
|
||||
logger.info(f"[publish_content_to_wordpress] 🎉 Successfully published content {content_id} to WordPress post {content.external_id}")
|
||||
return {
|
||||
"success": True,
|
||||
"external_id": content.external_id,
|
||||
"external_url": content.external_url
|
||||
"external_url": content.external_url,
|
||||
"wordpress_status": wp_status
|
||||
}
|
||||
|
||||
elif response.status_code == 409:
|
||||
# Content already exists
|
||||
wp_data = response.json().get('data', {})
|
||||
content.external_id = wp_data.get('post_id')
|
||||
wp_status = wp_data.get('post_status', 'publish')
|
||||
content.external_id = str(wp_data.get('post_id'))
|
||||
content.external_url = wp_data.get('post_url')
|
||||
content.status = 'published'
|
||||
|
||||
# Update wordpress_status in metadata
|
||||
if not hasattr(content, 'metadata') or content.metadata is None:
|
||||
content.metadata = {}
|
||||
content.metadata['wordpress_status'] = wp_status
|
||||
|
||||
content.save(update_fields=[
|
||||
'external_id', 'external_url', 'status', 'updated_at'
|
||||
'external_id', 'external_url', 'status', 'metadata', 'updated_at'
|
||||
])
|
||||
|
||||
# Log sync event
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
SyncEvent.objects.create(
|
||||
integration=site_integration,
|
||||
site=content.site,
|
||||
account=content.account,
|
||||
event_type='sync',
|
||||
action='content_publish',
|
||||
description=f"Content '{content.title}' already exists in WordPress",
|
||||
success=True,
|
||||
content_id=content.id,
|
||||
external_id=str(content.external_id),
|
||||
details={
|
||||
'post_url': content.external_url,
|
||||
'wordpress_status': wp_status,
|
||||
'already_exists': True,
|
||||
},
|
||||
duration_ms=duration_ms
|
||||
)
|
||||
|
||||
logger.info(f"Content {content_id} already exists on WordPress")
|
||||
return {"success": True, "message": "Content already exists", "external_id": content.external_id}
|
||||
return {
|
||||
"success": True,
|
||||
"message": "Content already exists",
|
||||
"external_id": content.external_id,
|
||||
"wordpress_status": wp_status
|
||||
}
|
||||
|
||||
else:
|
||||
# Error
|
||||
error_msg = f"WordPress API error: {response.status_code} - {response.text}"
|
||||
logger.error(f"[publish_content_to_wordpress] ❌ {error_msg}")
|
||||
|
||||
# Log sync event for failure
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
SyncEvent.objects.create(
|
||||
integration=site_integration,
|
||||
site=content.site,
|
||||
account=content.account,
|
||||
event_type='error',
|
||||
action='content_publish',
|
||||
description=f"Failed to publish content '{content.title}' to WordPress",
|
||||
success=False,
|
||||
content_id=content.id,
|
||||
error_message=error_msg,
|
||||
details={
|
||||
'status_code': response.status_code,
|
||||
'response_text': response.text[:500], # Limit length
|
||||
},
|
||||
duration_ms=duration_ms
|
||||
)
|
||||
|
||||
# Retry logic
|
||||
if self.request.retries < self.max_retries:
|
||||
# Exponential backoff: 1min, 5min, 15min
|
||||
@@ -239,6 +330,30 @@ def publish_content_to_wordpress(self, content_id: int, site_integration_id: int
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[publish_content_to_wordpress] ❌ Exception during publish: {str(e)}", exc_info=True)
|
||||
|
||||
# Log sync event for exception
|
||||
try:
|
||||
from igny8_core.business.integration.models import SyncEvent
|
||||
import time
|
||||
|
||||
SyncEvent.objects.create(
|
||||
integration=site_integration,
|
||||
site=content.site,
|
||||
account=content.account,
|
||||
event_type='error',
|
||||
action='content_publish',
|
||||
description=f"Exception while publishing content '{content.title}' to WordPress",
|
||||
success=False,
|
||||
content_id=content.id,
|
||||
error_message=str(e),
|
||||
details={
|
||||
'exception_type': type(e).__name__,
|
||||
'traceback': str(e),
|
||||
},
|
||||
)
|
||||
except Exception as log_error:
|
||||
logger.error(f"Failed to log sync event: {str(log_error)}")
|
||||
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user