awacke1 commited on
Commit
50b60f1
Β·
verified Β·
1 Parent(s): 3f7e7ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -170,15 +170,14 @@ def update_record(container, updated_record):
170
  except Exception as e:
171
  return False, f"Error: {traceback.format_exc()} 😱"
172
 
173
- # πŸ—‘οΈ Delete a record from Cosmos DB
174
- def delete_record(container, record, partition_key_field=None):
175
  """
176
- Delete a record from Cosmos DB using its ID and partition key.
177
 
178
  Args:
179
  container: Cosmos DB container client
180
- record: Dictionary containing at least 'id' and optionally the partition key field
181
- partition_key_field: Name of the field containing the partition key value (optional)
182
 
183
  Returns:
184
  tuple: (success: bool, message: str)
@@ -189,33 +188,38 @@ def delete_record(container, record, partition_key_field=None):
189
  return False, "Record must contain an 'id' field. πŸ›‘"
190
 
191
  doc_id = record["id"]
 
192
 
193
- # Determine partition key value
194
- if partition_key_field:
195
- partition_key_value = record.get(partition_key_field)
196
- if partition_key_value is None:
197
- return False, f"Partition key field '{partition_key_field}' not found in record {doc_id}. πŸ›‘"
198
- else:
199
- # Default to ID if no partition key field is specified
 
 
 
 
 
 
 
 
200
  partition_key_value = doc_id
 
201
 
202
- # Debug info
203
- st.write(f"Attempting to delete: ID={doc_id}, Partition Key={partition_key_value}")
204
 
205
  # Perform the deletion
206
  container.delete_item(item=doc_id, partition_key=partition_key_value)
207
  return True, f"Record {doc_id} successfully deleted from Cosmos DB. πŸ—‘οΈ"
208
 
209
  except exceptions.CosmosResourceNotFoundError:
210
- # Document doesn't exist, which is fine for a delete operation
211
  return True, f"Record {doc_id} not found in Cosmos DB (already deleted or never existed). πŸ—‘οΈ"
212
  except exceptions.CosmosHttpResponseError as e:
213
- # Other HTTP errors (e.g., wrong partition key, permissions)
214
  return False, f"HTTP error deleting {doc_id}: {str(e)}. 🚨"
215
  except Exception as e:
216
- # Unexpected errors with full traceback
217
  return False, f"Unexpected error deleting {doc_id}: {str(traceback.format_exc())}. 😱"
218
-
219
 
220
 
221
  # πŸ’Ύ Save a new document to Cosmos DB with extra fields
 
170
  except Exception as e:
171
  return False, f"Error: {traceback.format_exc()} 😱"
172
 
173
+ # πŸ—‘οΈ Delete a record from Cosmos DB with auto-detected partition key
174
+ def delete_record(container, record):
175
  """
176
+ Delete a record from Cosmos DB using its ID and an auto-detected partition key.
177
 
178
  Args:
179
  container: Cosmos DB container client
180
+ record: Dictionary containing at least 'id' and potentially a partition key field
 
181
 
182
  Returns:
183
  tuple: (success: bool, message: str)
 
188
  return False, "Record must contain an 'id' field. πŸ›‘"
189
 
190
  doc_id = record["id"]
191
+ st.write(f"Document to delete: {json.dumps(record, indent=2)}") # Debug output
192
 
193
+ # Auto-detect partition key from common fields
194
+ # List of potential partition key fields (customize if needed)
195
+ potential_partition_keys = ["partitionKey", "category", "type", "name", "testPART"]
196
+
197
+ # Check if any potential partition key field exists in the document
198
+ partition_key_value = None
199
+ partition_key_field = None
200
+ for field in potential_partition_keys:
201
+ if field in record:
202
+ partition_key_value = record[field]
203
+ partition_key_field = field
204
+ break
205
+
206
+ # If no specific partition key field is found, default to 'id' (matches your /id setup)
207
+ if partition_key_value is None:
208
  partition_key_value = doc_id
209
+ partition_key_field = "id (default)"
210
 
211
+ st.write(f"Detected Partition Key Field: {partition_key_field}, Value: {partition_key_value}") # Debug
 
212
 
213
  # Perform the deletion
214
  container.delete_item(item=doc_id, partition_key=partition_key_value)
215
  return True, f"Record {doc_id} successfully deleted from Cosmos DB. πŸ—‘οΈ"
216
 
217
  except exceptions.CosmosResourceNotFoundError:
 
218
  return True, f"Record {doc_id} not found in Cosmos DB (already deleted or never existed). πŸ—‘οΈ"
219
  except exceptions.CosmosHttpResponseError as e:
 
220
  return False, f"HTTP error deleting {doc_id}: {str(e)}. 🚨"
221
  except Exception as e:
 
222
  return False, f"Unexpected error deleting {doc_id}: {str(traceback.format_exc())}. 😱"
 
223
 
224
 
225
  # πŸ’Ύ Save a new document to Cosmos DB with extra fields