@@ -10,57 +10,16 @@ from django.shortcuts import get_object_or_404
|
||||
from django.utils import timezone
|
||||
from typing import Dict, Any, List
|
||||
|
||||
from igny8_core.business.content.models import Content
|
||||
from igny8_core.business.integration.models import SiteIntegration
|
||||
from igny8_core.models import ContentPost, SiteIntegration
|
||||
from igny8_core.tasks.wordpress_publishing import (
|
||||
publish_content_to_wordpress,
|
||||
bulk_publish_content_to_wordpress
|
||||
)
|
||||
|
||||
|
||||
@api_view(['POST'])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def simple_publish_content(request) -> Response:
|
||||
"""
|
||||
Simple publish endpoint that gets content_id from POST body
|
||||
POST /api/wordpress/publish/
|
||||
Body: {"content_id": "123"}
|
||||
"""
|
||||
content_id = request.data.get('content_id')
|
||||
if not content_id:
|
||||
return Response(
|
||||
{
|
||||
'success': False,
|
||||
'message': 'content_id is required',
|
||||
'error': 'missing_content_id'
|
||||
},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
try:
|
||||
content_id = int(content_id)
|
||||
except (ValueError, TypeError):
|
||||
return Response(
|
||||
{
|
||||
'success': False,
|
||||
'message': 'Invalid content_id format',
|
||||
'error': 'invalid_content_id'
|
||||
},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# Call the main publish function
|
||||
return publish_single_content_by_id(request, content_id)
|
||||
|
||||
|
||||
@api_view(['POST'])
|
||||
@permission_classes([IsAuthenticated])
|
||||
def publish_single_content(request, content_id: int) -> Response:
|
||||
"""URL-based publish endpoint"""
|
||||
return publish_single_content_by_id(request, content_id)
|
||||
|
||||
|
||||
def publish_single_content_by_id(request, content_id: int) -> Response:
|
||||
"""
|
||||
Publish a single content item to WordPress
|
||||
|
||||
@@ -73,7 +32,7 @@ def publish_single_content_by_id(request, content_id: int) -> Response:
|
||||
}
|
||||
"""
|
||||
try:
|
||||
content = get_object_or_404(Content, id=content_id)
|
||||
content = get_object_or_404(ContentPost, id=content_id)
|
||||
|
||||
# Check permissions
|
||||
if not request.user.has_perm('content.change_contentpost'):
|
||||
@@ -86,23 +45,47 @@ def publish_single_content_by_id(request, content_id: int) -> Response:
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
# Check if already published
|
||||
if content.sync_status == 'success':
|
||||
# Get site integration
|
||||
site_integration_id = request.data.get('site_integration_id')
|
||||
force = request.data.get('force', False)
|
||||
|
||||
if site_integration_id:
|
||||
site_integration = get_object_or_404(SiteIntegration, id=site_integration_id)
|
||||
else:
|
||||
# Get default WordPress integration for user's organization
|
||||
site_integration = SiteIntegration.objects.filter(
|
||||
platform='wordpress',
|
||||
is_active=True,
|
||||
# Add organization filter if applicable
|
||||
).first()
|
||||
|
||||
if not site_integration:
|
||||
return Response(
|
||||
{
|
||||
'success': False,
|
||||
'message': 'No WordPress integration found',
|
||||
'error': 'no_integration'
|
||||
},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# Check if already published (unless force is true)
|
||||
if not force and content.wordpress_sync_status == 'success':
|
||||
return Response(
|
||||
{
|
||||
'success': True,
|
||||
'message': 'Content already published to WordPress',
|
||||
'data': {
|
||||
'content_id': content.id,
|
||||
'wordpress_post_id': content.external_id,
|
||||
'wordpress_post_url': content.external_url,
|
||||
'wordpress_post_id': content.wordpress_post_id,
|
||||
'wordpress_post_url': content.wordpress_post_url,
|
||||
'status': 'already_published'
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
# Check if currently syncing
|
||||
if content.sync_status == 'syncing':
|
||||
if content.wordpress_sync_status == 'syncing':
|
||||
return Response(
|
||||
{
|
||||
'success': False,
|
||||
@@ -113,7 +96,7 @@ def publish_single_content_by_id(request, content_id: int) -> Response:
|
||||
)
|
||||
|
||||
# Validate content is ready for publishing
|
||||
if not content.title or not content.content_html:
|
||||
if not content.title or not (content.content_html or content.content):
|
||||
return Response(
|
||||
{
|
||||
'success': False,
|
||||
@@ -123,23 +106,34 @@ def publish_single_content_by_id(request, content_id: int) -> Response:
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# For now, just simulate successful publishing (simplified version)
|
||||
content.sync_status = 'success'
|
||||
content.external_id = f'wp_{content.id}'
|
||||
content.external_url = f'https://example-site.com/posts/{content.id}/'
|
||||
content.save(update_fields=['sync_status', 'external_id', 'external_url'])
|
||||
# Set status to pending and queue the task
|
||||
content.wordpress_sync_status = 'pending'
|
||||
content.save(update_fields=['wordpress_sync_status'])
|
||||
|
||||
# Get task_id if content is associated with a writer task
|
||||
task_id = None
|
||||
if hasattr(content, 'writer_task'):
|
||||
task_id = content.writer_task.id
|
||||
|
||||
# Queue the publishing task
|
||||
task_result = publish_content_to_wordpress.delay(
|
||||
content.id,
|
||||
site_integration.id,
|
||||
task_id
|
||||
)
|
||||
|
||||
return Response(
|
||||
{
|
||||
'success': True,
|
||||
'message': 'Content published to WordPress successfully',
|
||||
'message': 'Content queued for WordPress publishing',
|
||||
'data': {
|
||||
'content_id': content.id,
|
||||
'wordpress_post_id': content.external_id,
|
||||
'wordpress_post_url': content.external_url,
|
||||
'status': 'published'
|
||||
'site_integration_id': site_integration.id,
|
||||
'task_id': task_result.id,
|
||||
'status': 'queued'
|
||||
}
|
||||
}
|
||||
},
|
||||
status=status.HTTP_202_ACCEPTED
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user