Create pages/document_store.py
Browse files- pages/document_store.py +82 -0
pages/document_store.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pages/document_store.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from utils.collections import *
|
| 5 |
+
from utils.database import create_connection
|
| 6 |
+
|
| 7 |
+
def display_collection_manager():
|
| 8 |
+
"""Display the collection management interface."""
|
| 9 |
+
st.header("Collection Management")
|
| 10 |
+
|
| 11 |
+
# Get database connection from session state
|
| 12 |
+
conn = st.session_state.db_conn
|
| 13 |
+
|
| 14 |
+
# Create new collection section
|
| 15 |
+
with st.expander("Create New Collection", expanded=False):
|
| 16 |
+
col1, col2 = st.columns([3, 1])
|
| 17 |
+
with col1:
|
| 18 |
+
name = st.text_input("Collection Name")
|
| 19 |
+
description = st.text_area("Description")
|
| 20 |
+
with col2:
|
| 21 |
+
if st.button("Create Collection") and name:
|
| 22 |
+
if create_collection(conn, name, description):
|
| 23 |
+
st.success(f"Collection '{name}' created!")
|
| 24 |
+
st.rerun()
|
| 25 |
+
|
| 26 |
+
# Display existing collections
|
| 27 |
+
collections = get_collections(conn)
|
| 28 |
+
if collections:
|
| 29 |
+
st.subheader("Existing Collections")
|
| 30 |
+
|
| 31 |
+
for collection in collections:
|
| 32 |
+
with st.expander(f"📁 {collection['name']} ({collection['doc_count']} documents)"):
|
| 33 |
+
# Collection details
|
| 34 |
+
st.write(f"**Description:** {collection['description']}")
|
| 35 |
+
st.write(f"**Created:** {collection['created_at']}")
|
| 36 |
+
|
| 37 |
+
# Get documents in this collection
|
| 38 |
+
documents = get_collection_documents(conn, collection['id'])
|
| 39 |
+
|
| 40 |
+
# Display documents
|
| 41 |
+
if documents:
|
| 42 |
+
st.write("**Documents:**")
|
| 43 |
+
for doc in documents:
|
| 44 |
+
st.write(f"• {doc['name']}")
|
| 45 |
+
|
| 46 |
+
# Collection management buttons
|
| 47 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 48 |
+
with col1:
|
| 49 |
+
if st.button("Add Documents", key=f"add_{collection['id']}"):
|
| 50 |
+
st.session_state.selected_collection = collection['id']
|
| 51 |
+
st.session_state.show_upload = True
|
| 52 |
+
with col2:
|
| 53 |
+
if st.button("Edit", key=f"edit_{collection['id']}"):
|
| 54 |
+
st.session_state.editing_collection = collection['id']
|
| 55 |
+
with col3:
|
| 56 |
+
if st.button("Delete", key=f"del_{collection['id']}"):
|
| 57 |
+
if delete_collection(conn, collection['id']):
|
| 58 |
+
st.success(f"Collection '{collection['name']}' deleted!")
|
| 59 |
+
st.rerun()
|
| 60 |
+
|
| 61 |
+
def handle_document_upload_with_collection(uploaded_files, collection_id):
|
| 62 |
+
"""Handle document upload with collection assignment."""
|
| 63 |
+
conn = st.session_state.db_conn
|
| 64 |
+
|
| 65 |
+
for uploaded_file in uploaded_files:
|
| 66 |
+
# Your existing document processing code here
|
| 67 |
+
document_id = process_and_store_document(uploaded_file)
|
| 68 |
+
|
| 69 |
+
if document_id:
|
| 70 |
+
# Add to collection
|
| 71 |
+
if add_document_to_collection(conn, document_id, collection_id):
|
| 72 |
+
st.success(f"Added {uploaded_file.name} to collection!")
|
| 73 |
+
else:
|
| 74 |
+
st.error(f"Failed to add {uploaded_file.name} to collection!")
|
| 75 |
+
|
| 76 |
+
# Initialize required session state
|
| 77 |
+
if 'show_upload' not in st.session_state:
|
| 78 |
+
st.session_state.show_upload = False
|
| 79 |
+
if 'selected_collection' not in st.session_state:
|
| 80 |
+
st.session_state.selected_collection = None
|
| 81 |
+
if 'editing_collection' not in st.session_state:
|
| 82 |
+
st.session_state.editing_collection = None
|