admin.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import logging
  2. from flask import Blueprint, jsonify, render_template
  3. from lakesuperior.api import admin as admin_api
  4. from lakesuperior.exceptions import (
  5. ChecksumValidationError, ResourceNotExistsError, TombstoneError)
  6. from lakesuperior.util.toolbox import fsize_fmt
  7. # Admin interface and REST API.
  8. logger = logging.getLogger(__name__)
  9. admin = Blueprint('admin', __name__)
  10. @admin.route('/stats', methods=['GET'])
  11. def stats():
  12. """
  13. Get repository statistics.
  14. """
  15. repo_stats = admin_api.stats()
  16. return render_template(
  17. 'stats.html', fsize_fmt=fsize_fmt, **repo_stats)
  18. @admin.route('/tools', methods=['GET'])
  19. def admin_tools():
  20. """
  21. Admin tools.
  22. @TODO stub.
  23. """
  24. return render_template('admin_tools.html')
  25. @admin.route('/<path:uid>/fixity', methods=['GET'])
  26. def fixity_check(uid):
  27. """
  28. Check the fixity of a resource.
  29. """
  30. uid = '/' + uid.strip('/')
  31. try:
  32. admin_api.fixity_check(uid)
  33. except ResourceNotExistsError as e:
  34. return str(e), 404
  35. except TombstoneError as e:
  36. return str(e), 410
  37. except ChecksumValidationError as e:
  38. check_pass = False
  39. else:
  40. check_pass = True
  41. return (
  42. jsonify({
  43. 'uid': uid,
  44. 'pass': check_pass,
  45. }),
  46. 200,
  47. {'content-type': 'application/json'}
  48. )