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.

46 lines
1.3 KiB

  1. """User API blueprint and endpoint definitions."""
  2. from flask import Blueprint, abort, request
  3. from atheneum.api.decorators import return_json
  4. from atheneum.api.model import APIResponse
  5. from atheneum.middleware import authentication_middleware
  6. from atheneum.model import User
  7. from atheneum.service import user_service, transformation_service
  8. USER_BLUEPRINT = Blueprint(
  9. name='user', import_name=__name__, url_prefix='/user')
  10. @USER_BLUEPRINT.route('/<name>', methods=['GET'])
  11. @return_json
  12. @authentication_middleware.require_token_auth
  13. def get_user(name: str) -> APIResponse:
  14. """
  15. Get a user.
  16. :return: user if exists, else 404
  17. """
  18. user = user_service.find_by_name(name)
  19. if user is not None:
  20. return APIResponse(user, 200)
  21. return abort(404)
  22. @USER_BLUEPRINT.route('/', methods=['POST'])
  23. @return_json
  24. @authentication_middleware.require_token_auth
  25. @authentication_middleware.require_role(required_role=User.ROLE_ADMIN)
  26. def register_user() -> APIResponse:
  27. """
  28. Register a user with the service.
  29. :return: The newly registered User
  30. """
  31. new_user: User = transformation_service.deserialize_model(
  32. User.__name__, request.json)
  33. registered_user = user_service.register(
  34. name=new_user.name,
  35. password=None,
  36. role=new_user.role
  37. )
  38. return APIResponse(payload=registered_user, status=200)