diff options
| author | 2018-06-14 22:18:50 +0000 | |
|---|---|---|
| committer | 2018-06-14 22:18:50 +0000 | |
| commit | c05751f2ef5c4a3320cb5dda39fe513ad0118d5c (patch) | |
| tree | ba8b4a0c0ffe549e58a39e383c2e9d7f14efa600 /pysite/views | |
| parent | Remove debug print from production (diff) | |
| parent | Allow schemas to not use a list when applicable (diff) | |
Merge branch 'allow-non-list-schemas' into 'master'
Allow schemas to not use a list when applicable
See merge request python-discord/projects/site!2
Diffstat (limited to 'pysite/views')
| -rw-r--r-- | pysite/views/api/bot/hiphopify.py | 42 | ||||
| -rw-r--r-- | pysite/views/api/bot/tags.py | 37 | ||||
| -rw-r--r-- | pysite/views/tests/index.py | 15 | 
3 files changed, 41 insertions, 53 deletions
| diff --git a/pysite/views/api/bot/hiphopify.py b/pysite/views/api/bot/hiphopify.py index 50a811c6..3a47b64e 100644 --- a/pysite/views/api/bot/hiphopify.py +++ b/pysite/views/api/bot/hiphopify.py @@ -12,25 +12,19 @@ from pysite.utils.time import is_expired, parse_duration  log = logging.getLogger(__name__) -GET_SCHEMA = Schema([ -    { -        "user_id": str -    } -]) - -POST_SCHEMA = Schema([ -    { -        "user_id": str, -        "duration": str, -        Optional("forced_nick"): str -    } -]) - -DELETE_SCHEMA = Schema([ -    { -        "user_id": str -    } -]) +GET_SCHEMA = Schema({ +    "user_id": str +}) + +POST_SCHEMA = Schema({ +    "user_id": str, +    "duration": str, +    Optional("forced_nick"): str +}) + +DELETE_SCHEMA = Schema({ +    "user_id": str +})  class HiphopifyView(APIView, DBMixin): @@ -55,7 +49,7 @@ class HiphopifyView(APIView, DBMixin):          API key must be provided as header.          """ -        user_id = params[0].get("user_id") +        user_id = params.get("user_id")          log.debug(f"Checking if user ({user_id}) is permitted to change their nickname.")          data = self.db.get(self.prison_table, user_id) or {} @@ -83,9 +77,9 @@ class HiphopifyView(APIView, DBMixin):          API key must be provided as header.          """ -        user_id = json_data[0].get("user_id") -        duration = json_data[0].get("duration") -        forced_nick = json_data[0].get("forced_nick") +        user_id = json_data.get("user_id") +        duration = json_data.get("duration") +        forced_nick = json_data.get("forced_nick")          log.debug(f"Attempting to imprison user ({user_id}).") @@ -146,7 +140,7 @@ class HiphopifyView(APIView, DBMixin):          API key must be provided as header.          """ -        user_id = json_data[0].get("user_id") +        user_id = json_data.get("user_id")          log.debug(f"Attempting to release user ({user_id}) from hiphop-prison.")          prisoner_data = self.db.get(self.prison_table, user_id) diff --git a/pysite/views/api/bot/tags.py b/pysite/views/api/bot/tags.py index 7fdaee3c..4394c224 100644 --- a/pysite/views/api/bot/tags.py +++ b/pysite/views/api/bot/tags.py @@ -6,24 +6,18 @@ from pysite.constants import ValidationTypes  from pysite.decorators import api_key, api_params  from pysite.mixins import DBMixin -GET_SCHEMA = Schema([ -    { -        Optional("tag_name"): str -    } -]) - -POST_SCHEMA = Schema([ -    { -        "tag_name": str, -        "tag_content": str -    } -]) - -DELETE_SCHEMA = Schema([ -    { -        "tag_name": str -    } -]) +GET_SCHEMA = Schema({ +    Optional("tag_name"): str +}) + +POST_SCHEMA = Schema({ +    "tag_name": str, +    "tag_content": str +}) + +DELETE_SCHEMA = Schema({ +    "tag_name": str +})  class TagsView(APIView, DBMixin): @@ -53,7 +47,7 @@ class TagsView(APIView, DBMixin):          tag_name = None          if params: -            tag_name = params[0].get("tag_name") +            tag_name = params.get("tag_name")          if tag_name:              data = self.db.get(self.table_name, tag_name) or {} @@ -76,8 +70,6 @@ class TagsView(APIView, DBMixin):          API key must be provided as header.          """ -        json_data = json_data[0] -          tag_name = json_data.get("tag_name")          tag_content = json_data.get("tag_content") @@ -102,8 +94,7 @@ class TagsView(APIView, DBMixin):          API key must be provided as header.          """ -        json = data[0] -        tag_name = json.get("tag_name") +        tag_name = data.get("tag_name")          tag_exists = self.db.get(self.table_name, tag_name)          if tag_exists: diff --git a/pysite/views/tests/index.py b/pysite/views/tests/index.py index b96590c0..f99e3f3c 100644 --- a/pysite/views/tests/index.py +++ b/pysite/views/tests/index.py @@ -1,20 +1,23 @@  from flask import jsonify  from schema import Schema -from pysite.base_route import RouteView +from pysite.base_route import APIView  from pysite.constants import ValidationTypes  from pysite.decorators import api_params -SCHEMA = Schema([{"test": str}]) +LIST_SCHEMA = Schema([{"test": str}]) +DICT_SCHEMA = Schema({"segfault": str}) -REQUIRED_KEYS = ["test"] - -class TestParamsView(RouteView): +class TestParamsView(APIView):      path = "/testparams"      name = "testparams" -    @api_params(schema=SCHEMA, validation_type=ValidationTypes.params) +    @api_params(schema=DICT_SCHEMA, validation_type=ValidationTypes.params) +    def get(self, data): +        return jsonify(data) + +    @api_params(schema=LIST_SCHEMA, validation_type=ValidationTypes.params)      def post(self, data):          jsonified = jsonify(data)          return jsonified | 
