-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
64 lines (51 loc) · 1.81 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
# Database Models, Connection, and Setup
import datetime
from loguru import logger
from peewee import Check
from peewee import SqliteDatabase, Model
from peewee import BooleanField, IntegerField
from peewee import TextField, DateTimeField, ForeignKeyField
# db = SqliteDatabase("surveys.db")
db = SqliteDatabase("surveys.db", pragmas={
"journal_mode": "wal",
"cache_size": -1 * 64000, # 64MB
"foreign_keys": 1,
"ignore_check_constraints": 0,
"synchronous": 0})
class BaseModel(Model):
class Meta:
database = db
class Survey(BaseModel):
message_id = TextField()
message_url = TextField()
posted = DateTimeField(default=datetime.datetime.utcnow)
expires = DateTimeField(default=-1)
author = TextField()
question = TextField()
active = BooleanField(default=True)
realm = TextField(null=True)
locked_by = TextField(null=True)
votes_hidden = BooleanField(default=False)
vote_limit = IntegerField(
constraints=[Check('vote_limit > 0')]
)
def is_expired(self):
logger.info(f"self.expires: {self.expires}")
return self.expires <= datetime.datetime.utcnow()
class Option(BaseModel):
survey = ForeignKeyField(Survey, backref="options")
idx = IntegerField(null=False)
text = TextField(null=False)
color = IntegerField(default=1)
text_emoji_name = TextField(null=True)
button_emoji_name = TextField(null=True)
button_emoji_id = TextField(null=True)
button_text = TextField(null=True)
class Vote(BaseModel):
# TODO: Figure out how to deal with the option vs. option_idx situation
voter = TextField(null=False)
option_idx = IntegerField()
option = ForeignKeyField(Option, backref="votes")
survey = ForeignKeyField(Survey, backref="votes")
db.connect()
db.create_tables([Survey, Option, Vote])