shamimjony1000 commited on
Commit
69e3818
·
verified ·
1 Parent(s): 084e363

Update task_operations.py

Browse files
Files changed (1) hide show
  1. task_operations.py +24 -80
task_operations.py CHANGED
@@ -1,84 +1,28 @@
1
- # components/task_operations.py
2
-
3
- import pandas as pd
4
- import os
5
- from datetime import datetime, time
6
- import streamlit as st
7
- import pytz
8
-
9
  class TaskManager:
10
- TASKS_FILE = "tasks.csv"
11
- CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']
12
-
13
  def __init__(self):
14
- # Initialize the timezones properly
15
- self.local_timezone = pytz.timezone('Etc/GMT-6') # Adjusted for GMT+6 time zone
16
- self.utc_timezone = pytz.utc # UTC timezone
17
-
18
- if 'tasks' not in st.session_state:
19
- st.session_state.tasks = self.load_tasks()
20
-
21
- def load_tasks(self):
22
- if os.path.exists(self.TASKS_FILE):
23
- if os.path.getsize(self.TASKS_FILE) > 0:
24
- tasks = pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
25
- # Convert stored UTC times to local time
26
- for task in tasks:
27
- task['Task Time'] = pd.Timestamp(task['Task Time']).tz_localize(self.utc_timezone).tz_convert(self.local_timezone)
28
- return tasks
29
- else:
30
- return []
 
 
 
 
31
  else:
32
- df = pd.DataFrame(columns=["Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
33
- df.to_csv(self.TASKS_FILE, index=False)
34
- return []
35
-
36
- def save_tasks(self, tasks):
37
- # Convert local time back to UTC for saving
38
- tasks_copy = []
39
- for task in tasks:
40
- task_copy = task.copy()
41
- task_copy['Task Time'] = task_copy['Task Time'].astimezone(self.utc_timezone)
42
- tasks_copy.append(task_copy)
43
-
44
- df = pd.DataFrame(tasks_copy)
45
- df.to_csv(self.TASKS_FILE, index=False)
46
-
47
- def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category):
48
- task_time_full = datetime.combine(datetime.today(), task_time)
49
- # Localize to your local timezone and then convert to UTC for consistency
50
- task_time_full = self.local_timezone.localize(task_time_full).astimezone(self.utc_timezone)
51
- task_entry = {
52
- "Task Name": task_name,
53
- "Task Time": task_time_full,
54
- "Task Duration (hours)": int(task_duration_hours),
55
- "Task Duration (minutes)": int(task_duration_minutes),
56
- "Category": task_category
57
- }
58
- st.session_state.tasks.append(task_entry)
59
- self.save_tasks(st.session_state.tasks)
60
-
61
- def generate_report(self, timeframe):
62
- df = pd.DataFrame(st.session_state.tasks)
63
- if df.empty:
64
- return pd.DataFrame() # Return empty DataFrame if no tasks are present
65
-
66
- # Ensure 'Task Time' is already timezone-aware and convert it to local time for the report
67
- df['Task Time'] = pd.to_datetime(df['Task Time']).apply(lambda x: x.tz_convert(self.local_timezone))
68
-
69
- today = pd.Timestamp.now(tz=self.local_timezone).date()
70
-
71
- if timeframe == 'daily':
72
- report = df[df['Task Time'].dt.date == today]
73
- elif timeframe == 'weekly':
74
- week_start = pd.Timestamp.now(tz=self.local_timezone) - pd.DateOffset(days=pd.Timestamp.now(tz=self.local_timezone).dayofweek)
75
- report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.now(tz=self.local_timezone) + pd.DateOffset(days=1))]
76
- elif timeframe == 'monthly':
77
- report = df[df['Task Time'].dt.month == pd.Timestamp.now(tz=self.local_timezone).month]
78
- elif timeframe == 'yearly':
79
- report = df[df['Task Time'].dt.year == pd.Timestamp.now(tz=self.local_timezone).year]
80
- else:
81
- report = pd.DataFrame() # Empty DataFrame for unsupported timeframes
82
 
83
- report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0
84
- return report
 
 
 
 
 
 
 
 
 
1
  class TaskManager:
 
 
 
2
  def __init__(self):
3
+ # Initialization code (e.g., setting up local time zones and loading tasks)
4
+
5
+ def generate_report(self, timeframe):
6
+ df = pd.DataFrame(st.session_state.tasks)
7
+ if df.empty:
8
+ return pd.DataFrame() # Return empty DataFrame if no tasks are present
9
+
10
+ # Ensure 'Task Time' is already timezone-aware and convert it to local time for the report
11
+ df['Task Time'] = pd.to_datetime(df['Task Time']).apply(lambda x: x.tz_convert(self.local_timezone))
12
+
13
+ today = pd.Timestamp.now(tz=self.local_timezone).date()
14
+
15
+ if timeframe == 'daily':
16
+ report = df[df['Task Time'].dt.date == today]
17
+ elif timeframe == 'weekly':
18
+ week_start = pd.Timestamp.now(tz=self.local_timezone) - pd.DateOffset(days=pd.Timestamp.now(tz=self.local_timezone).dayofweek)
19
+ report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.now(tz=self.local_timezone) + pd.DateOffset(days=1))]
20
+ elif timeframe == 'monthly':
21
+ report = df[df['Task Time'].dt.month == pd.Timestamp.now(tz=self.local_timezone).month]
22
+ elif timeframe == 'yearly':
23
+ report = df[df['Task Time'].dt.year == pd.Timestamp.now(tz=self.local_timezone).year]
24
  else:
25
+ report = pd.DataFrame() # Empty DataFrame for unsupported timeframes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0
28
+ return report