Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ API_KEY = os.getenv("API_KEY", "your-api-key-here")
|
|
| 21 |
|
| 22 |
# Validate Salesforce credentials
|
| 23 |
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN ]):
|
| 24 |
-
raise ValueError("Missing Salesforce credentials. Set SF_USERNAME, SF_PASSWORD
|
| 25 |
|
| 26 |
# Initialize Salesforce connection
|
| 27 |
try:
|
|
@@ -123,7 +123,7 @@ def process_image(image, project_name):
|
|
| 123 |
|
| 124 |
# Validate image size and type
|
| 125 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 126 |
-
if
|
| 127 |
return "Error: Image size exceeds 20MB.", "Failure", "", "", 0
|
| 128 |
if not image.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 129 |
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
|
@@ -133,7 +133,7 @@ def process_image(image, project_name):
|
|
| 133 |
|
| 134 |
# Update Salesforce record
|
| 135 |
record = {
|
| 136 |
-
"
|
| 137 |
"Current_Milestone__c": milestone,
|
| 138 |
"Completion_Percentage__c": percent_complete,
|
| 139 |
"Last_Updated_On__c": datetime.now().isoformat(),
|
|
@@ -142,13 +142,15 @@ def process_image(image, project_name):
|
|
| 142 |
}
|
| 143 |
|
| 144 |
try:
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
result = sf.query(query)
|
| 147 |
if result["totalSize"] > 0:
|
| 148 |
project_id = result["records"][0]["Id"]
|
| 149 |
-
sf.
|
| 150 |
else:
|
| 151 |
-
sf.
|
| 152 |
except Exception as e:
|
| 153 |
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
|
| 154 |
|
|
|
|
| 21 |
|
| 22 |
# Validate Salesforce credentials
|
| 23 |
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN ]):
|
| 24 |
+
raise ValueError("Missing Salesforce credentials. Set SF_USERNAME, SF_PASSWORD SF_SECURITY_TOKEN in environment variables.")
|
| 25 |
|
| 26 |
# Initialize Salesforce connection
|
| 27 |
try:
|
|
|
|
| 123 |
|
| 124 |
# Validate image size and type
|
| 125 |
image_size_mb = os.path.getsize(image) / (1024 * 1024)
|
| 126 |
+
if image_size_mb_tb > 20:
|
| 127 |
return "Error: Image size exceeds 20MB.", "Failure", "", "", 0
|
| 128 |
if not image.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 129 |
return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
|
|
|
|
| 133 |
|
| 134 |
# Update Salesforce record
|
| 135 |
record = {
|
| 136 |
+
"Name__c": project_name, # Changed from "Name" to "Name__c"
|
| 137 |
"Current_Milestone__c": milestone,
|
| 138 |
"Completion_Percentage__c": percent_complete,
|
| 139 |
"Last_Updated_On__c": datetime.now().isoformat(),
|
|
|
|
| 142 |
}
|
| 143 |
|
| 144 |
try:
|
| 145 |
+
# Escape single quotes in project_name to prevent SQL injection
|
| 146 |
+
escaped_project_name = project_name.replace("'", "''")
|
| 147 |
+
query = f"SELECT Id FROM Construction__c WHERE Name__c = '{escaped_project_name}'"
|
| 148 |
result = sf.query(query)
|
| 149 |
if result["totalSize"] > 0:
|
| 150 |
project_id = result["records"][0]["Id"]
|
| 151 |
+
sf.Construction__c.update(project_id, record)
|
| 152 |
else:
|
| 153 |
+
sf.Construction__c.create(record)
|
| 154 |
except Exception as e:
|
| 155 |
return f"Error: Failed to update Salesforce - {str(e)}", "Failure", "", "", 0
|
| 156 |
|