diff options
| author | 2018-02-16 19:21:19 +0000 | |
|---|---|---|
| committer | 2018-02-16 19:21:19 +0000 | |
| commit | 74878acd022deec13176c96c72b72aa23fe0b800 (patch) | |
| tree | c9f09c2e20e09c4b0b71e9528b5c706817de9ae1 /pysite/views/api/bot | |
| parent | Fix slight error in database insertion (diff) | |
| parent | API_KEY -> BOT_API_KEY (diff) | |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'pysite/views/api/bot')
| -rw-r--r-- | pysite/views/api/bot/tag.py | 8 | ||||
| -rw-r--r-- | pysite/views/api/bot/user.py | 48 | 
2 files changed, 25 insertions, 31 deletions
| diff --git a/pysite/views/api/bot/tag.py b/pysite/views/api/bot/tag.py index b2ce145b..363f98fe 100644 --- a/pysite/views/api/bot/tag.py +++ b/pysite/views/api/bot/tag.py @@ -4,7 +4,7 @@ from flask import jsonify, request  from pysite.base_route import APIView, DBViewMixin  from pysite.constants import ErrorCodes -from pysite.decorators import valid_api_key +from pysite.decorators import api_key  class TagView(APIView, DBViewMixin): @@ -13,7 +13,7 @@ class TagView(APIView, DBViewMixin):      table_name = "tag"      table_primary_key = "tag_name" -    @valid_api_key +    @api_key      def get(self):          """          Data must be provided as params, @@ -29,7 +29,7 @@ class TagView(APIView, DBViewMixin):          return jsonify(data) -    @valid_api_key +    @api_key      def post(self):          """          Data must be provided as JSON. @@ -51,6 +51,6 @@ class TagView(APIView, DBViewMixin):                  }              )          else: -            return self.error(ErrorCodes.missing_parameters) +            return self.error(ErrorCodes.incorrect_parameters)          return jsonify({"success": True}) diff --git a/pysite/views/api/bot/user.py b/pysite/views/api/bot/user.py index aad58e05..8c2d8149 100644 --- a/pysite/views/api/bot/user.py +++ b/pysite/views/api/bot/user.py @@ -1,10 +1,20 @@  # coding=utf-8 -from flask import jsonify, request +from flask import jsonify + +from schema import Schema  from pysite.base_route import APIView, DBViewMixin -from pysite.constants import ErrorCodes -from pysite.decorators import valid_api_key +from pysite.constants import ValidationTypes +from pysite.decorators import api_key, api_params + + +SCHEMA = Schema([ +    { +        "user_id": int, +        "role": int +    } +])  REQUIRED_KEYS = [      "user_id", @@ -18,28 +28,12 @@ class UserView(APIView, DBViewMixin):      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) +    @api_key +    @api_params(schema=SCHEMA, validation_type=ValidationTypes.json) +    def post(self, data): +        changes = self.db.insert( +            self.table_name, *data, +            conflict="update" +        ) -        return jsonify({"success": True}) +        return jsonify(changes) | 
