Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -831,33 +831,64 @@ def main():
|
|
831 |
# Button to initiate cloning of the document
|
832 |
if st.button("๐ Clone Document", key=f'clone_button_{idx}'):
|
833 |
with st.spinner("Cloning document..."):
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
|
|
|
|
|
|
847 |
|
848 |
-
|
849 |
-
st.success(f"Cloned document saved successfully with ID: {cloned_doc['id']} ๐")
|
850 |
-
|
851 |
-
# Refresh session state documents to show the new cloned document
|
852 |
st.session_state.documents = list(container.query_items(
|
853 |
query="SELECT * FROM c ORDER BY c._ts DESC",
|
854 |
enable_cross_partition_query=True
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
861 |
|
862 |
# If clone mode is activated, allow user to edit the cloned document
|
863 |
if st.session_state.get('clone_mode', False):
|
|
|
831 |
# Button to initiate cloning of the document
|
832 |
if st.button("๐ Clone Document", key=f'clone_button_{idx}'):
|
833 |
with st.spinner("Cloning document..."):
|
834 |
+
|
835 |
+
try:
|
836 |
+
existing_doc = container.read_item(item=clone_id, partition_key=clone_id)
|
837 |
+
new_doc = existing_doc.copy()
|
838 |
+
new_doc['id'] = generate_unique_id() # Generate new unique ID with timestamp
|
839 |
+
new_doc['name'] = new_doc['id'] # Generate new unique ID with timestamp
|
840 |
+
new_doc['createdAt'] = datetime.utcnow().isoformat() # Update the creation time
|
841 |
+
new_doc['_rid'] = None # Reset _rid or any system-managed fields
|
842 |
+
new_doc['_self'] = None
|
843 |
+
new_doc['_etag'] = None
|
844 |
+
new_doc['_attachments'] = None
|
845 |
+
new_doc['_ts'] = None # Reset timestamp to be updated by Cosmos DB automatically
|
846 |
+
|
847 |
+
# Insert the cloned document
|
848 |
+
response = container.create_item(body=new_doc)
|
849 |
+
st.success(f"Cloned document saved successfully with ID: {new_doc['id']} ๐")
|
850 |
|
851 |
+
# Refresh the documents in session state
|
|
|
|
|
|
|
852 |
st.session_state.documents = list(container.query_items(
|
853 |
query="SELECT * FROM c ORDER BY c._ts DESC",
|
854 |
enable_cross_partition_query=True
|
855 |
+
))
|
856 |
+
|
857 |
+
except exceptions.CosmosResourceNotFoundError:
|
858 |
+
st.error(f"Document with ID {clone_id} not found for cloning.")
|
859 |
+
except exceptions.CosmosHttpResponseError as e:
|
860 |
+
st.error(f"HTTP error occurred: {str(e)} ๐จ")
|
861 |
+
except Exception as e:
|
862 |
+
st.error(f"An unexpected error occurred: {str(e)} ๐ฑ")
|
863 |
+
|
864 |
+
|
865 |
+
#try:
|
866 |
+
# Copy the document and reset system-managed fields
|
867 |
+
cloned_doc = doc.copy()
|
868 |
+
cloned_doc['id'] = generate_unique_id() # Generate new unique ID
|
869 |
+
cloned_doc['name'] = generate_unique_id() # Generate new unique ID
|
870 |
+
cloned_doc['createdAt'] = datetime.utcnow().isoformat() # Set new creation time
|
871 |
+
cloned_doc['_rid'] = None # Clear system-managed fields
|
872 |
+
cloned_doc['_self'] = None
|
873 |
+
cloned_doc['_etag'] = None
|
874 |
+
cloned_doc['_attachments'] = None
|
875 |
+
cloned_doc['_ts'] = None
|
876 |
+
|
877 |
+
# Save the cloned document
|
878 |
+
|
879 |
+
response = container.create_item(body=cloned_doc)
|
880 |
+
st.success(f"Cloned document saved successfully with ID: {cloned_doc['id']} ๐")
|
881 |
+
|
882 |
+
# Refresh session state documents to show the new cloned document
|
883 |
+
st.session_state.documents = list(container.query_items(
|
884 |
+
query="SELECT * FROM c ORDER BY c._ts DESC",
|
885 |
+
enable_cross_partition_query=True
|
886 |
+
))
|
887 |
+
|
888 |
+
#except exceptions.CosmosHttpResponseError as e:
|
889 |
+
# st.error(f"HTTP error occurred: {str(e)} ๐จ")
|
890 |
+
#except Exception as e:
|
891 |
+
# st.error(f"An unexpected error occurred: {str(e)} ๐ฑ")
|
892 |
|
893 |
# If clone mode is activated, allow user to edit the cloned document
|
894 |
if st.session_state.get('clone_mode', False):
|