-
Notifications
You must be signed in to change notification settings - Fork 4
/
db_utils.py
62 lines (52 loc) · 1.85 KB
/
db_utils.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
from .models import NotifierConfig
from CTFd.models import (
db
)
class DBUtils:
DEFAULT_CONFIG = [
{"key": "discord_notifier", "value": "false"},
{"key": "discord_webhook_url", "value": ""},
{"key": "twitter_notifier", "value": "false"},
{"key": "twitter_consumer_key", "value": ""},
{"key": "twitter_consumer_secret", "value": ""},
{"key": "twitter_access_token", "value": ""},
{"key": "twitter_access_token_secret", "value": ""},
{"key": "twitter_hashtags", "value": ""},
{"key": "telegram_bot_token", "value": ""},
{"key": "telegram_chat_id", "value": ""},
{"key": "telegram_message_thread_id", "value": ""},
]
@staticmethod
def get(key):
return NotifierConfig.query.filter_by(key=key).first()
@staticmethod
def get_config():
configs = NotifierConfig.query.all()
result = {}
for c in configs:
result[str(c.key)] = str(c.value)
return result
@staticmethod
def save_config(config):
for c in config:
q = db.session.query(NotifierConfig)
q = q.filter(NotifierConfig.key == c[0])
record = q.one_or_none()
if record:
record.value = c[1]
db.session.commit()
else:
config = NotifierConfig(key=c[0], value=c[1])
db.session.add(config)
db.session.commit()
db.session.close()
@staticmethod
def load_default():
for cv in DBUtils.DEFAULT_CONFIG:
# Query for the config setting
k = DBUtils.get(cv["key"])
# If its not created, create it with its default value
if not k:
c = NotifierConfig(key=cv["key"], value=cv["value"])
db.session.add(c)
db.session.commit()