-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbulk_import_leads.py
More file actions
118 lines (98 loc) · 3.65 KB
/
bulk_import_leads.py
File metadata and controls
118 lines (98 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
"""
Bulk Import Script for Leads
Run this script to populate the database with 25 lead entries from leads.json
"""
import requests
import json
import sys
import time
# Configuration
API_BASE_URL = "http://127.0.0.1:8000/api"
LEADS_ENDPOINT = f"{API_BASE_URL}/leads/"
JSON_FILE = "leads.json"
def load_leads():
"""Load leads from JSON file"""
try:
with open(JSON_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
print(f"❌ Error: {JSON_FILE} not found in current directory")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"❌ Error: Invalid JSON in {JSON_FILE}: {e}")
sys.exit(1)
def test_connection():
"""Test if the API server is running"""
try:
response = requests.get("http://127.0.0.1:8000/api/leads/", timeout=5)
print(f"Response status: {response.status_code}")
return response.status_code in [200, 404] # 404 is ok, means endpoint exists but no data
except requests.exceptions.RequestException as e:
print(f"Connection error: {e}")
return False
def import_lead(lead):
"""Import a single lead"""
try:
response = requests.post(
LEADS_ENDPOINT,
json=lead,
headers={'Content-Type': 'application/json'},
timeout=10
)
if response.status_code == 201:
return True, f"✓ Created: {lead['name']}"
elif response.status_code == 400:
error_data = response.json()
return False, f"✗ Validation error for {lead['name']}: {error_data}"
elif response.status_code == 500:
return False, f"✗ Server error for {lead['name']}: {response.text}"
else:
return False, f"✗ Unexpected status {response.status_code} for {lead['name']}: {response.text}"
except requests.exceptions.RequestException as e:
return False, f"✗ Network error for {lead['name']}: {e}"
def main():
print("🚀 Starting Bulk Lead Import")
print("=" * 50)
# Test connection
print("🔍 Testing API connection...")
if not test_connection():
print("❌ Error: Cannot connect to API server at", API_BASE_URL)
print(" Make sure Django server is running: python manage.py runserver")
sys.exit(1)
print("✅ API server is accessible")
# Load leads
print(f"📂 Loading leads from {JSON_FILE}...")
leads = load_leads()
print(f"✅ Loaded {len(leads)} leads")
# Import leads
print("\n📤 Starting import process...")
print("-" * 50)
success_count = 0
fail_count = 0
for i, lead in enumerate(leads, 1):
print("2d", end="", flush=True)
success, message = import_lead(lead)
print(f" {message}")
if success:
success_count += 1
else:
fail_count += 1
# Small delay to avoid overwhelming the server
time.sleep(0.1)
# Summary
print("\n" + "=" * 50)
print("📊 IMPORT SUMMARY")
print("=" * 50)
print(f"✅ Successfully imported: {success_count} leads")
print(f"❌ Failed to import: {fail_count} leads")
print(f"📈 Success rate: {(success_count/len(leads)*100):.1f}%")
if success_count > 0:
print("\n🎉 Import completed! You can now:")
print(" • View leads at: http://127.0.0.1:8000/api/leads/")
print(" • Check leads page at: http://localhost:3002/leads")
print(" • Convert leads to candidates through the UI")
if fail_count > 0:
print(f"\n⚠️ {fail_count} leads failed to import. Check the errors above.")
if __name__ == "__main__":
main()