-
Notifications
You must be signed in to change notification settings - Fork 0
/
datalayer.py
47 lines (39 loc) · 1.26 KB
/
datalayer.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
from datetime import datetime
from pynamodb import models, attributes
# noinspection PyAbstractClass
class Topic(models.Model):
topic = attributes.UnicodeAttribute(hash_key=True)
last_vote = attributes.UTCDateTimeAttribute(default=datetime.now)
votes = attributes.NumberAttribute(default=0)
class Meta:
table_name = 'Topic'
region = 'ap-southeast-2'
# def __init__(self, *args, **kwargs):
# if not Topic.exists():
# Topic.create_table(read_capacity_units=1, write_capacity_units=1, wait=True)
# super().__init__(*args, **kwargs)
def add_vote(self):
self.update({
'votes': {
'action': 'add',
'value': 1,
},
'last_vote': {
'action': 'put',
'value': datetime.now()
}
})
def reset_votes(self):
self.update({
'votes': {
'action': 'put',
'value': 1,
},
'last_vote': {
'action': 'put',
'value': datetime.now()
}
})
def __iter__(self):
for name, attr in self._get_attributes().items():
yield name, attr.serialize(getattr(self, name))