An ebook/comic library service and web client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.4 KiB

  1. """User API blueprint and endpoint definitions."""
  2. from flask import Blueprint, abort, request, g
  3. from atheneum.api.decorators import return_json
  4. from atheneum.api.model import APIResponse, APIMessage, APIPage
  5. from atheneum.middleware import authentication_middleware
  6. from atheneum.model import User
  7. from atheneum.service import (
  8. patch_service,
  9. transformation_service,
  10. user_service
  11. )
  12. from atheneum.service.patch_service import get_patch_fields
  13. from atheneum.service.role_service import Role
  14. from atheneum.utility.pagination_utility import get_pagination_params
  15. USER_BLUEPRINT = Blueprint(
  16. name='user', import_name=__name__, url_prefix='/user')
  17. @USER_BLUEPRINT.route('', methods=['GET'])
  18. @return_json
  19. @authentication_middleware.require_token_auth
  20. @authentication_middleware.require_role(required_role=Role.USER)
  21. def get_users() -> APIResponse:
  22. """
  23. Get a list of users.
  24. :return: a paginated list of users
  25. """
  26. page, per_page = get_pagination_params(request.args)
  27. user_page = user_service.get_users(page, per_page)
  28. if user_page is not None:
  29. return APIResponse(APIPage.from_page(user_page), 200)
  30. return abort(404)
  31. @USER_BLUEPRINT.route('/<name>', methods=['GET'])
  32. @return_json
  33. @authentication_middleware.require_token_auth
  34. @authentication_middleware.require_role(required_role=Role.USER)
  35. def get_user(name: str) -> APIResponse:
  36. """
  37. Get a user.
  38. :return: user if exists, else 404
  39. """
  40. user = user_service.find_by_name(name)
  41. if user is not None:
  42. return APIResponse(user, 200)
  43. return abort(404)
  44. @USER_BLUEPRINT.route('/<name>', methods=['PATCH'])
  45. @return_json
  46. @authentication_middleware.require_token_auth
  47. @authentication_middleware.require_role(required_role=Role.USER)
  48. def patch_user(name: str) -> APIResponse:
  49. """
  50. Patch a user.
  51. :return: user if patched, 4xx error on patching issue, 404 on nonexistent
  52. """
  53. user = user_service.find_by_name(name)
  54. if user is not None:
  55. user_patch: User = transformation_service.deserialize_model(
  56. User, request.json)
  57. patched_user = patch_service.patch(
  58. g.user, user, user_patch, get_patch_fields(request.json))
  59. return APIResponse(patched_user, 200)
  60. return abort(404)
  61. @USER_BLUEPRINT.route('', methods=['POST'])
  62. @return_json
  63. @authentication_middleware.require_token_auth
  64. @authentication_middleware.require_role(required_role=Role.ADMIN)
  65. def register_user() -> APIResponse:
  66. """
  67. Register a user with the service.
  68. :return: The newly registered User
  69. """
  70. new_user: User = transformation_service.deserialize_model(
  71. User, request.json)
  72. requested_password = None
  73. if 'password' in request.json:
  74. requested_password = request.json['password'].strip()
  75. registered_user = user_service.register(
  76. name=new_user.name,
  77. password=requested_password,
  78. role=new_user.role
  79. )
  80. return APIResponse(payload=registered_user, status=200)
  81. @USER_BLUEPRINT.route('/<name>', methods=['DELETE'])
  82. @return_json
  83. @authentication_middleware.require_token_auth
  84. @authentication_middleware.require_role(required_role=Role.ADMIN)
  85. def delete_user(name: str) -> APIResponse:
  86. """
  87. Delete a user with the service.
  88. :return: The newly registered User
  89. """
  90. user = user_service.find_by_name(name)
  91. if user is not None:
  92. user_service.delete(user)
  93. return APIResponse(
  94. APIMessage(True, 'Successfully Deleted'), status=200)
  95. return abort(404)