Spaces:
Sleeping
Sleeping
shamimjony1000
commited on
Update task_operations.py
Browse files- task_operations.py +23 -12
task_operations.py
CHANGED
@@ -14,15 +14,15 @@ class TaskManager:
|
|
14 |
self.create_table()
|
15 |
|
16 |
def create_table(self):
|
17 |
-
# Create tasks table with
|
18 |
query = """
|
19 |
CREATE TABLE IF NOT EXISTS tasks (
|
20 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
21 |
-
task_name TEXT,
|
22 |
-
task_time TEXT,
|
23 |
-
task_duration_hours INTEGER,
|
24 |
-
task_duration_minutes INTEGER,
|
25 |
-
category TEXT
|
26 |
);
|
27 |
"""
|
28 |
self.conn.execute(query)
|
@@ -34,17 +34,28 @@ class TaskManager:
|
|
34 |
df = pd.read_sql_query(query, self.conn)
|
35 |
|
36 |
if not df.empty:
|
37 |
-
#
|
38 |
-
|
39 |
-
df['id'] = df['id'].astype(int) # Ensure id is an integer
|
40 |
-
df = df.rename(columns={
|
41 |
'task_name': 'Task Name',
|
42 |
'task_time': 'Task Time',
|
43 |
'task_duration_hours': 'Task Duration (hours)',
|
44 |
'task_duration_minutes': 'Task Duration (minutes)',
|
45 |
'category': 'Category',
|
46 |
-
'id': 'Task ID'
|
47 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
return df.to_dict(orient='records')
|
49 |
else:
|
50 |
return []
|
|
|
14 |
self.create_table()
|
15 |
|
16 |
def create_table(self):
|
17 |
+
# Create tasks table with essential fields
|
18 |
query = """
|
19 |
CREATE TABLE IF NOT EXISTS tasks (
|
20 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
21 |
+
task_name TEXT NOT NULL,
|
22 |
+
task_time TEXT NOT NULL,
|
23 |
+
task_duration_hours INTEGER NOT NULL,
|
24 |
+
task_duration_minutes INTEGER NOT NULL,
|
25 |
+
category TEXT NOT NULL
|
26 |
);
|
27 |
"""
|
28 |
self.conn.execute(query)
|
|
|
34 |
df = pd.read_sql_query(query, self.conn)
|
35 |
|
36 |
if not df.empty:
|
37 |
+
# Rename columns explicitly to avoid conflicts
|
38 |
+
column_mapping = {
|
|
|
|
|
39 |
'task_name': 'Task Name',
|
40 |
'task_time': 'Task Time',
|
41 |
'task_duration_hours': 'Task Duration (hours)',
|
42 |
'task_duration_minutes': 'Task Duration (minutes)',
|
43 |
'category': 'Category',
|
44 |
+
'id': 'Task ID'
|
45 |
+
}
|
46 |
+
|
47 |
+
# Check for any conflicts before renaming
|
48 |
+
existing_columns = set(df.columns)
|
49 |
+
for original, new_name in column_mapping.items():
|
50 |
+
if new_name in existing_columns and original != new_name:
|
51 |
+
raise ValueError(f"Conflict detected: Column '{new_name}' already exists in the DataFrame.")
|
52 |
+
|
53 |
+
# Rename columns in the DataFrame
|
54 |
+
df.rename(columns=column_mapping, inplace=True)
|
55 |
+
|
56 |
+
# Convert 'task_time' to datetime after renaming
|
57 |
+
df['Task Time'] = pd.to_datetime(df['Task Time'])
|
58 |
+
|
59 |
return df.to_dict(orient='records')
|
60 |
else:
|
61 |
return []
|