Spaces:
Sleeping
Sleeping
Update task_operations.py
Browse files- task_operations.py +20 -5
task_operations.py
CHANGED
@@ -14,7 +14,6 @@ class TaskManager:
|
|
14 |
self.create_table()
|
15 |
|
16 |
def create_table(self):
|
17 |
-
# Create tasks table with unique id
|
18 |
query = """
|
19 |
CREATE TABLE IF NOT EXISTS tasks (
|
20 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
@@ -29,14 +28,17 @@ class TaskManager:
|
|
29 |
self.conn.commit()
|
30 |
|
31 |
def load_tasks(self):
|
32 |
-
# Load tasks from the database
|
33 |
query = "SELECT * FROM tasks;"
|
34 |
df = pd.read_sql_query(query, self.conn)
|
35 |
-
|
|
|
|
|
|
|
36 |
if not df.empty:
|
37 |
# Ensure 'Task Time' is in datetime format and id is an integer
|
38 |
df['Task Time'] = pd.to_datetime(df['task_time']) # Convert task_time to datetime
|
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',
|
@@ -45,12 +47,15 @@ class TaskManager:
|
|
45 |
'category': 'Category',
|
46 |
'id': 'Task ID' # Rename id to 'Task ID'
|
47 |
})
|
|
|
|
|
|
|
|
|
48 |
return df.to_dict(orient='records')
|
49 |
else:
|
50 |
return []
|
51 |
|
52 |
def save_task(self, task):
|
53 |
-
# Save task into the database
|
54 |
query = """
|
55 |
INSERT INTO tasks (task_name, task_time, task_duration_hours, task_duration_minutes, category)
|
56 |
VALUES (?, ?, ?, ?, ?);
|
@@ -72,12 +77,22 @@ class TaskManager:
|
|
72 |
st.session_state.tasks.append(task_entry)
|
73 |
|
74 |
def delete_task_by_id(self, task_id):
|
75 |
-
# Delete task by its ID
|
76 |
query = "DELETE FROM tasks WHERE id = ?;"
|
77 |
self.conn.execute(query, (task_id,))
|
78 |
self.conn.commit()
|
79 |
return True
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
def generate_report(self, timeframe):
|
82 |
df = pd.DataFrame(self.load_tasks())
|
83 |
if df.empty:
|
|
|
14 |
self.create_table()
|
15 |
|
16 |
def create_table(self):
|
|
|
17 |
query = """
|
18 |
CREATE TABLE IF NOT EXISTS tasks (
|
19 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
28 |
self.conn.commit()
|
29 |
|
30 |
def load_tasks(self):
|
|
|
31 |
query = "SELECT * FROM tasks;"
|
32 |
df = pd.read_sql_query(query, self.conn)
|
33 |
+
|
34 |
+
# Debug: Show the columns of the DataFrame immediately after loading
|
35 |
+
print("Loaded columns before processing:", df.columns.tolist())
|
36 |
+
|
37 |
if not df.empty:
|
38 |
# Ensure 'Task Time' is in datetime format and id is an integer
|
39 |
df['Task Time'] = pd.to_datetime(df['task_time']) # Convert task_time to datetime
|
40 |
df['id'] = df['id'].astype(int) # Ensure id is an integer
|
41 |
+
|
42 |
df = df.rename(columns={
|
43 |
'task_name': 'Task Name',
|
44 |
'task_time': 'Task Time',
|
|
|
47 |
'category': 'Category',
|
48 |
'id': 'Task ID' # Rename id to 'Task ID'
|
49 |
})
|
50 |
+
|
51 |
+
# Debug: Show the columns after renaming
|
52 |
+
print("Loaded columns after processing:", df.columns.tolist())
|
53 |
+
|
54 |
return df.to_dict(orient='records')
|
55 |
else:
|
56 |
return []
|
57 |
|
58 |
def save_task(self, task):
|
|
|
59 |
query = """
|
60 |
INSERT INTO tasks (task_name, task_time, task_duration_hours, task_duration_minutes, category)
|
61 |
VALUES (?, ?, ?, ?, ?);
|
|
|
77 |
st.session_state.tasks.append(task_entry)
|
78 |
|
79 |
def delete_task_by_id(self, task_id):
|
|
|
80 |
query = "DELETE FROM tasks WHERE id = ?;"
|
81 |
self.conn.execute(query, (task_id,))
|
82 |
self.conn.commit()
|
83 |
return True
|
84 |
|
85 |
+
def update_task(self, task_id, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
|
86 |
+
task_time_full = datetime.combine(datetime.today(), task_time)
|
87 |
+
query = """
|
88 |
+
UPDATE tasks
|
89 |
+
SET task_name = ?, task_time = ?, task_duration_hours = ?, task_duration_minutes = ?, category = ?
|
90 |
+
WHERE id = ?;
|
91 |
+
"""
|
92 |
+
self.conn.execute(query, (task_name, task_time_full, task_duration_hours, task_duration_minutes, task_category, task_id))
|
93 |
+
self.conn.commit()
|
94 |
+
return True
|
95 |
+
|
96 |
def generate_report(self, timeframe):
|
97 |
df = pd.DataFrame(self.load_tasks())
|
98 |
if df.empty:
|