選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

api.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # @name: api.py
  2. # @creation_date: 2023-01-12
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: Exposes database data as exportable JSON for an API
  6. # @acknowledgements:
  7. from flask import Blueprint, render_template, request, flash, redirect, url_for, jsonify
  8. from flask_login import login_required, current_user
  9. from .models import Resource, User
  10. from .schemas import UserSchema, ToolSchema, PracticeSchema, BookSchema
  11. api = Blueprint('api', __name__)
  12. # route for exporting all users in database
  13. @api.route('/api/users')
  14. @login_required
  15. def get_users():
  16. users = User.query.all()
  17. users_schema = UserSchema(many=True)
  18. result = users_schema.dump(users)
  19. # return all rows as a JSON array of objects
  20. return jsonify(result)
  21. # route for exporting all tools in database
  22. @api.route('/api/tools')
  23. def get_tools():
  24. type = 'tool'
  25. resources = Resource.query.filter_by(type=type)
  26. resources_schema = ToolSchema(many=True)
  27. result = resources_schema.dump(resources)
  28. # return all rows as a JSON array of objects
  29. return jsonify(result)
  30. # route for exporting all practices in database
  31. @api.route('/api/practices')
  32. def get_practices():
  33. type = 'practice'
  34. resources = Resource.query.filter_by(type=type)
  35. resources_schema = PracticeSchema(many=True)
  36. result = resources_schema.dump(resources)
  37. # return all rows as a JSON array of objects
  38. return jsonify(result)
  39. # route for exporting all books in database
  40. @api.route('/api/books')
  41. def get_books():
  42. type = 'book'
  43. resources = Resource.query.filter_by(type=type)
  44. resources_schema = BookSchema(many=True)
  45. result = resources_schema.dump(resources)
  46. # return all rows as a JSON array of objects
  47. return jsonify(result)