awacke1 commited on
Commit
e5f0c31
β€’
1 Parent(s): 5004f2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -11,6 +11,8 @@ from git import Repo
11
  from datetime import datetime
12
  import base64
13
  import json
 
 
14
 
15
  # πŸŽ‰ Welcome to our fun-filled Cosmos DB and GitHub Integration app!
16
  st.set_page_config(layout="wide")
@@ -92,7 +94,7 @@ def insert_record(container, record):
92
  container.create_item(body=record)
93
  return True, "Record inserted successfully! πŸŽ‰"
94
  except exceptions.CosmosHttpResponseError as e:
95
- return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code} 🚨"
96
  except Exception as e:
97
  return False, f"An unexpected error occurred: {str(e)} 😱"
98
 
@@ -101,7 +103,7 @@ def update_record(container, updated_record):
101
  container.upsert_item(body=updated_record)
102
  return True, f"Record with id {updated_record['id']} successfully updated. πŸ› οΈ"
103
  except exceptions.CosmosHttpResponseError as e:
104
- return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code} 🚨"
105
  except Exception as e:
106
  return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
107
 
@@ -112,10 +114,15 @@ def delete_record(container, name, id):
112
  except exceptions.CosmosResourceNotFoundError:
113
  return False, f"Record with id {id} not found. It may have been already deleted. πŸ•΅οΈβ€β™‚οΈ"
114
  except exceptions.CosmosHttpResponseError as e:
115
- return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code} 🚨"
116
  except Exception as e:
117
  return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
118
 
 
 
 
 
 
119
  # πŸ“¦ Function to archive current container
120
  def archive_current_container(database_name, container_name, client):
121
  try:
@@ -171,7 +178,7 @@ def main():
171
  if input_key:
172
  st.session_state.primary_key = input_key
173
  st.session_state.logged_in = True
174
- st.rerun()
175
  else:
176
  st.error("Invalid key. Please check your input. πŸ”‘βŒ")
177
  else:
@@ -189,7 +196,7 @@ def main():
189
  if selected_db != st.session_state.selected_database:
190
  st.session_state.selected_database = selected_db
191
  st.session_state.selected_container = None
192
- st.rerun()
193
 
194
  if st.session_state.selected_database:
195
  database = st.session_state.client.get_database_client(st.session_state.selected_database)
@@ -198,7 +205,7 @@ def main():
198
 
199
  if selected_container != st.session_state.selected_container:
200
  st.session_state.selected_container = selected_container
201
- st.rerun()
202
 
203
  if st.session_state.selected_container:
204
  container = database.get_container_client(st.session_state.selected_container)
@@ -257,8 +264,8 @@ def main():
257
  # 🧬 Clone Document
258
  if st.button("πŸ“„ Clone Document"):
259
  cloned_doc = selected_doc.copy()
260
- timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
261
- cloned_doc['id'] = f"{cloned_doc['id']}_clone_{timestamp}"
262
  success, message = insert_record(container, cloned_doc)
263
  if success:
264
  st.success(f"Document cloned with new id: {cloned_doc['id']} πŸŽ‰")
@@ -271,7 +278,7 @@ def main():
271
  try:
272
  new_doc = json.loads(new_doc_str)
273
  if 'id' not in new_doc:
274
- new_doc['id'] = f"new_doc_{datetime.now().strftime('%Y%m%d%H%M%S')}"
275
  success, message = insert_record(container, new_doc)
276
  if success:
277
  st.success(f"New document created with id: {new_doc['id']} πŸŽ‰")
@@ -334,7 +341,7 @@ def main():
334
  st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. πŸ”‘β“")
335
 
336
  except exceptions.CosmosHttpResponseError as e:
337
- st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)}. Status code: {e.status_code} 🚨")
338
  except Exception as e:
339
  st.error(f"An unexpected error occurred: {str(e)} 😱")
340
 
@@ -345,7 +352,10 @@ def main():
345
  st.session_state.client = None
346
  st.session_state.selected_database = None
347
  st.session_state.selected_container = None
