mirror of
https://github.com/Xevion/power-math.git
synced 2025-12-12 12:12:27 -06:00
change to Flask-RESTful, implement basic Question/Category/Categories classes, functionality outlined
This commit is contained in:
@@ -1,4 +1,14 @@
|
|||||||
from flask import current_app, jsonify, request
|
"""
|
||||||
|
api.py
|
||||||
|
|
||||||
|
Organizes all API routes for acquiring, checking, removing questions, as well as other important and miscellaneous
|
||||||
|
API interactions, static or dynamic.
|
||||||
|
"""
|
||||||
|
import copy
|
||||||
|
import random
|
||||||
|
|
||||||
|
from flask import jsonify
|
||||||
|
from flask_restful import Resource, reqparse, abort
|
||||||
|
|
||||||
from server import questions
|
from server import questions
|
||||||
from server.helpers import generate_id
|
from server.helpers import generate_id
|
||||||
@@ -7,25 +17,78 @@ from server.helpers import generate_id
|
|||||||
# Keys represent random question IDs. Values are Question objects.
|
# Keys represent random question IDs. Values are Question objects.
|
||||||
active_questions = {}
|
active_questions = {}
|
||||||
|
|
||||||
|
# Question categories, key name, value function for acquiring a random question generator.
|
||||||
categories = {
|
categories = {
|
||||||
'arithmetic': questions.get_arithmetic
|
'arithmetic': questions.get_arithmetic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parser = reqparse.RequestParser()
|
||||||
|
parser.add_argument(
|
||||||
|
'category',
|
||||||
|
required=False, type=str, choices=tuple(categories.keys()), help='Invalid Category: {error_msg}'
|
||||||
|
)
|
||||||
|
|
||||||
@current_app.route('/api/<category>/new')
|
|
||||||
def new_question(category: str):
|
class Question(Resource):
|
||||||
|
"""
|
||||||
|
Questions are small objects represented generated questions for users with a prompt and a answer.
|
||||||
|
They are identified by a String ID.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get(self, question_id):
|
||||||
|
"""Retrieve information about a given question, including the answer for it."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def put(self, question_id = None):
|
||||||
|
"""
|
||||||
|
Request a new question.
|
||||||
|
|
||||||
|
A category can be specified in the query arguments in order to filter the question type.
|
||||||
|
Furthermore, a question type can be also specified .
|
||||||
|
The Question object is returned, although the answer is omitted.
|
||||||
|
"""
|
||||||
|
args = parser.parse_args()
|
||||||
q_id = None
|
q_id = None
|
||||||
while q_id in active_questions.keys() or q_id is None:
|
while q_id in active_questions.keys() or q_id is None:
|
||||||
q_id = generate_id(5)
|
q_id = generate_id(5)
|
||||||
|
|
||||||
|
# Get category arg or choose one if not specified.
|
||||||
|
if args.get('category') is not None:
|
||||||
|
category = args.get('category')
|
||||||
|
if category not in categories.keys():
|
||||||
|
abort(404, message=f'Category {category} is not valid.')
|
||||||
|
else:
|
||||||
|
category = random.choice(list(categories.keys()))
|
||||||
|
|
||||||
|
# Acquire a question generator and generate a function, then store the result.
|
||||||
active_questions[q_id] = categories[category]()()
|
active_questions[q_id] = categories[category]()()
|
||||||
|
|
||||||
return jsonify(active_questions[q_id])
|
# Make a shallow copy, hide 'answer' key.
|
||||||
|
question = copy.copy(active_questions[q_id])
|
||||||
|
del question['answer']
|
||||||
|
|
||||||
|
return question, 200
|
||||||
|
|
||||||
|
|
||||||
@current_app.route('/api/<category>/check')
|
class Category(Resource):
|
||||||
def check_question(category: str):
|
"""
|
||||||
if 'id' not in request.args.keys():
|
A category is a static designation for a problem to be classified as.
|
||||||
return jsonify({'error': ''}), 400
|
Any question will only have one category it belongs to.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get(self, category_id):
|
||||||
|
"""Get all information about a category"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Categories(Resource):
|
||||||
|
"""
|
||||||
|
Categories are static identifiers for the different types of questions available to users.
|
||||||
|
They are identified with pre-chosen identifiers.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get(self):
|
||||||
|
"""Get a list of all categories."""
|
||||||
|
return list(categories.keys()), 200
|
||||||
|
|
||||||
|
|
||||||
return jsonify({'answer': active_questions.get(request.args[id])})
|
|
||||||
|
|||||||
@@ -1,24 +1,33 @@
|
|||||||
from flask import Flask, render_template
|
from flask import Flask, render_template
|
||||||
|
from flask_restful import Api
|
||||||
|
|
||||||
from server.config import configs
|
from server.config import configs
|
||||||
|
|
||||||
def create_app(env = None):
|
|
||||||
|
def create_app(env=None):
|
||||||
app = Flask(
|
app = Flask(
|
||||||
__name__,
|
__name__,
|
||||||
static_folder="./../dist/static",
|
static_folder="./../dist/static",
|
||||||
template_folder="./../dist"
|
template_folder="./../dist"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Instantiate Flask-Restful API and register appropriate routes
|
||||||
|
from server.api import Question, Category, Categories
|
||||||
|
api = Api(app, prefix='/api/')
|
||||||
|
api.add_resource(Question, '/question/', '/question/<string:question_id>')
|
||||||
|
api.add_resource(Category, '/category/<string:category_id>')
|
||||||
|
api.add_resource(Categories, '/categories/')
|
||||||
|
|
||||||
if not env:
|
if not env:
|
||||||
env = app.config['ENV']
|
env = app.config['ENV']
|
||||||
app.config.from_object(configs[env])
|
app.config.from_object(configs[env])
|
||||||
|
|
||||||
@app.shell_context_processor
|
# @app.shell_context_processor
|
||||||
def shell_context():
|
# def shell_context():
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
#noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
from server import api
|
from server import api
|
||||||
|
|
||||||
@app.route('/', defaults={'path': ''})
|
@app.route('/', defaults={'path': ''})
|
||||||
|
|||||||
Reference in New Issue
Block a user