aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar momothereal <[email protected]>2018-07-05 13:46:13 -0400
committerGravatar momothereal <[email protected]>2018-07-05 13:46:13 -0400
commitf3838900acae847b1dd93f748e8ffd714fdc579d (patch)
treee2d90ad80f7b557aa1c9c0d6f2425b6f457ea53e
parentMerge branch 'momo/jam-winner' into 'master' (diff)
Settings route
-rw-r--r--pysite/tables.py8
-rw-r--r--pysite/views/api/bot/settings.py56
2 files changed, 64 insertions, 0 deletions
diff --git a/pysite/tables.py b/pysite/tables.py
index 5ccebc77..617a20f0 100644
--- a/pysite/tables.py
+++ b/pysite/tables.py
@@ -250,4 +250,12 @@ TABLES = {
]),
locked=False
),
+
+ "bot_settings": Table(
+ primary_key="key",
+ keys=sorted([
+ "key", # str
+ "value" # any
+ ])
+ )
}
diff --git a/pysite/views/api/bot/settings.py b/pysite/views/api/bot/settings.py
new file mode 100644
index 00000000..a633a68a
--- /dev/null
+++ b/pysite/views/api/bot/settings.py
@@ -0,0 +1,56 @@
+from flask import jsonify
+from schema import Optional, Schema
+
+from pysite.base_route import APIView
+from pysite.constants import ValidationTypes
+from pysite.decorators import api_key, api_params
+from pysite.mixins import DBMixin
+
+# todo: type safety
+SETTINGS_KEYS_DEFAULTS = {
+ "defcon_enabled": False,
+ "defcon_days": 1
+}
+
+GET_SCHEMA = Schema({
+ Optional("keys"): str
+})
+
+
+def settings_schema():
+ schema_dict = {Optional(key): type(SETTINGS_KEYS_DEFAULTS[key]) for key in SETTINGS_KEYS_DEFAULTS.keys()}
+ return Schema(schema_dict)
+
+
+class ServerSettingsView(APIView, DBMixin):
+ path = "/bot/settings"
+ name = "bot.settings"
+
+ @api_key
+ @api_params(schema=GET_SCHEMA, validation_type=ValidationTypes.params)
+ def get(self, params=None):
+ keys_raw = None
+ if params:
+ keys_raw = params.get("keys")
+
+ keys = filter(lambda key: key in SETTINGS_KEYS_DEFAULTS,
+ keys_raw.split(",")) if keys_raw else SETTINGS_KEYS_DEFAULTS.keys()
+
+ result = {key: (self.db.get("bot_settings", key) or {}).get("value") or SETTINGS_KEYS_DEFAULTS[key] for key in
+ keys}
+ return jsonify(result)
+
+ @api_key
+ @api_params(schema=settings_schema(), validation_type=ValidationTypes.json)
+ def put(self, json_data):
+ # update in database
+
+ for key, value in json_data.items():
+ self.db.insert("bot_settings", {
+ "key": key,
+ "value": value
+ }, conflict="update")
+
+ return jsonify({
+ "success": True
+ })