Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -174,17 +174,33 @@ def update_record(container, updated_record):
|
|
| 174 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
| 175 |
|
| 176 |
# ποΈ Delete record - Saying goodbye to data (it's not you, it's me)
|
| 177 |
-
def delete_record(container,
|
| 178 |
try:
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
except exceptions.CosmosHttpResponseError as e:
|
| 184 |
return False, f"HTTP error occurred: {str(e)} π¨"
|
| 185 |
except Exception as e:
|
| 186 |
-
return False, f"An unexpected error occurred: {
|
| 187 |
|
|
|
|
| 188 |
# πΎ Save to Cosmos DB - Preserving data for future generations (or just until the next update)
|
| 189 |
def save_to_cosmos_db(container, query, response1, response2):
|
| 190 |
try:
|
|
@@ -517,27 +533,6 @@ def preprocess_text(text):
|
|
| 517 |
text = text.strip()
|
| 518 |
return text
|
| 519 |
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
# ποΈ Delete record - Handling partition key correctly
|
| 523 |
-
def delete_record(container, id):
|
| 524 |
-
try:
|
| 525 |
-
# Retrieve the document to ensure it exists and get the partition key
|
| 526 |
-
doc = container.read_item(item=id, partition_key=id) # Assuming 'id' is the partition key
|
| 527 |
-
# If your partition key is something else, you need to pass it explicitly here:
|
| 528 |
-
# doc = container.read_item(item=id, partition_key=doc['partition_key_field'])
|
| 529 |
-
|
| 530 |
-
# Now proceed with the deletion
|
| 531 |
-
container.delete_item(item=id, partition_key=doc['id']) # If 'id' is partition key
|
| 532 |
-
return True, f"Successfully deleted record with id: {id} ποΈ"
|
| 533 |
-
except exceptions.CosmosResourceNotFoundError:
|
| 534 |
-
return False, f"Record with id {id} not found. It may have been already deleted. π΅οΈββοΈ"
|
| 535 |
-
except exceptions.CosmosHttpResponseError as e:
|
| 536 |
-
return False, f"HTTP error occurred: {str(e)} π¨"
|
| 537 |
-
except Exception as e:
|
| 538 |
-
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
| 539 |
-
|
| 540 |
-
|
| 541 |
|
| 542 |
# π Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
|
| 543 |
def main():
|
|
|
|
| 174 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
| 175 |
|
| 176 |
# ποΈ Delete record - Saying goodbye to data (it's not you, it's me)
|
| 177 |
+
def delete_record(container, id):
|
| 178 |
try:
|
| 179 |
+
# First try to read the document with id as both item id and partition key
|
| 180 |
+
try:
|
| 181 |
+
doc = container.read_item(item=id, partition_key=id)
|
| 182 |
+
except exceptions.CosmosResourceNotFoundError:
|
| 183 |
+
# If that fails, query to find the document and get its partition key
|
| 184 |
+
query = f"SELECT * FROM c WHERE c.id = '{id}'"
|
| 185 |
+
items = list(container.query_items(
|
| 186 |
+
query=query,
|
| 187 |
+
enable_cross_partition_query=True
|
| 188 |
+
))
|
| 189 |
+
if not items:
|
| 190 |
+
return False, f"Record with id {id} not found. π΅οΈββοΈ"
|
| 191 |
+
doc = items[0]
|
| 192 |
+
|
| 193 |
+
# Now delete with the correct partition key
|
| 194 |
+
partition_key = doc.get('id', id) # Default to id if no partition key found
|
| 195 |
+
container.delete_item(item=id, partition_key=partition_key)
|
| 196 |
+
return True, f"Successfully deleted record with id: {id} ποΈ"
|
| 197 |
+
|
| 198 |
except exceptions.CosmosHttpResponseError as e:
|
| 199 |
return False, f"HTTP error occurred: {str(e)} π¨"
|
| 200 |
except Exception as e:
|
| 201 |
+
return False, f"An unexpected error occurred: {str(e)} π±"
|
| 202 |
|
| 203 |
+
|
| 204 |
# πΎ Save to Cosmos DB - Preserving data for future generations (or just until the next update)
|
| 205 |
def save_to_cosmos_db(container, query, response1, response2):
|
| 206 |
try:
|
|
|
|
| 533 |
text = text.strip()
|
| 534 |
return text
|
| 535 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
|
| 537 |
# π Main function - "All the world's a stage, and all the code merely players" -Shakespeare, probably
|
| 538 |
def main():
|