fixing wp-igny8-integration
This commit is contained in:
@@ -759,17 +759,19 @@ class ContentViewSet(SiteSectorModelViewSet):
|
||||
@action(detail=True, methods=['post'], url_path='publish', url_name='publish', permission_classes=[IsAuthenticatedAndActive, IsEditorOrAbove])
|
||||
def publish(self, request, pk=None):
|
||||
"""
|
||||
STAGE 3: Publish content to WordPress site.
|
||||
Prevents duplicate publishing and updates external_id/external_url.
|
||||
STAGE 3: Publish content to WordPress site via Celery task.
|
||||
Mirrors the automated publishing flow for manual publishing from Review page.
|
||||
|
||||
POST /api/v1/writer/content/{id}/publish/
|
||||
{
|
||||
"site_id": 1, // Optional - defaults to content's site
|
||||
"status": "publish" // Optional - draft or publish
|
||||
"site_integration_id": 1 // Optional - defaults to finding WordPress integration for content's site
|
||||
}
|
||||
"""
|
||||
from igny8_core.auth.models import Site
|
||||
from igny8_core.business.publishing.services.adapters.wordpress_adapter import WordPressAdapter
|
||||
from igny8_core.business.integration.models import SiteIntegration
|
||||
from igny8_core.tasks.wordpress_publishing import publish_content_to_wordpress
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
content = self.get_object()
|
||||
|
||||
@@ -782,72 +784,65 @@ class ContentViewSet(SiteSectorModelViewSet):
|
||||
errors={'external_id': [f'Already published with ID: {content.external_id}']}
|
||||
)
|
||||
|
||||
# Get site (use content's site if not specified)
|
||||
site_id = request.data.get('site_id') or content.site_id
|
||||
if not site_id:
|
||||
return error_response(
|
||||
error='site_id is required or content must have a site',
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
request=request
|
||||
)
|
||||
# Get site integration (use content's site if not specified)
|
||||
site_integration_id = request.data.get('site_integration_id')
|
||||
|
||||
if not site_integration_id:
|
||||
# Find WordPress integration for this site
|
||||
site_integrations = SiteIntegration.objects.filter(
|
||||
site=content.site,
|
||||
platform='wordpress',
|
||||
is_active=True
|
||||
)
|
||||
|
||||
if not site_integrations.exists():
|
||||
return error_response(
|
||||
error='No active WordPress integration found for this site',
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
request=request,
|
||||
errors={'site_integration': ['WordPress integration is required to publish']}
|
||||
)
|
||||
|
||||
site_integration = site_integrations.first()
|
||||
else:
|
||||
try:
|
||||
site_integration = SiteIntegration.objects.get(
|
||||
id=site_integration_id,
|
||||
site=content.site,
|
||||
platform='wordpress'
|
||||
)
|
||||
except SiteIntegration.DoesNotExist:
|
||||
return error_response(
|
||||
error=f'WordPress integration with id {site_integration_id} not found for this site',
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
request=request
|
||||
)
|
||||
|
||||
# Queue publishing task (same as automated flow)
|
||||
try:
|
||||
site = Site.objects.get(id=site_id)
|
||||
except Site.DoesNotExist:
|
||||
return error_response(
|
||||
error=f'Site with id {site_id} does not exist',
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
request=request
|
||||
result = publish_content_to_wordpress.delay(
|
||||
content_id=content.id,
|
||||
site_integration_id=site_integration.id
|
||||
)
|
||||
|
||||
# Get WordPress credentials from site metadata
|
||||
wp_credentials = site.metadata.get('wordpress', {}) if site.metadata else {}
|
||||
wp_url = wp_credentials.get('url') or site.url
|
||||
wp_username = wp_credentials.get('username')
|
||||
wp_app_password = wp_credentials.get('app_password')
|
||||
|
||||
if not wp_username or not wp_app_password:
|
||||
return error_response(
|
||||
error='WordPress credentials not configured for this site',
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
request=request,
|
||||
errors={'credentials': ['Missing WordPress username or app password in site settings']}
|
||||
)
|
||||
|
||||
# Use WordPress adapter to publish
|
||||
adapter = WordPressAdapter()
|
||||
wp_status = request.data.get('status', 'publish') # draft or publish
|
||||
|
||||
result = adapter.publish(
|
||||
content=content,
|
||||
destination_config={
|
||||
'site_url': wp_url,
|
||||
'username': wp_username,
|
||||
'app_password': wp_app_password,
|
||||
'status': wp_status,
|
||||
}
|
||||
)
|
||||
|
||||
if result.get('success'):
|
||||
# STAGE 3: Update content with external references
|
||||
content.external_id = result.get('external_id')
|
||||
content.external_url = result.get('url')
|
||||
content.status = 'published'
|
||||
content.save(update_fields=['external_id', 'external_url', 'status', 'updated_at'])
|
||||
|
||||
logger.info(f"[ContentViewSet.publish] Queued Celery task {result.id} for content {content.id}")
|
||||
|
||||
return success_response(
|
||||
data={
|
||||
'content_id': content.id,
|
||||
'status': content.status,
|
||||
'external_id': content.external_id,
|
||||
'external_url': content.external_url,
|
||||
'task_id': result.id,
|
||||
'status': 'queued',
|
||||
'message': 'Publishing queued - content will be published to WordPress shortly'
|
||||
},
|
||||
message='Content published to WordPress successfully',
|
||||
request=request
|
||||
message='Content publishing queued successfully',
|
||||
request=request,
|
||||
status_code=status.HTTP_202_ACCEPTED
|
||||
)
|
||||
else:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[ContentViewSet.publish] Error queuing publish task: {str(e)}", exc_info=True)
|
||||
return error_response(
|
||||
error=f"Failed to publish to WordPress: {result.get('metadata', {}).get('error', 'Unknown error')}",
|
||||
error=f"Failed to queue publishing task: {str(e)}",
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
request=request
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user