Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,11 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
| 7 |
import sqlite3
|
| 8 |
import os
|
| 9 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.DEBUG)
|
|
@@ -45,7 +50,7 @@ def init_db():
|
|
| 45 |
# Run database initialization on startup
|
| 46 |
init_db()
|
| 47 |
|
| 48 |
-
# Salesforce connection (using
|
| 49 |
def get_salesforce_connection():
|
| 50 |
try:
|
| 51 |
username = os.getenv("SFDC_USERNAME")
|
|
@@ -53,9 +58,6 @@ def get_salesforce_connection():
|
|
| 53 |
security_token = os.getenv("SFDC_SECURITY_TOKEN")
|
| 54 |
domain = os.getenv("SFDC_DOMAIN", "login")
|
| 55 |
|
| 56 |
-
logger.debug(f"SFDC_USERNAME: {username}")
|
| 57 |
-
logger.debug(f"SFDC_SECURITY_TOKEN: {security_token}")
|
| 58 |
-
|
| 59 |
if not username or not password or not security_token:
|
| 60 |
logger.error("Missing Salesforce credentials.")
|
| 61 |
return None
|
|
@@ -72,7 +74,13 @@ def get_salesforce_connection():
|
|
| 72 |
logger.error(f"Error connecting to Salesforce: {e}")
|
| 73 |
return None
|
| 74 |
|
| 75 |
-
# Sync function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
async def sync_to_salesforce():
|
| 77 |
try:
|
| 78 |
sf = get_salesforce_connection()
|
|
@@ -100,7 +108,7 @@ async def sync_to_salesforce():
|
|
| 100 |
conn.close()
|
| 101 |
except Exception as e:
|
| 102 |
logger.error(f"Error syncing to Salesforce: {str(e)}")
|
| 103 |
-
raise
|
| 104 |
|
| 105 |
# Set up scheduler
|
| 106 |
scheduler = AsyncIOScheduler()
|
|
@@ -133,16 +141,27 @@ async def submit_case(request: Request):
|
|
| 133 |
data = await request.json()
|
| 134 |
logger.debug(f"Received form data: {data}")
|
| 135 |
|
| 136 |
-
# Extract form data with default values
|
| 137 |
-
name = data.get("name", "")
|
| 138 |
-
mobile_number = data.get("mobileNumber", "")
|
| 139 |
-
email = data.get("email", "")
|
| 140 |
-
issue_description = data.get("issueDescription", "")
|
| 141 |
-
|
| 142 |
-
# Validate required fields
|
| 143 |
-
if not name
|
| 144 |
-
logger.error("Missing
|
| 145 |
-
raise HTTPException(status_code=400, detail="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# Store in SQLite
|
| 148 |
conn = sqlite3.connect("cases.db")
|
|
@@ -158,7 +177,7 @@ async def submit_case(request: Request):
|
|
| 158 |
conn.close()
|
| 159 |
logger.debug("Data successfully stored in SQLite.")
|
| 160 |
|
| 161 |
-
# Immediately trigger sync
|
| 162 |
await sync_to_salesforce()
|
| 163 |
|
| 164 |
return {"success": True}
|
|
|
|
| 7 |
import sqlite3
|
| 8 |
import os
|
| 9 |
import logging
|
| 10 |
+
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
|
| 13 |
+
# Load environment variables from .env file (for local development)
|
| 14 |
+
load_dotenv()
|
| 15 |
|
| 16 |
# Set up logging
|
| 17 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
| 50 |
# Run database initialization on startup
|
| 51 |
init_db()
|
| 52 |
|
| 53 |
+
# Salesforce connection (using environment variables)
|
| 54 |
def get_salesforce_connection():
|
| 55 |
try:
|
| 56 |
username = os.getenv("SFDC_USERNAME")
|
|
|
|
| 58 |
security_token = os.getenv("SFDC_SECURITY_TOKEN")
|
| 59 |
domain = os.getenv("SFDC_DOMAIN", "login")
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
if not username or not password or not security_token:
|
| 62 |
logger.error("Missing Salesforce credentials.")
|
| 63 |
return None
|
|
|
|
| 74 |
logger.error(f"Error connecting to Salesforce: {e}")
|
| 75 |
return None
|
| 76 |
|
| 77 |
+
# Sync function with retry logic
|
| 78 |
+
@retry(
|
| 79 |
+
stop=stop_after_attempt(3),
|
| 80 |
+
wait=wait_fixed(2),
|
| 81 |
+
retry=retry_if_exception_type(Exception),
|
| 82 |
+
before_sleep=lambda retry_state: logger.debug(f"Retrying Salesforce sync, attempt {retry_state.attempt_number}")
|
| 83 |
+
)
|
| 84 |
async def sync_to_salesforce():
|
| 85 |
try:
|
| 86 |
sf = get_salesforce_connection()
|
|
|
|
| 108 |
conn.close()
|
| 109 |
except Exception as e:
|
| 110 |
logger.error(f"Error syncing to Salesforce: {str(e)}")
|
| 111 |
+
raise
|
| 112 |
|
| 113 |
# Set up scheduler
|
| 114 |
scheduler = AsyncIOScheduler()
|
|
|
|
| 141 |
data = await request.json()
|
| 142 |
logger.debug(f"Received form data: {data}")
|
| 143 |
|
| 144 |
+
# Extract form data with default values and strip whitespace
|
| 145 |
+
name = data.get("name", "").strip()
|
| 146 |
+
mobile_number = data.get("mobileNumber", "").strip()
|
| 147 |
+
email = data.get("email", "").strip()
|
| 148 |
+
issue_description = data.get("issueDescription", "").strip()
|
| 149 |
+
|
| 150 |
+
# Validate required fields with specific error messages
|
| 151 |
+
if not name:
|
| 152 |
+
logger.error("Missing 'name' field in form data.")
|
| 153 |
+
raise HTTPException(status_code=400, detail="Name is required")
|
| 154 |
+
if not email:
|
| 155 |
+
logger.error("Missing 'email' field in form data.")
|
| 156 |
+
raise HTTPException(status_code=400, detail="Email is required")
|
| 157 |
+
if not issue_description:
|
| 158 |
+
logger.error("Missing 'issueDescription' field in form data.")
|
| 159 |
+
raise HTTPException(status_code=400, detail="Issue description is required")
|
| 160 |
+
|
| 161 |
+
# Basic email validation
|
| 162 |
+
if "@" not in email or "." not in email:
|
| 163 |
+
logger.error("Invalid email format.")
|
| 164 |
+
raise HTTPException(status_code=400, detail="Invalid email format")
|
| 165 |
|
| 166 |
# Store in SQLite
|
| 167 |
conn = sqlite3.connect("cases.db")
|
|
|
|
| 177 |
conn.close()
|
| 178 |
logger.debug("Data successfully stored in SQLite.")
|
| 179 |
|
| 180 |
+
# Immediately trigger sync
|
| 181 |
await sync_to_salesforce()
|
| 182 |
|
| 183 |
return {"success": True}
|