- Enhanced the `error_response` function to support backward compatibility by normalizing arguments when positional arguments are misused. - Updated various views to pass `None` for the `errors` parameter in `error_response` calls, ensuring consistent response formatting. - Adjusted logging in `ContentSyncService` and `WordPressClient` to use debug level for expected 401 errors, improving log clarity. - Removed deprecated fields from serializers and views, streamlining content management processes.
462 lines
17 KiB
Python
462 lines
17 KiB
Python
"""
|
|
Integration ViewSet
|
|
Phase 6: Site Integration & Multi-Destination Publishing
|
|
"""
|
|
from rest_framework import status
|
|
from rest_framework.decorators import action
|
|
from rest_framework.response import Response
|
|
|
|
from igny8_core.api.base import SiteSectorModelViewSet
|
|
from igny8_core.api.permissions import IsAuthenticatedAndActive, IsEditorOrAbove
|
|
from igny8_core.api.response import success_response, error_response
|
|
from igny8_core.api.throttles import DebugScopedRateThrottle
|
|
from igny8_core.business.integration.models import SiteIntegration
|
|
from igny8_core.business.integration.services.integration_service import IntegrationService
|
|
from igny8_core.business.integration.services.sync_service import SyncService
|
|
from igny8_core.business.integration.services.sync_health_service import SyncHealthService
|
|
from igny8_core.business.integration.services.content_sync_service import ContentSyncService
|
|
|
|
|
|
class IntegrationViewSet(SiteSectorModelViewSet):
|
|
"""
|
|
ViewSet for SiteIntegration model.
|
|
"""
|
|
queryset = SiteIntegration.objects.select_related('site')
|
|
permission_classes = [IsAuthenticatedAndActive, IsEditorOrAbove]
|
|
throttle_scope = 'integration'
|
|
throttle_classes = [DebugScopedRateThrottle]
|
|
|
|
def get_serializer_class(self):
|
|
from rest_framework import serializers
|
|
|
|
class SiteIntegrationSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = SiteIntegration
|
|
fields = '__all__'
|
|
read_only_fields = ['created_at', 'updated_at', 'last_sync_at']
|
|
|
|
return SiteIntegrationSerializer
|
|
|
|
@action(detail=True, methods=['post'])
|
|
def test_connection(self, request, pk=None):
|
|
"""
|
|
Test connection to integrated platform.
|
|
|
|
POST /api/v1/integration/integrations/{id}/test_connection/
|
|
"""
|
|
integration = self.get_object()
|
|
|
|
service = IntegrationService()
|
|
result = service.test_connection(integration)
|
|
|
|
if result.get('success'):
|
|
return success_response(result, request=request)
|
|
else:
|
|
return error_response(
|
|
result.get('message', 'Connection test failed'),
|
|
None,
|
|
status.HTTP_400_BAD_REQUEST,
|
|
request
|
|
)
|
|
|
|
from rest_framework.permissions import AllowAny
|
|
|
|
@action(detail=False, methods=['post'], url_path='test-connection', permission_classes=[AllowAny])
|
|
def test_connection_collection(self, request):
|
|
"""
|
|
Collection-level test connection endpoint for frontend convenience.
|
|
POST /api/v1/integration/integrations/test-connection/
|
|
|
|
Body:
|
|
{
|
|
"site_id": 123,
|
|
"api_key": "...",
|
|
"site_url": "https://example.com"
|
|
}
|
|
"""
|
|
site_id = request.data.get('site_id')
|
|
api_key = request.data.get('api_key')
|
|
site_url = request.data.get('site_url')
|
|
|
|
if not site_id:
|
|
return error_response('site_id is required', None, status.HTTP_400_BAD_REQUEST, request)
|
|
|
|
# Verify site exists
|
|
from igny8_core.auth.models import Site
|
|
try:
|
|
site = Site.objects.get(id=int(site_id))
|
|
except (Site.DoesNotExist, ValueError, TypeError):
|
|
return error_response('Site not found or invalid', None, status.HTTP_404_NOT_FOUND, request)
|
|
|
|
# Authentication: accept either authenticated user OR matching API key in body
|
|
api_key = request.data.get('api_key') or api_key
|
|
authenticated = False
|
|
# If request has a valid user and belongs to same account, allow
|
|
if hasattr(request, 'user') and getattr(request.user, 'is_authenticated', False):
|
|
try:
|
|
# If user has account, ensure site belongs to user's account
|
|
if site.account == request.user.account:
|
|
authenticated = True
|
|
except Exception:
|
|
# Ignore and fallback to api_key check
|
|
pass
|
|
|
|
# If not authenticated via session, allow if provided api_key matches site's stored wp_api_key
|
|
if not authenticated:
|
|
stored_key = getattr(site, 'wp_api_key', None)
|
|
if stored_key and api_key and str(api_key) == str(stored_key):
|
|
authenticated = True
|
|
|
|
if not authenticated:
|
|
return error_response('Authentication credentials were not provided.', None, status.HTTP_403_FORBIDDEN, request)
|
|
|
|
# Try to find an existing integration for this site+platform
|
|
integration = SiteIntegration.objects.filter(site=site, platform='wordpress').first()
|
|
|
|
# If not found, create a temporary in-memory integration object
|
|
if not integration:
|
|
integration = SiteIntegration(
|
|
site=site,
|
|
platform='wordpress',
|
|
config_json={'site_url': site_url} if site_url else {},
|
|
credentials_json={'api_key': api_key} if api_key else {},
|
|
is_active=False
|
|
)
|
|
|
|
service = IntegrationService()
|
|
result = service.test_connection(integration)
|
|
|
|
if result.get('success'):
|
|
return success_response(result, request=request)
|
|
else:
|
|
return error_response(result.get('message', 'Connection test failed'), None, status.HTTP_400_BAD_REQUEST, request)
|
|
|
|
@action(detail=True, methods=['post'])
|
|
def sync(self, request, pk=None):
|
|
"""
|
|
Trigger synchronization with integrated platform.
|
|
|
|
POST /api/v1/integration/integrations/{id}/sync/
|
|
|
|
Request body:
|
|
{
|
|
"direction": "both", # 'both', 'to_external', 'from_external'
|
|
"content_types": ["blog_post", "page"] # Optional
|
|
}
|
|
"""
|
|
integration = self.get_object()
|
|
|
|
direction = request.data.get('direction', 'both')
|
|
content_types = request.data.get('content_types')
|
|
|
|
sync_service = SyncService()
|
|
result = sync_service.sync(integration, direction=direction, content_types=content_types)
|
|
|
|
response_status = status.HTTP_200_OK if result.get('success') else status.HTTP_400_BAD_REQUEST
|
|
return success_response(result, request=request, status_code=response_status)
|
|
|
|
@action(detail=True, methods=['get'])
|
|
def sync_status(self, request, pk=None):
|
|
"""
|
|
Get sync status for integration.
|
|
|
|
GET /api/v1/integration/integrations/{id}/sync_status/
|
|
"""
|
|
integration = self.get_object()
|
|
|
|
sync_service = SyncService()
|
|
status_data = sync_service.get_sync_status(integration)
|
|
|
|
return success_response(status_data, request=request)
|
|
|
|
@action(detail=True, methods=['get'], url_path='content-types')
|
|
def content_types_summary(self, request, pk=None):
|
|
"""
|
|
Get content types summary with counts from synced data.
|
|
|
|
GET /api/v1/integration/integrations/{id}/content-types/
|
|
|
|
Returns:
|
|
{
|
|
"success": true,
|
|
"data": {
|
|
"post_types": {
|
|
"post": {"label": "Posts", "count": 123, "synced_count": 50},
|
|
"page": {"label": "Pages", "count": 12, "synced_count": 12},
|
|
"product": {"label": "Products", "count": 456, "synced_count": 200}
|
|
},
|
|
"taxonomies": {
|
|
"category": {"label": "Categories", "count": 25, "synced_count": 25},
|
|
"post_tag": {"label": "Tags", "count": 102, "synced_count": 80},
|
|
"product_cat": {"label": "Product Categories", "count": 15, "synced_count": 15}
|
|
},
|
|
"last_structure_fetch": "2025-11-22T10:00:00Z"
|
|
}
|
|
}
|
|
"""
|
|
integration = self.get_object()
|
|
site = integration.site
|
|
|
|
# Get config from integration
|
|
config = integration.config_json or {}
|
|
content_types = config.get('content_types', {})
|
|
|
|
# Get synced counts from Content and ContentTaxonomy models
|
|
from igny8_core.business.content.models import Content, ContentTaxonomy
|
|
|
|
# Build response with synced counts
|
|
post_types_data = {}
|
|
for wp_type, type_config in content_types.get('post_types', {}).items():
|
|
# Map WP type to entity_type
|
|
entity_type_map = {
|
|
'post': 'post',
|
|
'page': 'page',
|
|
'product': 'product',
|
|
'service': 'service',
|
|
}
|
|
entity_type = entity_type_map.get(wp_type, 'post')
|
|
|
|
# Count synced content
|
|
synced_count = Content.objects.filter(
|
|
site=site,
|
|
entity_type=entity_type,
|
|
external_type=wp_type,
|
|
sync_status__in=['imported', 'synced']
|
|
).count()
|
|
|
|
post_types_data[wp_type] = {
|
|
'label': type_config.get('label', wp_type.title()),
|
|
'count': type_config.get('count', 0),
|
|
'synced_count': synced_count,
|
|
'enabled': type_config.get('enabled', False),
|
|
'fetch_limit': type_config.get('fetch_limit', 100),
|
|
'last_synced': type_config.get('last_synced'),
|
|
}
|
|
|
|
taxonomies_data = {}
|
|
for wp_tax, tax_config in content_types.get('taxonomies', {}).items():
|
|
# Count synced taxonomies
|
|
synced_count = ContentTaxonomy.objects.filter(
|
|
site=site,
|
|
external_taxonomy=wp_tax,
|
|
sync_status__in=['imported', 'synced']
|
|
).count()
|
|
|
|
taxonomies_data[wp_tax] = {
|
|
'label': tax_config.get('label', wp_tax.title()),
|
|
'count': tax_config.get('count', 0),
|
|
'synced_count': synced_count,
|
|
'enabled': tax_config.get('enabled', False),
|
|
'fetch_limit': tax_config.get('fetch_limit', 100),
|
|
'last_synced': tax_config.get('last_synced'),
|
|
}
|
|
|
|
summary = {
|
|
'post_types': post_types_data,
|
|
'taxonomies': taxonomies_data,
|
|
'last_structure_fetch': config.get('last_structure_fetch'),
|
|
'plugin_connection_enabled': config.get('plugin_connection_enabled', True),
|
|
'two_way_sync_enabled': config.get('two_way_sync_enabled', True),
|
|
}
|
|
|
|
return success_response(summary, request=request)
|
|
|
|
# Stage 4: Site-level sync endpoints
|
|
|
|
@action(detail=False, methods=['get'], url_path='sites/(?P<site_id>[^/.]+)/sync/status')
|
|
def sync_status_by_site(self, request, site_id=None):
|
|
"""
|
|
Get sync status for all integrations on a site.
|
|
Stage 4: Site-level sync health endpoint.
|
|
|
|
GET /api/v1/integration/integrations/sites/{site_id}/sync/status/
|
|
"""
|
|
try:
|
|
site_id_int = int(site_id)
|
|
except (ValueError, TypeError):
|
|
return error_response(
|
|
'Invalid site_id',
|
|
None,
|
|
status.HTTP_400_BAD_REQUEST,
|
|
request
|
|
)
|
|
|
|
# Verify site belongs to user's account
|
|
from igny8_core.auth.models import Site
|
|
try:
|
|
site = Site.objects.get(id=site_id_int, account=request.user.account)
|
|
except Site.DoesNotExist:
|
|
return error_response(
|
|
'Site not found',
|
|
None,
|
|
status.HTTP_404_NOT_FOUND,
|
|
request
|
|
)
|
|
|
|
sync_health_service = SyncHealthService()
|
|
status_data = sync_health_service.get_sync_status(site_id_int)
|
|
|
|
return success_response(status_data, request=request)
|
|
|
|
@action(detail=False, methods=['post'], url_path='sites/(?P<site_id>[^/.]+)/sync/run')
|
|
def run_sync(self, request, site_id=None):
|
|
"""
|
|
Trigger sync for all integrations on a site.
|
|
Stage 4: Site-level sync trigger endpoint.
|
|
|
|
POST /api/v1/integration/integrations/sites/{site_id}/sync/run/
|
|
|
|
Request body:
|
|
{
|
|
"direction": "both", # Optional: 'both', 'to_external', 'from_external'
|
|
"content_types": ["blog_post", "product"] # Optional
|
|
}
|
|
"""
|
|
try:
|
|
site_id_int = int(site_id)
|
|
except (ValueError, TypeError):
|
|
return error_response(
|
|
'Invalid site_id',
|
|
None,
|
|
status.HTTP_400_BAD_REQUEST,
|
|
request
|
|
)
|
|
|
|
# Verify site belongs to user's account
|
|
from igny8_core.auth.models import Site
|
|
try:
|
|
site = Site.objects.get(id=site_id_int, account=request.user.account)
|
|
except Site.DoesNotExist:
|
|
return error_response(
|
|
'Site not found',
|
|
None,
|
|
status.HTTP_404_NOT_FOUND,
|
|
request
|
|
)
|
|
|
|
direction = request.data.get('direction', 'both')
|
|
content_types = request.data.get('content_types')
|
|
|
|
# Get all active integrations for this site
|
|
integrations = SiteIntegration.objects.filter(
|
|
site_id=site_id_int,
|
|
is_active=True,
|
|
sync_enabled=True
|
|
)
|
|
|
|
if not integrations.exists():
|
|
return error_response(
|
|
'No active integrations found for this site',
|
|
None,
|
|
status.HTTP_400_BAD_REQUEST,
|
|
request
|
|
)
|
|
|
|
sync_service = SyncService()
|
|
sync_health_service = SyncHealthService()
|
|
results = []
|
|
|
|
for integration in integrations:
|
|
result = sync_service.sync(integration, direction=direction, content_types=content_types)
|
|
|
|
# Record sync run
|
|
sync_health_service.record_sync_run(integration.id, result)
|
|
|
|
results.append({
|
|
'integration_id': integration.id,
|
|
'platform': integration.platform,
|
|
'result': result
|
|
})
|
|
|
|
return success_response({
|
|
'site_id': site_id_int,
|
|
'sync_results': results,
|
|
'total_integrations': len(results)
|
|
}, request=request)
|
|
|
|
@action(detail=False, methods=['get'], url_path='sites/(?P<site_id>[^/.]+)/sync/mismatches')
|
|
def get_mismatches(self, request, site_id=None):
|
|
"""
|
|
Get sync mismatches for a site.
|
|
Stage 4: Detailed mismatch information.
|
|
|
|
GET /api/v1/integration/integrations/sites/{site_id}/sync/mismatches/
|
|
"""
|
|
try:
|
|
site_id_int = int(site_id)
|
|
except (ValueError, TypeError):
|
|
return error_response(
|
|
'Invalid site_id',
|
|
None,
|
|
status.HTTP_400_BAD_REQUEST,
|
|
request
|
|
)
|
|
|
|
# Verify site belongs to user's account
|
|
from igny8_core.auth.models import Site
|
|
try:
|
|
site = Site.objects.get(id=site_id_int, account=request.user.account)
|
|
except Site.DoesNotExist:
|
|
return error_response(
|
|
'Site not found',
|
|
None,
|
|
status.HTTP_404_NOT_FOUND,
|
|
request
|
|
)
|
|
|
|
sync_health_service = SyncHealthService()
|
|
mismatches = sync_health_service.get_mismatches(site_id_int)
|
|
|
|
return success_response(mismatches, request=request)
|
|
|
|
@action(detail=False, methods=['get'], url_path='sites/(?P<site_id>[^/.]+)/sync/logs')
|
|
def get_sync_logs(self, request, site_id=None):
|
|
"""
|
|
Get sync logs for a site.
|
|
Stage 4: Sync history and logs.
|
|
|
|
GET /api/v1/integration/integrations/sites/{site_id}/sync/logs/
|
|
|
|
Query params:
|
|
- limit: Number of logs to return (default: 100)
|
|
- integration_id: Filter by specific integration
|
|
"""
|
|
try:
|
|
site_id_int = int(site_id)
|
|
except (ValueError, TypeError):
|
|
return error_response(
|
|
'Invalid site_id',
|
|
None,
|
|
status.HTTP_400_BAD_REQUEST,
|
|
request
|
|
)
|
|
|
|
# Verify site belongs to user's account
|
|
from igny8_core.auth.models import Site
|
|
try:
|
|
site = Site.objects.get(id=site_id_int, account=request.user.account)
|
|
except Site.DoesNotExist:
|
|
return error_response(
|
|
'Site not found',
|
|
None,
|
|
status.HTTP_404_NOT_FOUND,
|
|
request
|
|
)
|
|
|
|
limit = int(request.query_params.get('limit', 100))
|
|
integration_id = request.query_params.get('integration_id')
|
|
|
|
sync_health_service = SyncHealthService()
|
|
logs = sync_health_service.get_sync_logs(
|
|
site_id_int,
|
|
integration_id=int(integration_id) if integration_id else None,
|
|
limit=limit
|
|
)
|
|
|
|
return success_response({
|
|
'site_id': site_id_int,
|
|
'logs': logs,
|
|
'count': len(logs)
|
|
}, request=request)
|
|
|