__init__.py 7.1KB

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