__init__.py 6.2KB

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