django bacekdn opeartioanl fixes and site wp integration api fixes
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
Management command to ensure SiteIntegration records exist for sites with WordPress API keys.
|
||||
API key is stored ONLY in Site.wp_api_key (single source of truth).
|
||||
SiteIntegration is used for integration status/config only, NOT for credential storage.
|
||||
"""
|
||||
from django.core.management.base import BaseCommand
|
||||
from igny8_core.auth.models import Site
|
||||
from igny8_core.business.integration.models import SiteIntegration
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Ensure SiteIntegration records exist for sites with WordPress API keys'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--dry-run',
|
||||
action='store_true',
|
||||
help='Show what would be done without making changes',
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
dry_run = options['dry_run']
|
||||
|
||||
if dry_run:
|
||||
self.stdout.write(self.style.WARNING('DRY RUN MODE - No changes will be made'))
|
||||
|
||||
# Find all sites with wp_api_key
|
||||
sites_with_key = Site.objects.filter(wp_api_key__isnull=False).exclude(wp_api_key='')
|
||||
|
||||
self.stdout.write(f'Found {sites_with_key.count()} sites with wp_api_key')
|
||||
|
||||
created_count = 0
|
||||
already_exists_count = 0
|
||||
cleared_credentials_count = 0
|
||||
|
||||
for site in sites_with_key:
|
||||
try:
|
||||
# Check if SiteIntegration exists
|
||||
integration = SiteIntegration.objects.filter(
|
||||
site=site,
|
||||
platform='wordpress'
|
||||
).first()
|
||||
|
||||
if integration:
|
||||
# Check if credentials_json has api_key (should be cleared)
|
||||
if integration.credentials_json.get('api_key'):
|
||||
if not dry_run:
|
||||
integration.credentials_json = {} # Clear - API key is on Site model
|
||||
integration.save(update_fields=['credentials_json'])
|
||||
self.stdout.write(
|
||||
self.style.WARNING(
|
||||
f' ⟳ {site.name} (ID: {site.id}) - Cleared api_key from credentials_json (now stored in Site.wp_api_key only)'
|
||||
)
|
||||
)
|
||||
cleared_credentials_count += 1
|
||||
else:
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f' ✓ {site.name} (ID: {site.id}) - Integration exists, API key correctly stored in Site.wp_api_key only'
|
||||
)
|
||||
)
|
||||
already_exists_count += 1
|
||||
else:
|
||||
# Create new SiteIntegration (for status tracking, not credentials)
|
||||
if not dry_run:
|
||||
integration = SiteIntegration.objects.create(
|
||||
account=site.account,
|
||||
site=site,
|
||||
platform='wordpress',
|
||||
platform_type='cms',
|
||||
is_active=True,
|
||||
sync_enabled=False, # Don't enable sync by default
|
||||
credentials_json={}, # Empty - API key is on Site model
|
||||
config_json={}
|
||||
)
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f' + {site.name} (ID: {site.id}) - Created SiteIntegration (API key stays in Site.wp_api_key)'
|
||||
)
|
||||
)
|
||||
created_count += 1
|
||||
|
||||
except Exception as e:
|
||||
self.stdout.write(
|
||||
self.style.ERROR(
|
||||
f' ✗ {site.name} (ID: {site.id}) - Error: {str(e)}'
|
||||
)
|
||||
)
|
||||
|
||||
# Summary
|
||||
self.stdout.write('\\n' + '='*60)
|
||||
self.stdout.write(self.style.SUCCESS(f'Summary:'))
|
||||
self.stdout.write(f' Created: {created_count}')
|
||||
self.stdout.write(f' Cleared credentials_json: {cleared_credentials_count}')
|
||||
self.stdout.write(f' Already correct: {already_exists_count}')
|
||||
self.stdout.write(f' Total processed: {created_count + cleared_credentials_count + already_exists_count}')
|
||||
|
||||
if dry_run:
|
||||
self.stdout.write('\\n' + self.style.WARNING('DRY RUN - Run without --dry-run to apply changes'))
|
||||
Reference in New Issue
Block a user