Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -811,56 +811,39 @@ def main():
|
|
| 811 |
st.markdown("#### Clone a document:")
|
| 812 |
|
| 813 |
for idx, doc in enumerate(documents_to_display):
|
| 814 |
-
st.markdown(f"##### Document ID: {doc.get('id', '')}")
|
| 815 |
|
| 816 |
if st.button("📄 Clone Document", key=f'clone_button_{idx}'):
|
| 817 |
-
#
|
| 818 |
-
|
|
|
|
| 819 |
|
| 820 |
-
#
|
| 821 |
-
|
| 822 |
-
|
|
|
|
|
|
|
|
|
|
| 823 |
|
| 824 |
-
#
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
cloned_doc['name'] = new_name
|
| 828 |
-
|
| 829 |
-
# Update nested ID and name fields if they exist
|
| 830 |
-
def update_nested_ids(obj):
|
| 831 |
-
if isinstance(obj, dict):
|
| 832 |
-
if 'id' in obj:
|
| 833 |
-
obj['id'] = new_id
|
| 834 |
-
if 'name' in obj:
|
| 835 |
-
obj['name'] = new_name
|
| 836 |
-
for value in obj.values():
|
| 837 |
-
update_nested_ids(value)
|
| 838 |
-
elif isinstance(obj, list):
|
| 839 |
-
for item in obj:
|
| 840 |
-
update_nested_ids(item)
|
| 841 |
-
|
| 842 |
-
update_nested_ids(cloned_doc)
|
| 843 |
-
|
| 844 |
-
# Display editable JSON
|
| 845 |
-
edited_doc_str = st.text_area(
|
| 846 |
-
"Edit cloned document before saving:",
|
| 847 |
value=json.dumps(cloned_doc, indent=2),
|
| 848 |
-
height=300
|
|
|
|
| 849 |
)
|
| 850 |
|
| 851 |
-
if st.button("💾 Save
|
| 852 |
try:
|
| 853 |
-
final_doc = json.loads(
|
| 854 |
success, message = insert_record(container, final_doc)
|
| 855 |
-
|
| 856 |
if success:
|
| 857 |
-
st.success(f"Cloned document saved
|
| 858 |
st.rerun()
|
| 859 |
else:
|
| 860 |
-
st.error(
|
| 861 |
except json.JSONDecodeError as e:
|
| 862 |
st.error(f"Invalid JSON format: {str(e)}")
|
| 863 |
-
|
| 864 |
|
| 865 |
elif selected_view == 'New Record':
|
| 866 |
# 🆕 New Record
|
|
|
|
| 811 |
st.markdown("#### Clone a document:")
|
| 812 |
|
| 813 |
for idx, doc in enumerate(documents_to_display):
|
| 814 |
+
st.markdown(f"##### Original Document ID: {doc.get('id', '')}")
|
| 815 |
|
| 816 |
if st.button("📄 Clone Document", key=f'clone_button_{idx}'):
|
| 817 |
+
# Generate new unique identifiers
|
| 818 |
+
new_id = str(uuid.uuid4())
|
| 819 |
+
new_name = f"Clone_{str(uuid.uuid4())[:8]}"
|
| 820 |
|
| 821 |
+
# Create new document with unique ID and name
|
| 822 |
+
cloned_doc = {
|
| 823 |
+
'id': new_id,
|
| 824 |
+
'name': new_name,
|
| 825 |
+
**{k: v for k, v in doc.items() if k not in ['id', 'name']}
|
| 826 |
+
}
|
| 827 |
|
| 828 |
+
# Show editable preview
|
| 829 |
+
edited_doc = st.text_area(
|
| 830 |
+
"Edit cloned document:",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 831 |
value=json.dumps(cloned_doc, indent=2),
|
| 832 |
+
height=300,
|
| 833 |
+
key=f'edit_clone_{idx}'
|
| 834 |
)
|
| 835 |
|
| 836 |
+
if st.button("💾 Save Clone", key=f'save_clone_{idx}'):
|
| 837 |
try:
|
| 838 |
+
final_doc = json.loads(edited_doc)
|
| 839 |
success, message = insert_record(container, final_doc)
|
|
|
|
| 840 |
if success:
|
| 841 |
+
st.success(f"Cloned document saved with ID: {new_id}")
|
| 842 |
st.rerun()
|
| 843 |
else:
|
| 844 |
+
st.error(message)
|
| 845 |
except json.JSONDecodeError as e:
|
| 846 |
st.error(f"Invalid JSON format: {str(e)}")
|
|
|
|
| 847 |
|
| 848 |
elif selected_view == 'New Record':
|
| 849 |
# 🆕 New Record
|