348
- st.rerun()
349
 
350
  if __name__ == "__main__":
351
  main()
 
 
 
 
11
  from datetime import datetime
12
  import base64
13
  import json
14
+ import random
15
+ import string # 🎲 For generating random IDs
16
 
17
  # πŸŽ‰ Welcome to our fun-filled Cosmos DB and GitHub Integration app!
18
  st.set_page_config(layout="wide")
 
94
  container.create_item(body=record)
95
  return True, "Record inserted successfully! πŸŽ‰"
96
  except exceptions.CosmosHttpResponseError as e:
97
+ return False, f"HTTP error occurred: {str(e)} 🚨"
98
  except Exception as e:
99
  return False, f"An unexpected error occurred: {str(e)} 😱"
100
 
 
103
  container.upsert_item(body=updated_record)
104
  return True, f"Record with id {updated_record['id']} successfully updated. πŸ› οΈ"
105
  except exceptions.CosmosHttpResponseError as e:
106
+ return False, f"HTTP error occurred: {str(e)} 🚨"
107
  except Exception as e:
108
  return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
109
 
 
114
  except exceptions.CosmosResourceNotFoundError:
115
  return False, f"Record with id {id} not found. It may have been already deleted. πŸ•΅οΈβ€β™‚οΈ"
116
  except exceptions.CosmosHttpResponseError as e:
117
+ return False, f"HTTP error occurred: {str(e)} 🚨"
118
  except Exception as e:
119
  return False, f"An unexpected error occurred: {traceback.format_exc()} 😱"
120
 
121
+ # 🎲 Function to generate a short, unique alphanumeric ID
122
+ def generate_short_id(length=8):
123
+ # πŸ§™β€β™‚οΈ Generating a short, unique ID!
124
+ return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
125
+
126
  # πŸ“¦ Function to archive current container
127
  def archive_current_container(database_name, container_name, client):
128
  try:
 
178
  if input_key:
179
  st.session_state.primary_key = input_key
180
  st.session_state.logged_in = True
181
+ st.experimental_rerun()
182
  else:
183
  st.error("Invalid key. Please check your input. πŸ”‘βŒ")
184
  else:
 
196
  if selected_db != st.session_state.selected_database:
197
  st.session_state.selected_database = selected_db
198
  st.session_state.selected_container = None
199
+ st.experimental_rerun()
200
 
201
  if st.session_state.selected_database:
202
  database = st.session_state.client.get_database_client(st.session_state.selected_database)
 
205
 
206
  if selected_container != st.session_state.selected_container:
207
  st.session_state.selected_container = selected_container
208
+ st.experimental_rerun()
209
 
210
  if st.session_state.selected_container:
211
  container = database.get_container_client(st.session_state.selected_container)
 
264
  # 🧬 Clone Document
265
  if st.button("πŸ“„ Clone Document"):
266
  cloned_doc = selected_doc.copy()
267
+ # Generate a unique, short ID for the cloned document
268
+ cloned_doc['id'] = generate_short_id()
269
  success, message = insert_record(container, cloned_doc)
270
  if success:
271
  st.success(f"Document cloned with new id: {cloned_doc['id']} πŸŽ‰")
 
278
  try:
279
  new_doc = json.loads(new_doc_str)
280
  if 'id' not in new_doc:
281
+ new_doc['id'] = generate_short_id()
282
  success, message = insert_record(container, new_doc)
283
  if success:
284
  st.success(f"New document created with id: {new_doc['id']} πŸŽ‰")
 
341
  st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. πŸ”‘β“")
342
 
343
  except exceptions.CosmosHttpResponseError as e:
344
+ st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} 🚨")
345
  except Exception as e:
346
  st.error(f"An unexpected error occurred: {str(e)} 😱")
347
 
 
352
  st.session_state.client = None
353
  st.session_state.selected_database = None
354
  st.session_state.selected_container = None
355
+ st.experimental_rerun()
356
 
357
  if __name__ == "__main__":
358
  main()
359
+
360
+
361
+