A multipurpose python flask API server and administration SPA
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.

47 lines
1.4 KiB

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