forked from python-discord/site
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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.
- Loading branch information
1 parent
6be52b1
commit 5d685b2
Showing
9 changed files
with
254 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
[run] | ||
omit = /usr/*, gunicorn_config.py, deploy.py, app_test.py, app.py, pysite/websockets.py, pysite/views/*__init__.py, pysite/route_manager.py | ||
|
||
[report] | ||
exclude_lines = return jsonify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |
Oops, something went wrong.