shamimjony1000 commited on
Commit
3065bc5
·
verified ·
1 Parent(s): 2d9495e

Update task_operations.py

Browse files
Files changed (1) hide show
  1. task_operations.py +37 -24
task_operations.py CHANGED
@@ -13,37 +13,46 @@ class TaskManager:
13
  st.session_state.tasks = self.load_tasks()
14
 
15
  def load_tasks(self):
16
- if os.path.exists(self.TASKS_FILE):
17
- if os.path.getsize(self.TASKS_FILE) > 0:
18
- try:
19
- return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
20
- except pd.errors.EmptyDataError:
21
- return [] # Return an empty list if CSV is empty
22
- else:
23
  return []
24
  else:
25
- # If the file doesn't exist, create it with the correct columns
26
  df = pd.DataFrame(columns=["Task ID", "Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
27
  df.to_csv(self.TASKS_FILE, index=False)
28
- return []
29
 
30
  def save_tasks(self, tasks):
31
- df = pd.DataFrame(tasks)
32
- df.to_csv(self.TASKS_FILE, index=False)
 
 
 
 
33
 
34
  def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
35
- task_time_full = datetime.combine(datetime.today(), task_time)
36
- task_id = str(uuid.uuid4()) # Generate a unique ID using uuid4
37
- task_entry = {
38
- "Task ID": task_id,
39
- "Task Name": task_name,
40
- "Task Time": task_time_full,
41
- "Task Duration (hours)": int(task_duration_hours),
42
- "Task Duration (minutes)": int(task_duration_minutes),
43
- "Category": task_category
44
- }
45
- st.session_state.tasks.append(task_entry)
46
- self.save_tasks(st.session_state.tasks)
 
 
 
 
 
47
 
48
  def delete_task_by_id(self, task_id):
49
  task_found = False
@@ -52,7 +61,11 @@ class TaskManager:
52
  st.session_state.tasks.pop(index)
53
  task_found = True
54
  break
55
- self.save_tasks(st.session_state.tasks)
 
 
 
 
56
  return task_found
57
 
58
  def generate_report(self, timeframe):
 
13
  st.session_state.tasks = self.load_tasks()
14
 
15
  def load_tasks(self):
16
+ # Check if the file exists and has data
17
+ if os.path.exists(self.TASKS_FILE) and os.path.getsize(self.TASKS_FILE) > 0:
18
+ try:
19
+ # Load tasks into session state from CSV
20
+ return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
21
+ except pd.errors.EmptyDataError:
22
+ # If the file is empty, return an empty list
23
  return []
24
  else:
25
+ # If file doesn't exist, initialize it with an empty DataFrame and correct columns
26
  df = pd.DataFrame(columns=["Task ID", "Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
27
  df.to_csv(self.TASKS_FILE, index=False)
28
+ return [] # Return an empty list to the session state
29
 
30
  def save_tasks(self, tasks):
31
+ if tasks: # Only save if tasks are not empty
32
+ df = pd.DataFrame(tasks)
33
+ df.to_csv(self.TASKS_FILE, index=False)
34
+ else:
35
+ # Ensure the file is not accidentally overwritten with an empty DataFrame
36
+ st.warning("No tasks to save. Skipping file write to prevent data loss.")
37
 
38
  def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
39
+ # Ensure valid task details are being added
40
+ if task_name and task_category:
41
+ task_time_full = datetime.combine(datetime.today(), task_time)
42
+ task_id = str(uuid.uuid4()) # Generate a unique ID using uuid4
43
+ task_entry = {
44
+ "Task ID": task_id,
45
+ "Task Name": task_name,
46
+ "Task Time": task_time_full,
47
+ "Task Duration (hours)": int(task_duration_hours),
48
+ "Task Duration (minutes)": int(task_duration_minutes),
49
+ "Category": task_category
50
+ }
51
+ st.session_state.tasks.append(task_entry)
52
+ self.save_tasks(st.session_state.tasks)
53
+ st.success(f"Task '{task_name}' added successfully!")
54
+ else:
55
+ st.error("Task name and category are required.")
56
 
57
  def delete_task_by_id(self, task_id):
58
  task_found = False
 
61
  st.session_state.tasks.pop(index)
62
  task_found = True
63
  break
64
+ if task_found:
65
+ self.save_tasks(st.session_state.tasks)
66
+ st.success(f"Task with ID '{task_id}' deleted.")
67
+ else:
68
+ st.error(f"Task with ID '{task_id}' not found.")
69
  return task_found
70
 
71
  def generate_report(self, timeframe):