-
Notifications
You must be signed in to change notification settings - Fork 121
/
book_review.py
54 lines (39 loc) · 1.32 KB
/
book_review.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
import os
from pyairtable import Api
API_TOKEN = os.environ.get('AIRTABLE_TOKEN')
BASE_ID = 'appi1uzlLKn1TEKSw'
TABLE_ID = 'tblvMMAVHo901m2Ra'
api = Api(API_TOKEN)
table = api.table(BASE_ID, TABLE_ID)
def get_all_records(count=None, sort=None):
sort_param = []
if sort and sort.upper()=='DESC':
sort_param = ['-Rating']
elif sort and sort.upper()=='ASC':
sort_param = ['Rating']
return table.all(max_records=count, sort=sort_param)
def get_record_id(name):
return table.first(formula=f"Book='{name}'")['id']
def update_record(record_id, data):
table.update(record_id, data)
return True
def add_record(data):
# require data contains a "Book" key and a "Rating" key (data is a dict)
if 'Book' not in data or 'Rating' not in data:
return False
table.create(data)
return True
if __name__ == '__main__':
## Show getting certain records
print("Show getting certain records")
print(table.all(formula="Rating < 5", sort=['-Rating']))
## Show getting a single record
print("Show getting a single record")
# Replace a record
print("Replace a record")
name = "Test Message"
record_id = table.first(formula=f"Book='{name}'")['id']
table.update(record_id, {"Rating": 5})
## Show all records
print("All records!")
print(table.all())