mirror of
https://github.com/kjanat/livegraphs-django.git
synced 2026-02-13 10:29:30 +01:00
Adds a management command and helper script to configure the Jumbo API external data source and fetch chat sessions. Ensures idempotent creation and linkage to the Jumbo company while reading API credentials from environment variables. Prints data source details and a post-fetch summary to ease setup and verification.
78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Script to create Jumbo API external data source and fetch data.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
|
|
import django
|
|
|
|
# Setup Django
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dashboard_project.settings")
|
|
django.setup()
|
|
|
|
from accounts.models import Company # noqa: E402
|
|
from data_integration.models import ExternalDataSource # noqa: E402
|
|
from data_integration.tasks import refresh_specific_source # noqa: E402
|
|
|
|
|
|
def main():
|
|
print("Setting up Jumbo API data source...")
|
|
|
|
# Get Jumbo company
|
|
try:
|
|
jumbo = Company.objects.get(name="Jumbo")
|
|
print(f"✓ Found Jumbo company (ID: {jumbo.id})")
|
|
except Company.DoesNotExist:
|
|
print("✗ Jumbo company not found. Run setup_jumbo command first.")
|
|
return
|
|
|
|
# Create or get Jumbo API external data source
|
|
source, created = ExternalDataSource.objects.get_or_create(
|
|
name="Jumbo API",
|
|
defaults={
|
|
"company": jumbo,
|
|
"api_endpoint": "https://mijn.jumbo.com/api/chat/sessions",
|
|
"api_username": os.environ.get("EXTERNAL_API_USERNAME", ""),
|
|
"api_password": os.environ.get("EXTERNAL_API_PASSWORD", ""),
|
|
"is_active": True,
|
|
},
|
|
)
|
|
|
|
# Ensure company is set if already existed
|
|
if not created and not source.company:
|
|
source.company = jumbo
|
|
source.save()
|
|
print(f"✓ Linked existing Jumbo API source to {jumbo.name}")
|
|
elif created:
|
|
print("✓ Created Jumbo API external data source")
|
|
else:
|
|
print("✓ Jumbo API source already exists")
|
|
|
|
print("\nData source details:")
|
|
print(f" ID: {source.id}")
|
|
print(f" Company: {source.company.name if source.company else 'None'}")
|
|
print(f" Endpoint: {source.api_endpoint}")
|
|
print(f" Active: {source.is_active}")
|
|
|
|
# Fetch data
|
|
print("\nFetching Jumbo chat data...")
|
|
try:
|
|
result = refresh_specific_source(source.id)
|
|
print(f"✓ Data fetch completed: {result}")
|
|
except Exception as e:
|
|
print(f"✗ Error fetching data: {e}")
|
|
|
|
# Show summary
|
|
from data_integration.models import ChatSession
|
|
|
|
session_count = ChatSession.objects.filter(company=jumbo).count()
|
|
print("\n✓ Setup complete!")
|
|
print(f" Total Jumbo chat sessions: {session_count}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|