-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
87 lines (67 loc) · 2.16 KB
/
db.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
import copy
import os
import datetime
import boto3
DYNAMO_DB = None
TABLE_NAME = 'happy-birthday'
CACHE = {}
class db:
@staticmethod
def init():
global DYNAMO_DB
if not DYNAMO_DB:
DYNAMO_DB = boto3.resource(
'dynamodb',
endpoint_url=os.environ.get('YDB_ENDPOINT'),
region_name='ru',
aws_access_key_id=os.environ.get('YDB_ACCESS_KEY_ID'),
aws_secret_access_key=os.environ.get('YDB_ACCESS_KEY_SECRET')
)
table_names = [table.name for table in DYNAMO_DB.tables.all()]
table = None
if TABLE_NAME not in table_names:
table = DYNAMO_DB.create_table(
TableName=TABLE_NAME,
KeySchema=[
{
'AttributeName': 'id',
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{'AttributeName': 'id', 'AttributeType': 'S'},
{'AttributeName': 'step', 'AttributeType': 'S'},
{'AttributeName': 'errors', 'AttributeType': 'S'},
{'AttributeName': 'updated', 'AttributeType': 'S'},
{'AttributeName': 'questions', 'AttributeType': 'S'},
{'AttributeName': 'final', 'AttributeType': 'S'}
]
)
else:
table = DYNAMO_DB.Table(TABLE_NAME)
return table
@staticmethod
def save_user(id, data):
id = str(id)
table = db.init()
data['id'] = id
data['updated'] = datetime.datetime.now().isoformat()
table.put_item(
Item=data
)
CACHE[id] = copy.deepcopy(data)
return data
@staticmethod
def get_user(id):
id = str(id)
if id in CACHE:
return copy.deepcopy(CACHE[id])
table = db.init()
data = None
try:
response = table.get_item(Key={'id': id})
data = response['Item']
CACHE[id] = copy.deepcopy(data)
except Exception as e:
data = None
return data