-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-tasks-tag
executable file
·155 lines (117 loc) · 3.91 KB
/
my-tasks-tag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
import json
from datetime import time, datetime
import sys
import asana
# memberships api is changing
# https://forum.asana.com/t/paused-upcoming-changes-that-impact-getting-and-setting-memberships-and-access-levels/232106
#
# https://github.com/Asana/python-asana#asana-change-warnings
AUTH_FILE = 'auth.json'
MAX_NAME_WIDTH = 60
MAX_PROJ_WIDTH = 24
try:
SEARCH_TAG = sys.argv[1]
except IndexError:
SEARCH_TAG = 'active'
with open(AUTH_FILE) as f:
j = json.load(f)
asana_key = j['key']
workspace_gid = j['workspace']
client = asana.Client.access_token(asana_key)
client.headers = {'asana-enable': 'new_goal_memberships'}
# me = client.users.me()
# print(me)
# opts = {'tags.any':'active'}
# result = client.tasks.search_tasks_for_workspace(workspace_gid, opts, opt_pretty=True)
# opts = {'workspace':workspace_gid,}
# result = client.tasks.get_tasks(opts, opt_pretty=True)
task_list = client.user_task_lists.get_user_task_list_for_user(
'me', {'workspace':workspace_gid})
res = client.user_task_lists.get_user_task_list(task_list['gid'])
opts = {'completed_since':'now'}
fields = ['this.name',
'this.due_on',
'this.liked',
'this.memberships.project.name',
'this.memberships.section.name',
'this.tags.name']
res = client.tasks.find_by_user_task_list(res['gid'], opts, opt_fields=fields)
def has_tag(r, tag):
for t in r['tags']:
if tag == t['name']:
return True
return False
# {'gid': '1200716274440367',
# 'memberships': [
# {'project': {'gid': '1200601501511683', 'name': 'Electronics 340 2021'},
# 'section': {'gid': '1200716274440346', 'name': 'Tasks'}}
# ],
# 'name': 'Setup syllabus',
# 'tags': [{'gid': '682001759886391', 'name': 'active'}]}
tagged_tasks = []
for r in res:
if has_tag(r, SEARCH_TAG):
tagged_tasks.append(r)
today = datetime.now()
# want day only so +1 day means tomorrow at any current time
today = datetime.combine(today.date(), time(0))
tasks = []
for task in tagged_tasks:
for m in task['memberships']:
t = {}
t['name'] = task['name']
t['liked'] = task['liked']
due_str = task['due_on']
t['due_on'] = due_str
if due_str:
due_date = datetime.strptime(due_str, '%Y-%m-%d')
days_due = (due_date - today).days
t['days_due'] = days_due
else:
# un-dated tagged are considered due today
t['days_due'] = 0
t['p_name'] = m['project']['name']
s_name = m['section']['name']
if s_name == 'Untitled section':
s_name = 'none'
t['s_name'] = m['section']['name']
# 'tags': [
# {'gid': '67092546920020', 'name': 'waiting'},
# {'gid': '682001759886391', 'name': 'active'}
# ]
tags = [tag['name'] for tag in task['tags'] if tag['name'] != SEARCH_TAG]
t['tags'] = tags
tasks.append(t)
def multisort(xs, specs):
from operator import getitem
for key, reverse in reversed(specs):
xs.sort(key=lambda x: x[key], reverse=reverse)
return xs
for t in multisort(tasks,
(('days_due', False),
('p_name', False),
('name', False))):
p_name = t['p_name']
s_name = t['s_name']
t_name = t['name']
if len(p_name) > MAX_PROJ_WIDTH:
p_name = p_name[:MAX_PROJ_WIDTH-1] + '.'
if len(t_name) > MAX_NAME_WIDTH:
t_name = t_name[:MAX_NAME_WIDTH-1] + '.'
days_due = t['days_due']
if days_due == 0:
due = '==>'
else:
due = f'{days_due:+3d}'
if t['liked']:
like = '**'
else:
like = ' '
if t['tags']:
tags = '[' + ','.join(t['tags']) + '] '
else:
tags = ''
# print(f'{p_name:16.16s} : {s_name:10.10s} : {t_name} {due}')
pw = MAX_PROJ_WIDTH
print(f'{p_name:{pw}.{pw}} {due}{like}{tags}{t_name}')