awacke1 commited on
Commit
e6f6372
Β·
verified Β·
1 Parent(s): c73a77d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -27
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, name, id):
178
  try:
179
- container.delete_item(item=id, partition_key=id)
180
- return True, f"Successfully deleted record with name: {name} and id: {id} πŸ—‘οΈ"
181
- except exceptions.CosmosResourceNotFoundError:
182
- return False, f"Record with id {id} not found. It may have been already deleted. πŸ•΅οΈβ€β™‚οΈ"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: {traceback.format_exc()} 😱"
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():