dhorvath commited on
Commit
62bd8af
·
verified ·
1 Parent(s): a160bf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -39
app.py CHANGED
@@ -38,53 +38,113 @@ DEFAULT_PROJECT_GID = "1209104858113361"
38
 
39
  def create_asana_task(name, due_date=None):
40
  """Create a task in Asana."""
41
- url = f"{ASANA_BASE_URL}/tasks"
42
- data = {
43
- "data": {
44
- "name": name,
45
- "projects": [DEFAULT_PROJECT_GID]
 
 
 
 
 
 
 
 
46
  }
47
- }
48
- if due_date:
49
- data["data"]["due_on"] = due_date # Asana uses 'due_on' in YYYY-MM-DD format
50
- resp = requests.post(url, json=data, headers=ASANA_HEADERS)
51
- if resp.status_code == 201:
52
- return resp.json()["data"] # returns the newly created task object
53
- else:
54
- return {"error": resp.text}
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  def list_asana_tasks(only_open=True):
57
  """List tasks in the default project, optionally filtering for only open tasks."""
58
- url = f"{ASANA_BASE_URL}/projects/{DEFAULT_PROJECT_GID}/tasks"
59
- params = {
60
- "opt_fields": "name,completed" # Include the "completed" field to verify task status
61
- }
62
- if only_open:
63
- params["completed_since"] = "now" # Fetch only incomplete or recently updated tasks
64
-
65
- resp = requests.get(url, headers=ASANA_HEADERS, params=params)
66
- if resp.status_code == 200:
67
- tasks = resp.json()["data"]
68
- if only_open:
69
- # Filter out completed tasks if only_open is True
70
- tasks = [task for task in tasks if not task.get("completed", False)]
71
- return tasks
72
- else:
73
- return {"error": resp.text}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  def complete_asana_task(task_gid):
76
  """Mark a task as complete."""
77
- url = f"{ASANA_BASE_URL}/tasks/{task_gid}"
78
- data = {
79
- "data": {
80
- "completed": True
 
 
 
 
 
 
 
 
81
  }
82
- }
83
- resp = requests.put(url, json=data, headers=ASANA_HEADERS)
84
- if resp.status_code == 200:
85
- return resp.json()["data"]
86
- else:
87
- return {"error": resp.text}
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  def call_llm(user_message, conversation_history=None):
90
  today_date = datetime.date.today().strftime("%Y-%m-%d")
 
38
 
39
  def create_asana_task(name, due_date=None):
40
  """Create a task in Asana."""
41
+ api_client = None # Initialize to None
42
+ try:
43
+ configuration = asana.Configuration()
44
+ configuration.access_token = access_token
45
+ api_client = asana.ApiClient(configuration)
46
+ tasks_api = asana.TasksApi(api_client)
47
+
48
+ url = f"{ASANA_BASE_URL}/tasks" # Construct the URL for creating tasks
49
+ data = {
50
+ "data": {
51
+ "name": name,
52
+ "projects": [DEFAULT_PROJECT_GID]
53
+ }
54
  }
55
+ if due_date:
56
+ data["data"]["due_on"] = due_date # Asana uses 'due_on' in YYYY-MM-DD format
57
+
58
+ # Create the task using the requests library
59
+ headers = {
60
+ 'Authorization': f'Bearer {access_token}',
61
+ 'Content-Type': 'application/json',
62
+ }
63
+ response = requests.post(url, headers=headers, json={"data": data["data"]})
64
+
65
+ if response.status_code == 201:
66
+ return response.json()["data"] # returns the newly created task object
67
+ else:
68
+ return {"error": response.text}
69
+
70
+ except Exception as e:
71
+ print(f"Exception when creating task: {e}\n")
72
+ return {"error": str(e)}
73
+ finally:
74
+ if api_client:
75
+ api_client.close()
76
 
77
  def list_asana_tasks(only_open=True):
78
  """List tasks in the default project, optionally filtering for only open tasks."""
79
+ api_client = None
80
+ try:
81
+ configuration = asana.Configuration()
82
+ configuration.access_token = access_token
83
+ api_client = asana.ApiClient(configuration)
84
+ tasks_api = asana.TasksApi(api_client)
85
+
86
+ url = f"{ASANA_BASE_URL}/projects/{DEFAULT_PROJECT_GID}/tasks"
87
+ params = {
88
+ "opt_fields": "name,completed",
89
+ "completed_since": "now" if only_open else None
90
+ }
91
+
92
+ headers = {
93
+ 'Authorization': f'Bearer {access_token}',
94
+ 'Accept': 'application/json',
95
+ }
96
+
97
+ response = requests.get(url, headers=headers, params=params)
98
+
99
+ if response.status_code == 200:
100
+ tasks = response.json()["data"]
101
+ if only_open:
102
+ tasks = [task for task in tasks if not task.get("completed", False)]
103
+ return tasks
104
+ else:
105
+ return {"error": response.text}
106
+
107
+ except Exception as e:
108
+ print(f"Exception when listing tasks: {e}\n")
109
+ return {"error": str(e)}
110
+ finally:
111
+ if api_client:
112
+ api_client.close()
113
 
114
  def complete_asana_task(task_gid):
115
  """Mark a task as complete."""
116
+ api_client = None
117
+ try:
118
+ configuration = asana.Configuration()
119
+ configuration.access_token = access_token
120
+ api_client = asana.ApiClient(configuration)
121
+ tasks_api = asana.TasksApi(api_client)
122
+
123
+ url = f"{ASANA_BASE_URL}/tasks/{task_gid}"
124
+ data = {
125
+ "data": {
126
+ "completed": True
127
+ }
128
  }
129
+
130
+ headers = {
131
+ 'Authorization': f'Bearer {access_token}',
132
+ 'Content-Type': 'application/json',
133
+ }
134
+
135
+ response = requests.put(url, headers=headers, json=data)
136
+
137
+ if response.status_code == 200:
138
+ return response.json()["data"]
139
+ else:
140
+ return {"error": response.text}
141
+
142
+ except Exception as e:
143
+ print(f"Exception when completing task: {e}\n")
144
+ return {"error": str(e)}
145
+ finally:
146
+ if api_client:
147
+ api_client.close()
148
 
149
  def call_llm(user_message, conversation_history=None):
150
  today_date = datetime.date.today().strftime("%Y-%m-%d")