diff options
| author | 2018-02-14 23:10:31 +0000 | |
|---|---|---|
| committer | 2018-02-14 23:10:31 +0000 | |
| commit | 70f0a9166b15645845370f4db8b1c9d1cfb75e6a (patch) | |
| tree | a9ff85c2ecdb1db529a935f070b2d54ecbee93ef /pysite/views/api/bot | |
| parent | [API] You need to return the value of `self.error()` (diff) | |
Database API Improvements #1qcra (#13)
* A large set of changes, including:
* A mixin for views that need the DB
* Many changes to the database class in order to make things more fluid
* Provide the route manager in view setup() methods
* Pushing up the progress so far
* snekchek
* Full (undocumented) database implementation
* snekchek
* Don't rely on exceptions for table deletion
* Add RethinkDB data to gitignore
* Documentation for DB class
* Make Flake8 ignore P102
What even is that? What does "docstring does contain unindexed parameters" mean?
* Document the base_routes module
* Cleanup RE latest reviews
* snekchek (bah)
Diffstat (limited to 'pysite/views/api/bot')
| -rw-r--r-- | pysite/views/api/bot/tag.py | 64 |
1 files changed, 27 insertions, 37 deletions
diff --git a/pysite/views/api/bot/tag.py b/pysite/views/api/bot/tag.py index e679b500..84fd8977 100644 --- a/pysite/views/api/bot/tag.py +++ b/pysite/views/api/bot/tag.py @@ -1,66 +1,56 @@ # coding=utf-8 -from flask import g, jsonify, request +from flask import jsonify, request -import rethinkdb - -from pysite.base_route import APIView +from pysite.base_route import APIView, DBViewMixin from pysite.constants import ErrorCodes -class TagView(APIView): - path = '/tag' - name = 'tag' - table = 'tag' - - def __init__(self): - # make sure the table exists - with g.db.get_connection() as conn: - try: - rethinkdb.db(g.db.database).table_create(self.table, {'primary_key': 'tag_name'}).run(conn) - except rethinkdb.RqlRuntimeError: - print(f'Table {self.table} exists') +class TagView(APIView, DBViewMixin): + path = "/tag" + name = "tag" + table_name = "tag" + table_primary_key = "tag_name" def get(self): """ - Indata must be provided as params, + Data must be provided as params, API key must be provided as header """ - rdb = rethinkdb.table(self.table) - api_key = request.headers.get('X-API-Key') - tag_name = request.args.get('tag_name') + api_key = request.headers.get("X-API-Key") + tag_name = request.args.get("tag_name") if self.validate_key(api_key): if tag_name: - data = rdb.get(tag_name).run(g.db.conn) - data = dict(data) if data else {} + data = self.db.get(self.table_name, tag_name) else: - data = rdb.pluck('tag_name').run(g.db.conn) - data = list(data) if data else [] + data = self.db.pluck(self.table_name, "tag_name") else: return self.error(ErrorCodes.invalid_api_key) - return jsonify(data) + return jsonify(data or {}) def post(self): - """ Indata must be provided as JSON. """ - rdb = rethinkdb.table(self.table) + """ Data must be provided as JSON. """ indata = request.get_json() - tag_name = indata.get('tag_name') - tag_content = indata.get('tag_content') - tag_category = indata.get('tag_category') - api_key = indata.get('api_key') + tag_name = indata.get("tag_name") + tag_content = indata.get("tag_content") + tag_category = indata.get("tag_category") + api_key = request.headers.get("X-API-Key") if self.validate_key(api_key): if tag_name and tag_content: - rdb.insert({ - 'tag_name': tag_name, - 'tag_content': tag_content, - 'tag_category': tag_category - }).run(g.db.conn) + self.db.insert( + self.table_name, + { + "tag_name": tag_name, + "tag_content": tag_content, + "tag_category": tag_category + } + ) else: return self.error(ErrorCodes.missing_parameters) else: return self.error(ErrorCodes.invalid_api_key) - return jsonify({'success': True}) + return jsonify({"success": True}) |