-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbr_quickbase.py
More file actions
79 lines (71 loc) · 2.48 KB
/
Copy pathcbr_quickbase.py
File metadata and controls
79 lines (71 loc) · 2.48 KB
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
import pandas as pd
import requests
from dateutil import parser
class CBR_QB:
def __init__(self, realm, usertoken, logging=False):
self.logging=logging
self.headers = {
'QB-Realm-Hostname': realm,
'Authorization': f'QB-USER-TOKEN {usertoken}'
}
def _process_dates(self, cell):
if cell and type(cell) == str:
if cell[:4] == '1900':
return ''
else:
return parser.isoparse(cell).astimezone()
else:
if not type(cell) == str:
print(type(cell))
return cell
def _qb_to_df(self,r):
df = pd.json_normalize(r.json()['data'])
fields = pd.DataFrame(r.json()['fields'])
fields_mapping = dict(zip(fields['id'], fields['label']))
df.rename(columns=lambda x: fields_mapping[int(x.replace('.value', ''))], inplace=True)
df.set_index('Related Site', inplace=True)
df = df.applymap(self._process_dates)
df.reset_index(inplace=True)
return df
def query_records(self, tableid, fieldslist, query):
body = {
"from": tableid,
"select": fieldslist,
"where": query
}
r = requests.post(
'https://api.quickbase.com/v1/records/query',
headers = self.headers,
json = body
)
return self._qb_to_df(r)
def _process_dataframe_for_update(self, df, tableid):
data = []
for _, row in df.iterrows():
mapping = {}
for column in list(df.columns):
mapping[str(column)] = {
"value": row[column]
}
data.append(mapping)
return {
"to": tableid,
"data": data
}
#Dataframe must have specific form
#Columns are field IDs, rows contain the data, no index
def update_records_with_dataframe(self, df, tableid):
body = self._process_dataframe_for_update(df, tableid)
r = requests.post(
'https://api.quickbase.com/v1/records',
headers = self.headers,
json = body
)
print(r.json()) if self.logging else None
return
if __name__ == '__main__':
import numpy as np
qb_client = CBR_QB('thecbrgroup', 'b5qarz_i55j_0_cqcsib22aqqtcfrxvsizrdtam')
df = pd.DataFrame([['TEST', 'ABC123']], columns=[6, 27])
print(df.head())
qb_client.update_records_with_dataframe(df, 'bq8fc85sy')