__init__.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. try: # Python 3.5+
  3. from http import HTTPStatus
  4. except ImportError:
  5. from http import client as HTTPStatus
  6. from pyramid.config import Configurator
  7. from pyramid.authentication import BasicAuthAuthenticationPolicy
  8. from hapic.ext.pyramid import PyramidContext
  9. from sqlalchemy.exc import OperationalError
  10. from tracim_backend.extensions import hapic
  11. from tracim_backend.config import CFG
  12. from tracim_backend.lib.utils.request import TracimRequest
  13. from tracim_backend.lib.utils.authentification import basic_auth_check_credentials
  14. from tracim_backend.lib.utils.authentification import BASIC_AUTH_WEBUI_REALM
  15. from tracim_backend.lib.utils.authorization import AcceptAllAuthorizationPolicy
  16. from tracim_backend.lib.utils.authorization import TRACIM_DEFAULT_PERM
  17. from tracim_backend.lib.utils.cors import add_cors_support
  18. from tracim_backend.lib.webdav import WebdavAppFactory
  19. from tracim_backend.views import BASE_API_V2
  20. from tracim_backend.views.contents_api.html_document_controller import HTMLDocumentController # nopep8
  21. from tracim_backend.views.contents_api.threads_controller import ThreadController
  22. from tracim_backend.views.core_api.session_controller import SessionController
  23. from tracim_backend.views.core_api.system_controller import SystemController
  24. from tracim_backend.views.core_api.user_controller import UserController
  25. from tracim_backend.views.core_api.workspace_controller import WorkspaceController
  26. from tracim_backend.views.contents_api.comment_controller import CommentController
  27. from tracim_backend.views.contents_api.file_controller import FileController
  28. from tracim_backend.views.frontend import FrontendController
  29. from tracim_backend.views.errors import ErrorSchema
  30. from tracim_backend.exceptions import NotAuthenticated
  31. from tracim_backend.exceptions import PageNotFound
  32. from tracim_backend.exceptions import UserNotActive
  33. from tracim_backend.exceptions import InvalidId
  34. from tracim_backend.exceptions import InsufficientUserProfile
  35. from tracim_backend.exceptions import InsufficientUserRoleInWorkspace
  36. from tracim_backend.exceptions import WorkspaceNotFoundInTracimRequest
  37. from tracim_backend.exceptions import UserNotFoundInTracimRequest
  38. from tracim_backend.exceptions import ContentNotFoundInTracimRequest
  39. from tracim_backend.exceptions import WorkspaceNotFound
  40. from tracim_backend.exceptions import ContentNotFound
  41. from tracim_backend.exceptions import UserDoesNotExist
  42. from tracim_backend.exceptions import AuthenticationFailed
  43. from tracim_backend.exceptions import ContentTypeNotAllowed
  44. def web(global_config, **local_settings):
  45. """ This function returns a Pyramid WSGI application.
  46. """
  47. settings = global_config
  48. settings.update(local_settings)
  49. # set CFG object
  50. app_config = CFG(settings)
  51. app_config.configure_filedepot()
  52. settings['CFG'] = app_config
  53. configurator = Configurator(settings=settings, autocommit=True)
  54. # Add BasicAuthPolicy
  55. authn_policy = BasicAuthAuthenticationPolicy(
  56. basic_auth_check_credentials,
  57. realm=BASIC_AUTH_WEBUI_REALM,
  58. )
  59. configurator.include(add_cors_support)
  60. # make sure to add this before other routes to intercept OPTIONS
  61. configurator.add_cors_preflight_handler()
  62. # Default authorization : Accept anything.
  63. configurator.set_authorization_policy(AcceptAllAuthorizationPolicy())
  64. configurator.set_authentication_policy(authn_policy)
  65. # INFO - GM - 11-04-2018 - set default perm
  66. # setting default perm is needed to force authentification
  67. # mecanism in all views.
  68. configurator.set_default_permission(TRACIM_DEFAULT_PERM)
  69. # Override default request
  70. configurator.set_request_factory(TracimRequest)
  71. # Pyramids "plugin" include.
  72. configurator.include('pyramid_jinja2')
  73. # Add SqlAlchemy DB
  74. configurator.include('.models')
  75. # set Hapic
  76. context = PyramidContext(
  77. configurator=configurator,
  78. default_error_builder=ErrorSchema(),
  79. debug=app_config.DEBUG,
  80. )
  81. hapic.set_context(context)
  82. # INFO - G.M - 2018-07-04 - global-context exceptions
  83. # Not found
  84. context.handle_exception(PageNotFound, HTTPStatus.NOT_FOUND)
  85. # Bad request
  86. context.handle_exception(WorkspaceNotFoundInTracimRequest, HTTPStatus.BAD_REQUEST) # nopep8
  87. context.handle_exception(UserNotFoundInTracimRequest, HTTPStatus.BAD_REQUEST) # nopep8
  88. context.handle_exception(ContentNotFoundInTracimRequest, HTTPStatus.BAD_REQUEST) # nopep8
  89. context.handle_exception(WorkspaceNotFound, HTTPStatus.BAD_REQUEST)
  90. context.handle_exception(UserDoesNotExist, HTTPStatus.BAD_REQUEST)
  91. context.handle_exception(ContentNotFound, HTTPStatus.BAD_REQUEST)
  92. context.handle_exception(ContentTypeNotAllowed, HTTPStatus.BAD_REQUEST)
  93. context.handle_exception(InvalidId, HTTPStatus.BAD_REQUEST)
  94. # Auth exception
  95. context.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
  96. context.handle_exception(UserNotActive, HTTPStatus.FORBIDDEN)
  97. context.handle_exception(AuthenticationFailed, HTTPStatus.FORBIDDEN)
  98. context.handle_exception(InsufficientUserRoleInWorkspace, HTTPStatus.FORBIDDEN) # nopep8
  99. context.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
  100. # Internal server error
  101. context.handle_exception(OperationalError, HTTPStatus.INTERNAL_SERVER_ERROR)
  102. context.handle_exception(Exception, HTTPStatus.INTERNAL_SERVER_ERROR)
  103. # Add controllers
  104. session_controller = SessionController()
  105. system_controller = SystemController()
  106. user_controller = UserController()
  107. workspace_controller = WorkspaceController()
  108. comment_controller = CommentController()
  109. html_document_controller = HTMLDocumentController()
  110. thread_controller = ThreadController()
  111. file_controller = FileController()
  112. configurator.include(session_controller.bind, route_prefix=BASE_API_V2)
  113. configurator.include(system_controller.bind, route_prefix=BASE_API_V2)
  114. configurator.include(user_controller.bind, route_prefix=BASE_API_V2)
  115. configurator.include(workspace_controller.bind, route_prefix=BASE_API_V2)
  116. configurator.include(comment_controller.bind, route_prefix=BASE_API_V2)
  117. configurator.include(html_document_controller.bind, route_prefix=BASE_API_V2) # nopep8
  118. configurator.include(thread_controller.bind, route_prefix=BASE_API_V2)
  119. configurator.include(file_controller.bind, route_prefix=BASE_API_V2)
  120. if app_config.FRONTEND_SERVE:
  121. configurator.include('pyramid_mako')
  122. frontend_controller = FrontendController(app_config.FRONTEND_DIST_FOLDER_PATH) # nopep8
  123. configurator.include(frontend_controller.bind)
  124. hapic.add_documentation_view(
  125. '/api/v2/doc',
  126. 'Tracim v2 API',
  127. 'API of Tracim v2',
  128. )
  129. return configurator.make_wsgi_app()
  130. def webdav(global_config, **local_settings):
  131. settings = global_config
  132. settings.update(local_settings)
  133. app_factory = WebdavAppFactory(
  134. tracim_config_file_path=settings['__file__'],
  135. )
  136. return app_factory.get_wsgi_app()