diff options
Diffstat (limited to 'dbstore/peewee_store.py')
-rw-r--r-- | dbstore/peewee_store.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/dbstore/peewee_store.py b/dbstore/peewee_store.py new file mode 100644 index 0000000..37b8c01 --- /dev/null +++ b/dbstore/peewee_store.py | |||
@@ -0,0 +1,59 @@ | |||
1 | from peewee import * | ||
2 | |||
3 | db = SqliteDatabase("checkinbot.db") | ||
4 | db.connect(reuse_if_open=True) | ||
5 | |||
6 | |||
7 | class BaseModel(Model): | ||
8 | class Meta: | ||
9 | database = db | ||
10 | |||
11 | |||
12 | class User(BaseModel): | ||
13 | telegram_user_id = CharField(unique=True, primary_key=True) | ||
14 | access_key = CharField(max_length=64) | ||
15 | home_instance = CharField(max_length=128) | ||
16 | state = CharField(max_length=128) | ||
17 | client_id = CharField(max_length=128) | ||
18 | client_secret = CharField(max_length=128) | ||
19 | |||
20 | |||
21 | class Location(BaseModel): | ||
22 | fsq_id = CharField(unique=True, primary_key=True) | ||
23 | name = CharField(max_length=128) | ||
24 | locality = CharField(max_length=128) | ||
25 | region = CharField(max_length=128) | ||
26 | latitude = CharField(max_length=128) | ||
27 | longitude = CharField(max_length=128) | ||
28 | |||
29 | |||
30 | with db.connection_context(): | ||
31 | db.create_tables([User, Location]) | ||
32 | |||
33 | |||
34 | def get_poi_by_fsq_id(fsq_id) -> dict: | ||
35 | with db.connection_context(): | ||
36 | try: | ||
37 | poi = Location.get(Location.fsq_id == fsq_id) | ||
38 | return { | ||
39 | "name": poi.name, | ||
40 | "locality": poi.locality, | ||
41 | "region": poi.region, | ||
42 | "latitude": poi.latitude, | ||
43 | "longitude": poi.longitude, | ||
44 | } | ||
45 | except DoesNotExist: | ||
46 | return {} | ||
47 | |||
48 | |||
49 | def create_poi(poi: dict): | ||
50 | with db.connection_context(): | ||
51 | poi = Location.create( | ||
52 | fsq_id=poi["fsq_id"], | ||
53 | name=poi["name"], | ||
54 | locality=poi["locality"], | ||
55 | region=poi["region"], | ||
56 | latitude=poi["latitude"], | ||
57 | longitude=poi["longitude"], | ||
58 | ) | ||
59 | poi.save() | ||