diff options
author | 2018-02-15 22:07:13 +0000 | |
---|---|---|
committer | 2018-02-15 23:07:13 +0100 | |
commit | 7a074f09687c97cce12117ba77441a77302c7f93 (patch) | |
tree | 6ccdf2f3582fd6d3eef9e2f253d83328b1ff4778 /pysite | |
parent | Make the websocket taste page use `wss://` (diff) |
API view for syncing users from the bot #yumr (#14)
* API view for syncing users from the bot
* Fix view class name
Diffstat (limited to 'pysite')
-rw-r--r-- | pysite/views/api/bot/user.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/pysite/views/api/bot/user.py b/pysite/views/api/bot/user.py new file mode 100644 index 00000000..aad58e05 --- /dev/null +++ b/pysite/views/api/bot/user.py @@ -0,0 +1,45 @@ +# coding=utf-8 + +from flask import jsonify, request + +from pysite.base_route import APIView, DBViewMixin +from pysite.constants import ErrorCodes +from pysite.decorators import valid_api_key + +REQUIRED_KEYS = [ + "user_id", + "role" +] + + +class UserView(APIView, DBViewMixin): + path = "/user" + name = "user" + table_name = "users" + table_primary_key = "user_id" + + @valid_api_key + def post(self): + data = request.get_json() + + if not isinstance(data, list): + data = [data] + + for user in data: + if not all(k in user for k in REQUIRED_KEYS): + print(user) + return self.error(ErrorCodes.missing_parameters) + + self.db.insert( + self.table_name, + { + "user_id": user["user_id"], + "role": user["role"], + }, + conflict="update", + durability="soft" + ) + + self.db.sync(self.table_name) + + return jsonify({"success": True}) |