Spaces:
Sleeping
Sleeping
Update sheets_integration.py
Browse files- sheets_integration.py +136 -17
sheets_integration.py
CHANGED
@@ -1,8 +1,19 @@
|
|
1 |
import os
|
2 |
import json
|
|
|
|
|
3 |
import gspread
|
4 |
from google.oauth2 import service_account
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
class SheetsLogger:
|
8 |
def __init__(self):
|
@@ -32,15 +43,21 @@ class SheetsLogger:
|
|
32 |
# Initialize gspread client
|
33 |
self.gc = gspread.authorize(credentials)
|
34 |
|
35 |
-
# Open spreadsheet by ID
|
36 |
SHEET_ID = os.getenv('GOOGLE_SHEET_ID')
|
|
|
|
|
|
|
37 |
self.sheet = self.gc.open_by_key(SHEET_ID).sheet1
|
38 |
|
|
|
|
|
|
|
39 |
except Exception as e:
|
40 |
print(f"Error initializing Google Sheets client: {str(e)}")
|
41 |
self.gc = None
|
42 |
self.sheet = None
|
43 |
-
|
44 |
def initialize_sheet(self):
|
45 |
"""Initialize sheet with headers if needed"""
|
46 |
headers = [
|
@@ -54,8 +71,9 @@ class SheetsLogger:
|
|
54 |
"Urgency",
|
55 |
"User Summary",
|
56 |
"Technical Analysis",
|
57 |
-
"Status",
|
58 |
-
"Notes"
|
|
|
59 |
]
|
60 |
|
61 |
try:
|
@@ -63,17 +81,28 @@ class SheetsLogger:
|
|
63 |
existing_headers = self.sheet.row_values(1)
|
64 |
if not existing_headers:
|
65 |
self.sheet.insert_row(headers, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
except Exception as e:
|
67 |
print(f"Error initializing headers: {str(e)}")
|
68 |
-
|
69 |
def log_request(self, data):
|
70 |
"""Log a request to Google Sheets"""
|
71 |
if not self.sheet:
|
|
|
72 |
return False
|
73 |
|
74 |
try:
|
|
|
75 |
row = [
|
76 |
-
data.get('timestamp',
|
77 |
data.get('name', ''),
|
78 |
data.get('employee_id', ''),
|
79 |
data.get('email', ''),
|
@@ -83,25 +112,115 @@ class SheetsLogger:
|
|
83 |
data.get('urgency', ''),
|
84 |
data.get('user_summary', ''),
|
85 |
data.get('system_analysis', ''),
|
86 |
-
|
87 |
-
''
|
|
|
88 |
]
|
89 |
|
|
|
90 |
self.sheet.append_row(row)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
return True
|
92 |
|
93 |
except Exception as e:
|
94 |
print(f"Error logging to sheet: {str(e)}")
|
|
|
95 |
|
96 |
-
def
|
97 |
-
"""
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
try:
|
102 |
-
#
|
103 |
-
|
104 |
-
|
|
|
|
|
105 |
except Exception as e:
|
106 |
-
print(f"Error getting
|
107 |
return None
|
|
|
1 |
import os
|
2 |
import json
|
3 |
+
from datetime import datetime
|
4 |
+
from enum import Enum
|
5 |
import gspread
|
6 |
from google.oauth2 import service_account
|
7 |
+
|
8 |
+
class RequestStatus(Enum):
|
9 |
+
NEW = "New"
|
10 |
+
IN_PROGRESS = "In Progress"
|
11 |
+
PENDING_INFO = "Pending Additional Info"
|
12 |
+
DATA_COLLECTION = "Data Collection"
|
13 |
+
READY_FOR_REVIEW = "Ready for Review"
|
14 |
+
COMPLETED = "Completed"
|
15 |
+
CANCELLED = "Cancelled"
|
16 |
+
ON_HOLD = "On Hold"
|
17 |
|
18 |
class SheetsLogger:
|
19 |
def __init__(self):
|
|
|
43 |
# Initialize gspread client
|
44 |
self.gc = gspread.authorize(credentials)
|
45 |
|
46 |
+
# Open spreadsheet by ID
|
47 |
SHEET_ID = os.getenv('GOOGLE_SHEET_ID')
|
48 |
+
if not SHEET_ID:
|
49 |
+
raise ValueError("Google Sheet ID not found in environment")
|
50 |
+
|
51 |
self.sheet = self.gc.open_by_key(SHEET_ID).sheet1
|
52 |
|
53 |
+
# Initialize headers if needed
|
54 |
+
self.initialize_sheet()
|
55 |
+
|
56 |
except Exception as e:
|
57 |
print(f"Error initializing Google Sheets client: {str(e)}")
|
58 |
self.gc = None
|
59 |
self.sheet = None
|
60 |
+
|
61 |
def initialize_sheet(self):
|
62 |
"""Initialize sheet with headers if needed"""
|
63 |
headers = [
|
|
|
71 |
"Urgency",
|
72 |
"User Summary",
|
73 |
"Technical Analysis",
|
74 |
+
"Status",
|
75 |
+
"Notes",
|
76 |
+
"Last Updated"
|
77 |
]
|
78 |
|
79 |
try:
|
|
|
81 |
existing_headers = self.sheet.row_values(1)
|
82 |
if not existing_headers:
|
83 |
self.sheet.insert_row(headers, 1)
|
84 |
+
# Format header row
|
85 |
+
self.sheet.format('A1:M1', {
|
86 |
+
"backgroundColor": {"red": 0.2, "green": 0.2, "blue": 0.2},
|
87 |
+
"textFormat": {"bold": True, "foregroundColor": {"red": 1, "green": 1, "blue": 1}}
|
88 |
+
})
|
89 |
+
# Set column widths
|
90 |
+
self.sheet.format('A:M', {
|
91 |
+
"wrapStrategy": "WRAP"
|
92 |
+
})
|
93 |
except Exception as e:
|
94 |
print(f"Error initializing headers: {str(e)}")
|
95 |
+
|
96 |
def log_request(self, data):
|
97 |
"""Log a request to Google Sheets"""
|
98 |
if not self.sheet:
|
99 |
+
print("Sheet not initialized")
|
100 |
return False
|
101 |
|
102 |
try:
|
103 |
+
# Prepare row data
|
104 |
row = [
|
105 |
+
data.get('timestamp', datetime.now().strftime("%Y-%m-%d %H:%M:%S")),
|
106 |
data.get('name', ''),
|
107 |
data.get('employee_id', ''),
|
108 |
data.get('email', ''),
|
|
|
112 |
data.get('urgency', ''),
|
113 |
data.get('user_summary', ''),
|
114 |
data.get('system_analysis', ''),
|
115 |
+
RequestStatus.NEW.value, # Initial status
|
116 |
+
'', # Empty notes
|
117 |
+
datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Last updated
|
118 |
]
|
119 |
|
120 |
+
# Append row to sheet
|
121 |
self.sheet.append_row(row)
|
122 |
+
|
123 |
+
# Get the row number that was just added
|
124 |
+
last_row = len(self.sheet.get_all_values())
|
125 |
+
|
126 |
+
# Apply conditional formatting for status
|
127 |
+
self.apply_status_formatting(last_row)
|
128 |
+
|
129 |
+
print(f"Successfully logged request to row {last_row}")
|
130 |
return True
|
131 |
|
132 |
except Exception as e:
|
133 |
print(f"Error logging to sheet: {str(e)}")
|
134 |
+
return False
|
135 |
|
136 |
+
def apply_status_formatting(self, row_number):
|
137 |
+
"""Apply conditional formatting based on status"""
|
138 |
+
status_cell = f'K{row_number}'
|
139 |
+
formats = {
|
140 |
+
RequestStatus.NEW.value: {
|
141 |
+
"backgroundColor": {"red": 1, "green": 1, "blue": 0.8} # Light yellow
|
142 |
+
},
|
143 |
+
RequestStatus.IN_PROGRESS.value: {
|
144 |
+
"backgroundColor": {"red": 0.8, "green": 0.9, "blue": 1} # Light blue
|
145 |
+
},
|
146 |
+
RequestStatus.PENDING_INFO.value: {
|
147 |
+
"backgroundColor": {"red": 1, "green": 0.8, "blue": 0.8} # Light red
|
148 |
+
},
|
149 |
+
RequestStatus.DATA_COLLECTION.value: {
|
150 |
+
"backgroundColor": {"red": 0.8, "green": 1, "blue": 0.8} # Light green
|
151 |
+
},
|
152 |
+
RequestStatus.READY_FOR_REVIEW.value: {
|
153 |
+
"backgroundColor": {"red": 0.9, "green": 0.9, "blue": 1} # Light purple
|
154 |
+
},
|
155 |
+
RequestStatus.COMPLETED.value: {
|
156 |
+
"backgroundColor": {"red": 0.9, "green": 1, "blue": 0.9} # Lighter green
|
157 |
+
},
|
158 |
+
RequestStatus.CANCELLED.value: {
|
159 |
+
"backgroundColor": {"red": 0.9, "green": 0.9, "blue": 0.9} # Gray
|
160 |
+
},
|
161 |
+
RequestStatus.ON_HOLD.value: {
|
162 |
+
"backgroundColor": {"red": 1, "green": 0.9, "blue": 0.8} # Light orange
|
163 |
+
}
|
164 |
+
}
|
165 |
+
|
166 |
+
try:
|
167 |
+
status = self.sheet.acell(status_cell).value
|
168 |
+
if status in formats:
|
169 |
+
self.sheet.format(status_cell, formats[status])
|
170 |
+
except Exception as e:
|
171 |
+
print(f"Error applying formatting: {str(e)}")
|
172 |
+
|
173 |
+
def update_status(self, row_number, new_status, notes=None):
|
174 |
+
"""Update the status and notes of a request"""
|
175 |
+
try:
|
176 |
+
if not isinstance(new_status, str):
|
177 |
+
if isinstance(new_status, RequestStatus):
|
178 |
+
new_status = new_status.value
|
179 |
+
else:
|
180 |
+
raise ValueError("Invalid status type")
|
181 |
+
|
182 |
+
# Update status
|
183 |
+
self.sheet.update_cell(row_number, 11, new_status) # Column K
|
184 |
+
|
185 |
+
# Update notes if provided
|
186 |
+
if notes:
|
187 |
+
current_notes = self.sheet.cell(row_number, 12).value # Column L
|
188 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
189 |
+
updated_notes = f"{current_notes}\n{timestamp}: {notes}" if current_notes else f"{timestamp}: {notes}"
|
190 |
+
self.sheet.update_cell(row_number, 12, updated_notes)
|
191 |
+
|
192 |
+
# Update last updated timestamp
|
193 |
+
self.sheet.update_cell(row_number, 13, datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
194 |
+
|
195 |
+
# Apply formatting
|
196 |
+
self.apply_status_formatting(row_number)
|
197 |
|
198 |
+
return True
|
199 |
+
except Exception as e:
|
200 |
+
print(f"Error updating status: {str(e)}")
|
201 |
+
return False
|
202 |
+
|
203 |
+
def get_request_history(self, request_id=None, employee_id=None):
|
204 |
+
"""Get request history filtered by ID or employee"""
|
205 |
+
try:
|
206 |
+
all_data = self.sheet.get_all_records()
|
207 |
+
if request_id:
|
208 |
+
return [row for row in all_data if str(row.get('Employee ID', '')).strip() == str(request_id).strip()]
|
209 |
+
elif employee_id:
|
210 |
+
return [row for row in all_data if str(row.get('Employee ID', '')).strip() == str(employee_id).strip()]
|
211 |
+
return all_data
|
212 |
+
except Exception as e:
|
213 |
+
print(f"Error getting history: {str(e)}")
|
214 |
+
return []
|
215 |
+
|
216 |
+
def get_request_by_row(self, row_number):
|
217 |
+
"""Get a specific request by row number"""
|
218 |
try:
|
219 |
+
if row_number < 2: # Row 1 is headers
|
220 |
+
return None
|
221 |
+
row_data = self.sheet.row_values(row_number)
|
222 |
+
headers = self.sheet.row_values(1)
|
223 |
+
return dict(zip(headers, row_data))
|
224 |
except Exception as e:
|
225 |
+
print(f"Error getting row {row_number}: {str(e)}")
|
226 |
return None
|