|
-
-
-
-
-
-
-
-
-
- from flask import Blueprint, render_template, request, flash, redirect, url_for, jsonify
- from flask_login import login_required, current_user
- from .models import Resource, User
- from .schemas import UserSchema, ToolSchema, PracticeSchema, BookSchema
-
- api = Blueprint('api', __name__)
-
-
- def get_resource_json(type):
- resources = Resource.query.filter_by(type=type)
- if (type=='tool'):
- resources_schema = ToolSchema(many=True)
- elif (type=='practice'):
- resources_schema = PracticeSchema(many=True)
- elif (type=='book'):
- resources_schema = BookSchema(many=True)
- result = resources_schema.dump(resources)
-
- result = jsonify(result)
-
- return result
-
-
- @api.route('/api')
- def api_page():
- return render_template('api.html')
-
-
- @api.route('/api/users')
- @login_required
- def get_users():
- users = User.query.all()
- users_schema = UserSchema(many=True)
- result = users_schema.dump(users)
-
-
- return jsonify(result)
-
-
- @api.route('/api/tools')
- def get_tools():
- type = 'tool'
- output = get_resource_json(type)
- return output
-
-
- @api.route('/api/practices')
- def get_practices():
- type = 'practice'
- output = get_resource_json(type)
- return output
-
-
- @api.route('/api/books')
- def get_books():
- type = 'book'
- output = get_resource_json(type)
- return output
|