Spaces:
Build error
Build error
Create utils/case_manager.py
Browse files- utils/case_manager.py +70 -0
utils/case_manager.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/case_manager.py
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from typing import Dict, List, Optional
|
| 6 |
+
import streamlit as st
|
| 7 |
+
|
| 8 |
+
class CaseManager:
|
| 9 |
+
def __init__(self, base_path: str = "data/cases"):
|
| 10 |
+
self.base_path = base_path
|
| 11 |
+
os.makedirs(base_path, exist_ok=True)
|
| 12 |
+
self._load_cases()
|
| 13 |
+
|
| 14 |
+
def _load_cases(self):
|
| 15 |
+
"""Load existing cases from storage"""
|
| 16 |
+
if 'cases' not in st.session_state:
|
| 17 |
+
st.session_state.cases = {}
|
| 18 |
+
for case_id in os.listdir(self.base_path):
|
| 19 |
+
case_path = os.path.join(self.base_path, case_id)
|
| 20 |
+
if os.path.isdir(case_path):
|
| 21 |
+
with open(os.path.join(case_path, 'metadata.json'), 'r') as f:
|
| 22 |
+
st.session_state.cases[case_id] = json.load(f)
|
| 23 |
+
|
| 24 |
+
def create_case(self, title: str, description: str, case_type: str) -> str:
|
| 25 |
+
"""Create a new case"""
|
| 26 |
+
case_id = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 27 |
+
case_path = os.path.join(self.base_path, case_id)
|
| 28 |
+
os.makedirs(case_path, exist_ok=True)
|
| 29 |
+
os.makedirs(os.path.join(case_path, 'documents'), exist_ok=True)
|
| 30 |
+
|
| 31 |
+
case_data = {
|
| 32 |
+
'id': case_id,
|
| 33 |
+
'title': title,
|
| 34 |
+
'description': description,
|
| 35 |
+
'case_type': case_type,
|
| 36 |
+
'created_at': datetime.now().isoformat(),
|
| 37 |
+
'documents': []
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
with open(os.path.join(case_path, 'metadata.json'), 'w') as f:
|
| 41 |
+
json.dump(case_data, f)
|
| 42 |
+
|
| 43 |
+
st.session_state.cases[case_id] = case_data
|
| 44 |
+
return case_id
|
| 45 |
+
|
| 46 |
+
def add_document(self, case_id: str, document_data: Dict) -> str:
|
| 47 |
+
"""Add document to case"""
|
| 48 |
+
case = st.session_state.cases.get(case_id)
|
| 49 |
+
if not case:
|
| 50 |
+
raise ValueError(f"Case {case_id} not found")
|
| 51 |
+
|
| 52 |
+
doc_id = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 53 |
+
document_data['id'] = doc_id
|
| 54 |
+
document_data['added_at'] = datetime.now().isoformat()
|
| 55 |
+
|
| 56 |
+
case['documents'].append(document_data)
|
| 57 |
+
|
| 58 |
+
case_path = os.path.join(self.base_path, case_id)
|
| 59 |
+
with open(os.path.join(case_path, 'metadata.json'), 'w') as f:
|
| 60 |
+
json.dump(case, f)
|
| 61 |
+
|
| 62 |
+
return doc_id
|
| 63 |
+
|
| 64 |
+
def get_case(self, case_id: str) -> Optional[Dict]:
|
| 65 |
+
"""Get case details"""
|
| 66 |
+
return st.session_state.cases.get(case_id)
|
| 67 |
+
|
| 68 |
+
def get_all_cases(self) -> List[Dict]:
|
| 69 |
+
"""Get all cases"""
|
| 70 |
+
return list(st.session_state.cases.values())
|