Bhanumani12 commited on
Commit
53e9bb8
·
verified ·
1 Parent(s): cda8f2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -15
app.py CHANGED
@@ -120,7 +120,7 @@ def _escape_bare_ampersands(text: str) -> str:
120
  text
121
  )
122
 
123
- # ---------- Metadata Validator ----------
124
  def validate_metadata(metadata, admin_id=None):
125
  if not metadata.strip():
126
  return "No metadata provided.", "", ""
@@ -129,34 +129,27 @@ def validate_metadata(metadata, admin_id=None):
129
  issue = "Unknown"
130
  recommendation = "No recommendation found."
131
 
132
- # Preview to logs for debugging
133
  preview = metadata[:100].replace("\n", "\\n")
134
  log_to_console({"preview": preview}, "Metadata Input Preview")
135
 
136
  # Clean typical paste artefacts
137
  cleaned = _strip_code_fences(metadata).lstrip("\ufeff").strip()
138
 
139
- # Quick non-XML guard
140
- if "<" not in cleaned or ">" not in cleaned:
141
  issue = "Invalid Input (Not XML)"
142
  recommendation = (
143
  "Please paste Salesforce metadata XML (starts with '<'). Example:\n"
144
  "<CustomField xmlns=\"http://soap.sforce.com/2006/04/metadata\">...</CustomField>"
145
  )
146
  else:
147
- # If leading junk before first '<', cut it
148
- if cleaned and not cleaned.lstrip().startswith("<"):
149
- idx = cleaned.find("<")
150
- if idx != -1:
151
- cleaned = cleaned[idx:].strip()
152
-
153
- # Escape stray ampersands
154
  cleaned = _escape_bare_ampersands(cleaned)
155
 
156
- # Try to parse
157
  try:
158
  root = ET.fromstring(cleaned)
159
- # Find <description> regardless of namespace
160
  has_description = any(
161
  elem.tag.split('}')[-1].lower() == "description"
162
  for elem in root.iter()
@@ -176,7 +169,7 @@ def validate_metadata(metadata, admin_id=None):
176
  issue = "Validation Error"
177
  recommendation = f"Unexpected error while validating metadata: {str(e)}"
178
 
179
- # Log to Salesforce
180
  log_data = {
181
  "Name": f"MetadataLog_{mtype}",
182
  "MetadataType__c": mtype,
@@ -184,7 +177,6 @@ def validate_metadata(metadata, admin_id=None):
184
  "Recommendation__c": recommendation,
185
  "Status__c": "Open"
186
  }
187
-
188
  if admin_id:
189
  log_data["Admin__c"] = admin_id
190
 
 
120
  text
121
  )
122
 
123
+ # ---------- Metadata Validator (updated to handle non-XML gracefully) ----------
124
  def validate_metadata(metadata, admin_id=None):
125
  if not metadata.strip():
126
  return "No metadata provided.", "", ""
 
129
  issue = "Unknown"
130
  recommendation = "No recommendation found."
131
 
132
+ # Log a short preview to help diagnose pastes
133
  preview = metadata[:100].replace("\n", "\\n")
134
  log_to_console({"preview": preview}, "Metadata Input Preview")
135
 
136
  # Clean typical paste artefacts
137
  cleaned = _strip_code_fences(metadata).lstrip("\ufeff").strip()
138
 
139
+ # Only treat as XML if the first non-space character is '<'
140
+ if not cleaned or not cleaned.lstrip().startswith("<"):
141
  issue = "Invalid Input (Not XML)"
142
  recommendation = (
143
  "Please paste Salesforce metadata XML (starts with '<'). Example:\n"
144
  "<CustomField xmlns=\"http://soap.sforce.com/2006/04/metadata\">...</CustomField>"
145
  )
146
  else:
147
+ # Escape stray ampersands (common XML error)
 
 
 
 
 
 
148
  cleaned = _escape_bare_ampersands(cleaned)
149
 
 
150
  try:
151
  root = ET.fromstring(cleaned)
152
+ # Detect <description> regardless of namespace
153
  has_description = any(
154
  elem.tag.split('}')[-1].lower() == "description"
155
  for elem in root.iter()
 
169
  issue = "Validation Error"
170
  recommendation = f"Unexpected error while validating metadata: {str(e)}"
171
 
172
+ # Log to Salesforce (unchanged)
173
  log_data = {
174
  "Name": f"MetadataLog_{mtype}",
175
  "MetadataType__c": mtype,
 
177
  "Recommendation__c": recommendation,
178
  "Status__c": "Open"
179
  }
 
180
  if admin_id:
181
  log_data["Admin__c"] = admin_id
182