Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -155,7 +155,7 @@ def update_record(container, updated_record):
|
|
| 155 |
def delete_record(container, record):
|
| 156 |
try:
|
| 157 |
doc_id = record["id"]
|
| 158 |
-
partition_key_value = record.get("pk", doc_id)
|
| 159 |
st.write(f"Deleting {doc_id} with partition key {partition_key_value}") # Debug log
|
| 160 |
container.delete_item(item=doc_id, partition_key=partition_key_value)
|
| 161 |
return True, f"Record {doc_id} deleted. 🗑️"
|
|
@@ -353,6 +353,11 @@ def edit_all_documents(container):
|
|
| 353 |
if not documents:
|
| 354 |
st.info("No documents in this container.")
|
| 355 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
for doc in documents:
|
| 357 |
ts = doc.get("_ts", 0)
|
| 358 |
dt = datetime.fromtimestamp(ts) if ts else datetime.now()
|
|
@@ -360,26 +365,24 @@ def edit_all_documents(container):
|
|
| 360 |
header = f"{doc.get('name', 'Unnamed')} - {formatted_ts}"
|
| 361 |
with st.expander(header):
|
| 362 |
doc_key = f"editor_{doc['id']}"
|
| 363 |
-
if
|
| 364 |
-
|
| 365 |
-
edited_content = st.text_area("Edit JSON", value=
|
| 366 |
col_save, col_delete = st.columns(2)
|
| 367 |
with col_save:
|
| 368 |
if st.button("💾 Save", key=f"save_{doc['id']}"):
|
| 369 |
try:
|
| 370 |
-
# Parse edited content
|
| 371 |
cleaned_content = sanitize_json_text(edited_content)
|
| 372 |
updated_doc = json.loads(cleaned_content)
|
| 373 |
-
# Preserve identity fields
|
| 374 |
updated_doc['id'] = doc['id']
|
| 375 |
updated_doc['pk'] = doc.get('pk', doc['id'])
|
| 376 |
-
# Remove system fields
|
| 377 |
for field in ['_ts', '_rid', '_self', '_etag', '_attachments']:
|
| 378 |
updated_doc.pop(field, None)
|
| 379 |
success, message = update_record(container, updated_doc)
|
| 380 |
if success:
|
| 381 |
st.success(f"Saved {doc['id']}")
|
| 382 |
-
st.session_state[
|
|
|
|
| 383 |
else:
|
| 384 |
st.error(message)
|
| 385 |
except json.JSONDecodeError as e:
|
|
@@ -391,8 +394,8 @@ def edit_all_documents(container):
|
|
| 391 |
success, message = delete_record(container, doc)
|
| 392 |
if success:
|
| 393 |
st.success(message)
|
| 394 |
-
if
|
| 395 |
-
del st.session_state[
|
| 396 |
st.rerun()
|
| 397 |
else:
|
| 398 |
st.error(message)
|
|
|
|
| 155 |
def delete_record(container, record):
|
| 156 |
try:
|
| 157 |
doc_id = record["id"]
|
| 158 |
+
partition_key_value = record.get("pk", doc_id)
|
| 159 |
st.write(f"Deleting {doc_id} with partition key {partition_key_value}") # Debug log
|
| 160 |
container.delete_item(item=doc_id, partition_key=partition_key_value)
|
| 161 |
return True, f"Record {doc_id} deleted. 🗑️"
|
|
|
|
| 353 |
if not documents:
|
| 354 |
st.info("No documents in this container.")
|
| 355 |
return
|
| 356 |
+
|
| 357 |
+
# Initialize saved_docs if not present
|
| 358 |
+
if 'saved_docs' not in st.session_state:
|
| 359 |
+
st.session_state.saved_docs = {}
|
| 360 |
+
|
| 361 |
for doc in documents:
|
| 362 |
ts = doc.get("_ts", 0)
|
| 363 |
dt = datetime.fromtimestamp(ts) if ts else datetime.now()
|
|
|
|
| 365 |
header = f"{doc.get('name', 'Unnamed')} - {formatted_ts}"
|
| 366 |
with st.expander(header):
|
| 367 |
doc_key = f"editor_{doc['id']}"
|
| 368 |
+
# Use saved_docs for initial value if available, else original doc
|
| 369 |
+
initial_value = st.session_state.saved_docs.get(doc['id'], json.dumps(doc, indent=2))
|
| 370 |
+
edited_content = st.text_area("Edit JSON", value=initial_value, height=300, key=doc_key)
|
| 371 |
col_save, col_delete = st.columns(2)
|
| 372 |
with col_save:
|
| 373 |
if st.button("💾 Save", key=f"save_{doc['id']}"):
|
| 374 |
try:
|
|
|
|
| 375 |
cleaned_content = sanitize_json_text(edited_content)
|
| 376 |
updated_doc = json.loads(cleaned_content)
|
|
|
|
| 377 |
updated_doc['id'] = doc['id']
|
| 378 |
updated_doc['pk'] = doc.get('pk', doc['id'])
|
|
|
|
| 379 |
for field in ['_ts', '_rid', '_self', '_etag', '_attachments']:
|
| 380 |
updated_doc.pop(field, None)
|
| 381 |
success, message = update_record(container, updated_doc)
|
| 382 |
if success:
|
| 383 |
st.success(f"Saved {doc['id']}")
|
| 384 |
+
st.session_state.saved_docs[doc['id']] = json.dumps(updated_doc, indent=2)
|
| 385 |
+
st.rerun() # Refresh UI to reflect saved state
|
| 386 |
else:
|
| 387 |
st.error(message)
|
| 388 |
except json.JSONDecodeError as e:
|
|
|
|
| 394 |
success, message = delete_record(container, doc)
|
| 395 |
if success:
|
| 396 |
st.success(message)
|
| 397 |
+
if doc['id'] in st.session_state.saved_docs:
|
| 398 |
+
del st.session_state.saved_docs[doc['id']]
|
| 399 |
st.rerun()
|
| 400 |
else:
|
| 401 |
st.error(message)
|