diff options
| author | 2018-03-06 20:05:44 +0100 | |
|---|---|---|
| committer | 2018-03-06 19:05:44 +0000 | |
| commit | 5d685b27c5454e29809fe6039e0cf8945cbbb52f (patch) | |
| tree | 2782bd8144f744bfc46924f64fb57f465cf31d67 /pysite/views/api/bot | |
| parent | Fix user API test (diff) | |
API for tags (#34)
* Help page and misc improvements
Committing so I can go home >:|
* [WIP] - API improvements for the tag features. Not completed.
* renaming tag.py to tags.py and refactoring the nomenclature of docs to tags
* fixed error message in tags, cleaning up app_test.py
* tests for the tags feature
* ignoring jsonify returns cause coverall can't handle them
* Catch-all error view for the API blueprint
* cleaning up APIErrorView a little
* bringing coverage for tags.py to 100%
* how did this get in here?
* how did this get in here? ROUND 2
* Removing the 503 database error handling. It's not in use and we should probably rethink that whole custom error handling system anyway.
* Converting the tags file to use the @api_params decorator instead of validating manually. Tested with bot staging.
Diffstat (limited to 'pysite/views/api/bot')
| -rw-r--r-- | pysite/views/api/bot/tag.py | 57 | ||||
| -rw-r--r-- | pysite/views/api/bot/tags.py | 116 | 
2 files changed, 116 insertions, 57 deletions
diff --git a/pysite/views/api/bot/tag.py b/pysite/views/api/bot/tag.py deleted file mode 100644 index 8818074e..00000000 --- a/pysite/views/api/bot/tag.py +++ /dev/null @@ -1,57 +0,0 @@ -# coding=utf-8 - -from flask import jsonify, request - -from pysite.base_route import APIView -from pysite.constants import ErrorCodes -from pysite.decorators import api_key -from pysite.mixins import DBMixin - - -class TagView(APIView, DBMixin): -    path = "/tag" -    name = "tag" -    table_name = "tag" -    table_primary_key = "tag_name" - -    @api_key -    def get(self): -        """ -        Data must be provided as params, -        API key must be provided as header -        """ - -        tag_name = request.args.get("tag_name") - -        if tag_name: -            data = self.db.get(self.table_name, tag_name) or {}  # pragma: no cover -        else: -            data = self.db.pluck(self.table_name, "tag_name") or [] - -        return jsonify(data)  # pragma: no cover - -    @api_key -    def post(self): -        """ -        Data must be provided as JSON. -        """ - -        data = request.get_json() - -        tag_name = data.get("tag_name") -        tag_content = data.get("tag_content") -        tag_category = data.get("tag_category") - -        if tag_name and tag_content: -            self.db.insert( -                self.table_name, -                { -                    "tag_name": tag_name, -                    "tag_content": tag_content, -                    "tag_category": tag_category -                } -            ) -        else: -            return self.error(ErrorCodes.incorrect_parameters) - -        return jsonify({"success": True})  # pragma: no cover diff --git a/pysite/views/api/bot/tags.py b/pysite/views/api/bot/tags.py new file mode 100644 index 00000000..9db926f4 --- /dev/null +++ b/pysite/views/api/bot/tags.py @@ -0,0 +1,116 @@ +# coding=utf-8 + +from flask import jsonify +from schema import Schema, Optional + +from pysite.base_route import APIView +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 +    } +]) + + +class TagsView(APIView, DBMixin): +    path = "/tags" +    name = "tags" +    table_name = "tags" +    table_primary_key = "tag_name" + +    @api_key +    @api_params(schema=GET_SCHEMA, validation_type=ValidationTypes.params) +    def get(self, params=None): +        """ +        Fetches tags from the database. + +        - If tag_name is provided, it fetches +        that specific tag. + +        - If tag_category is provided, it fetches +        all tags in that category. + +        - If nothing is provided, it will +        fetch a list of all tag_names. + +        Data must be provided as params. +        API key must be provided as header. +        """ + +        tag_name = None + +        if params: +            tag_name = params[0].get("tag_name") + +        if tag_name: +            data = self.db.get(self.table_name, tag_name) or {} +        else: +            data = self.db.pluck(self.table_name, "tag_name") or [] + +        return jsonify(data) + +    @api_key +    @api_params(schema=POST_SCHEMA, validation_type=ValidationTypes.json) +    def post(self, json_data): +        """ +        If the tag_name doesn't exist, this +        saves a new tag in the database. + +        If the tag_name already exists, +        this will edit the existing tag. + +        Data must be provided as JSON. +        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") + +        self.db.insert( +            self.table_name, +            { +                "tag_name": tag_name, +                "tag_content": tag_content +            }, +            conflict="update"  # If it exists, update it. +        ) + +        return jsonify({"success": True}) + +    @api_key +    @api_params(schema=DELETE_SCHEMA, validation_type=ValidationTypes.json) +    def delete(self, data): +        """ +        Deletes a tag from the database. + +        Data must be provided as JSON. +        API key must be provided as header. +        """ + +        json = data[0] +        tag_name = json.get("tag_name") + +        self.db.delete( +            self.table_name, +            tag_name +        ) + +        return jsonify({"success": True})  |