Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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
|
| 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 |
-
#
|
| 194 |
-
if
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
partition_key_value = doc_id
|
|
|
|
| 201 |
|
| 202 |
-
# Debug
|
| 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
|