-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
95 lines (78 loc) · 3.47 KB
/
app.py
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
import requests
import json
from airtable import AirTable as at
def main():
# get list of projects records from your projects table
# be sure you set your base id and table name
project_records = get_projects()
# grab your map of project phase and associated assignees from your base
# be sure you set your base id and table name
project_phase_owner_map = get_project_phase_owner_map()
# check each records current phase value and assignee value
# check who the assignee should be
# if the 'should be' assignee is different, update the record with the new assignee
for record in project_records:
# store record id and grab project data
record_id = record['id']
project = record['fields']
# check if a value is set for the project phase
# if value is set, grab the value and check the phase/assignee map to see who owns it
if at.PROJECT_PHASE_FIELD in project:
current_phase = project[at.PROJECT_PHASE_FIELD]
next_assignee = project_phase_owner_map[current_phase]['id']
# check if an assignee is set for the given project record
# if value is set, grab the current assignee
# if value is not set, then set the correct assignee
if at.ASSIGNEE_FIELD in project:
current_assignee = project[at.ASSIGNEE_FIELD]['id']
# check if the assignee currently listed for the given phase is the assignee that should be there
# if the current assignee is not the assignee that should be, update the assignee to the correct one
# if value is not set, do nothing
if current_assignee != next_assignee:
update_project_owner(project_phase_owner_map, current_phase, record_id)
else:
print('No need to auto-assign assignee--correct assignee is already in place.')
else:
update_project_owner(project_phase_owner_map, current_phase, record_id)
else:
print('No need to auto-assign asignee--single-select value is not present.')
# get a list of projects from airtable
def get_projects():
req = requests.get(
url = f'https://api.airtable.com/v0/{at.BASE}/{at.PROJECT_TABLE_NAME}/',
headers={
'Authorization': f'Bearer {at.API_KEY}'
}
)
return req.json()['records']
# update the current assignee for the record
def update_project_owner(project_owners_map, current_single_select_value, record_id):
new_assignee = {
"fields": {
at.ASSIGNEE_FIELD: project_owners_map[current_single_select_value]
}
}
req = requests.request(
'PATCH',
url = f'https://api.airtable.com/v0/{at.BASE}/{at.PROJECT_TABLE_NAME}/{record_id}',
headers = {
'Authorization': f'Bearer {at.API_KEY}',
'Content-Type': 'application/json'
},
data = json.dumps(new_assignee)
)
# get your map of phases and their owners from your base
def get_project_phase_owner_map():
phase_owner_map = {}
req = requests.get(
url = f'https://api.airtable.com/v0/{at.BASE}/{at.PROJECT_PHASE_OWNERS_TABLE}/',
headers={
'Authorization': f'Bearer {at.API_KEY}'
}
)
for record in req.json()['records']:
fields = record['fields']
phase_owner_map.update({fields['Project Phase']:fields['Assignee']})
return phase_owner_map
if __name__ == '__main__':
main()