Skip to content

Commit 6258a48

Browse files
committed
add FileUpload object
1 parent b487266 commit 6258a48

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/quickbase_json/helpers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import base64
2+
3+
14
class IncorrectParameters(Exception):
25
def __init__(self, value, expected):
36
self.value = value
@@ -93,3 +96,21 @@ def build(self):
9396
sorters.append({'fieldId': pair[0], 'grouping': pair[1]})
9497

9598
return sorters
99+
100+
101+
class FileUpload(dict):
102+
"""
103+
Represents a file object for easy upload to quickbase.
104+
When uploading, set FID to FileUpload directly, bypass {'value': etc}
105+
"""
106+
def __init__(self, path: str):
107+
"""
108+
:param path: path to file
109+
"""
110+
super().__init__()
111+
self.path = path
112+
113+
with open(path, 'rb') as f:
114+
# get file as a b64 string for upload to quickbase
115+
file = base64.b64encode(f.read()).decode()
116+
self.update({'value': {'fileName': f'{f.name.split("/")[-1]}', 'data': file}})

src/quickbase_json/qb_response.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22

33

4-
class bcolors:
4+
class Bcolors:
55
HEADER = '\033[95m'
66
OKBLUE = '\033[94m'
77
OKCYAN = '\033[96m'
@@ -28,13 +28,13 @@ def info(self):
2828
"""
2929
if self.response_type == 'records':
3030

31-
print(f'{bcolors.OKBLUE}Sample Data:\n')
31+
print(f'{Bcolors.OKBLUE}Sample Data:\n')
3232
try:
3333
print('\t', self.get('data')[0], '\n')
3434
except KeyError as e:
3535
print('\t', self.get('data'), '\n')
36-
print(bcolors.ENDC)
37-
print(f'{bcolors.OKGREEN}Fields:\n')
36+
print(Bcolors.ENDC)
37+
print(f'{Bcolors.OKGREEN}Fields:\n')
3838

3939
fields_info = []
4040
for field in self.get('fields'):
@@ -47,12 +47,12 @@ def info(self):
4747
# print field info
4848
for fi in fields_info:
4949
print('\t', '{:<16s} {:<32s} {:<16s}'.format(fi[0], fi[1], fi[2]))
50-
print(bcolors.ENDC)
50+
print(Bcolors.ENDC)
5151

52-
print(f'\n{bcolors.OKCYAN}Metadata:\n')
52+
print(f'\n{Bcolors.OKCYAN}Metadata:\n')
5353
for k, v in self.get('metadata').items():
5454
print('\t{:<16s} {:<16s}'.format(k, str(v)))
55-
print(bcolors.ENDC)
55+
print(Bcolors.ENDC)
5656

5757
def fields(self, prop):
5858
"""
@@ -63,6 +63,10 @@ def fields(self, prop):
6363
return [i.get(prop) for i in self.get('fields')]
6464

6565
def data(self):
66+
"""
67+
Gets data, shorthand for .get('data')
68+
:return: data
69+
"""
6670
return self.get('data')
6771

6872
def prd(self, data):
@@ -204,7 +208,6 @@ def transform(self, transformation, **kwargs):
204208
dt = datetime.datetime.strptime(str_dt, kwargs.get('datestring'))
205209
row.update({str(field.get('id')): {'value': dt}})
206210

207-
208211
if transformation == 'intround':
209212
"""Rounds numbers and transforms them to ints"""
210213

0 commit comments

Comments
 (0)