cryogenic22 commited on
Commit
95f59bc
·
verified ·
1 Parent(s): 081071b

Delete utils/collections.py

Browse files
Files changed (1) hide show
  1. utils/collections.py +0 -128
utils/collections.py DELETED
@@ -1,128 +0,0 @@
1
- import sqlite3
2
- import streamlit as st
3
- from typing import Dict, List, Optional, Any
4
- from datetime import datetime
5
- from threading import Lock
6
-
7
- # Use the same lock from database
8
- from .database import conn_lock
9
-
10
- def create_collection_tables(conn: sqlite3.Connection) -> None:
11
- """Create collection-related tables."""
12
- try:
13
- with conn_lock:
14
- # Collections table
15
- conn.execute('''
16
- CREATE TABLE IF NOT EXISTS collections (
17
- id INTEGER PRIMARY KEY AUTOINCREMENT,
18
- name TEXT NOT NULL UNIQUE,
19
- description TEXT,
20
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
21
- )
22
- ''')
23
-
24
- # Document-Collections relationship table
25
- conn.execute('''
26
- CREATE TABLE IF NOT EXISTS document_collections (
27
- document_id INTEGER,
28
- collection_id INTEGER,
29
- added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
30
- PRIMARY KEY (document_id, collection_id),
31
- FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE,
32
- FOREIGN KEY (collection_id) REFERENCES collections (id) ON DELETE CASCADE
33
- )
34
- ''')
35
-
36
- conn.commit()
37
- except sqlite3.Error as e:
38
- st.error(f"Error creating collection tables: {e}")
39
-
40
- def get_collections(conn: sqlite3.Connection) -> List[Dict]:
41
- """Get all collections with their document counts."""
42
- try:
43
- with conn_lock:
44
- cursor = conn.cursor()
45
- cursor.execute('''
46
- SELECT
47
- c.id,
48
- c.name,
49
- c.description,
50
- c.created_at,
51
- COUNT(DISTINCT dc.document_id) as doc_count
52
- FROM collections c
53
- LEFT JOIN document_collections dc ON c.id = dc.collection_id
54
- GROUP BY c.id
55
- ORDER BY c.name
56
- ''')
57
-
58
- return [{
59
- 'id': row[0],
60
- 'name': row[1],
61
- 'description': row[2],
62
- 'created_at': row[3],
63
- 'doc_count': row[4]
64
- } for row in cursor.fetchall()]
65
-
66
- except sqlite3.Error as e:
67
- st.error(f"Error retrieving collections: {e}")
68
- return []
69
-
70
- def create_collection(conn: sqlite3.Connection, name: str, description: str = "") -> Optional[int]:
71
- """Create a new collection."""
72
- try:
73
- with conn_lock:
74
- cursor = conn.cursor()
75
- cursor.execute('''
76
- INSERT INTO collections (name, description)
77
- VALUES (?, ?)
78
- ''', (name, description))
79
- conn.commit()
80
- return cursor.lastrowid
81
-
82
- except sqlite3.Error as e:
83
- st.error(f"Error creating collection: {e}")
84
- return None
85
-
86
- def add_document_to_collection(conn: sqlite3.Connection, document_id: int, collection_id: int) -> bool:
87
- """Add a document to a collection."""
88
- try:
89
- with conn_lock:
90
- cursor = conn.cursor()
91
- cursor.execute('''
92
- INSERT OR IGNORE INTO document_collections (document_id, collection_id)
93
- VALUES (?, ?)
94
- ''', (document_id, collection_id))
95
- conn.commit()
96
- return True
97
-
98
- except sqlite3.Error as e:
99
- st.error(f"Error adding document to collection: {e}")
100
- return False
101
-
102
- def get_collection_documents(conn: sqlite3.Connection, collection_id: int) -> List[Dict]:
103
- """Get all documents in a specific collection."""
104
- try:
105
- with conn_lock:
106
- cursor = conn.cursor()
107
- cursor.execute('''
108
- SELECT
109
- d.id,
110
- d.name,
111
- d.content,
112
- d.upload_date
113
- FROM documents d
114
- JOIN document_collections dc ON d.id = dc.document_id
115
- WHERE dc.collection_id = ?
116
- ORDER BY d.upload_date DESC
117
- ''', (collection_id,))
118
-
119
- return [{
120
- 'id': row[0],
121
- 'name': row[1],
122
- 'content': row[2],
123
- 'upload_date': row[3]
124
- } for row in cursor.fetchall()]
125
-
126
- except sqlite3.Error as e:
127
- st.error(f"Error retrieving collection documents: {e}")
128
- return []