__init__.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import time
  4. from pyramid.config import Configurator
  5. from pyramid.authentication import BasicAuthAuthenticationPolicy
  6. from hapic.ext.pyramid import PyramidContext
  7. from pyramid.exceptions import NotFound
  8. from sqlalchemy.exc import OperationalError
  9. from tracim.extensions import hapic
  10. from tracim.config import CFG
  11. from tracim.lib.utils.request import TracimRequest
  12. from tracim.lib.utils.authentification import basic_auth_check_credentials
  13. from tracim.lib.utils.authentification import BASIC_AUTH_WEBUI_REALM
  14. from tracim.lib.utils.authorization import AcceptAllAuthorizationPolicy
  15. from tracim.lib.utils.authorization import TRACIM_DEFAULT_PERM
  16. from tracim.lib.webdav import WebdavAppFactory
  17. from tracim.views import BASE_API_V2
  18. from tracim.views.contents_api.html_document_controller import HTMLDocumentController # nopep8
  19. from tracim.views.contents_api.threads_controller import ThreadController
  20. from tracim.views.core_api.session_controller import SessionController
  21. from tracim.views.core_api.system_controller import SystemController
  22. from tracim.views.core_api.user_controller import UserController
  23. from tracim.views.core_api.workspace_controller import WorkspaceController
  24. from tracim.views.contents_api.comment_controller import CommentController
  25. from tracim.views.errors import ErrorSchema
  26. from tracim.lib.utils.cors import add_cors_support
  27. def web(global_config, **local_settings):
  28. """ This function returns a Pyramid WSGI application.
  29. """
  30. settings = global_config
  31. settings.update(local_settings)
  32. # set CFG object
  33. app_config = CFG(settings)
  34. app_config.configure_filedepot()
  35. settings['CFG'] = app_config
  36. configurator = Configurator(settings=settings, autocommit=True)
  37. # Add BasicAuthPolicy
  38. authn_policy = BasicAuthAuthenticationPolicy(
  39. basic_auth_check_credentials,
  40. realm=BASIC_AUTH_WEBUI_REALM,
  41. )
  42. configurator.include(add_cors_support)
  43. # make sure to add this before other routes to intercept OPTIONS
  44. configurator.add_cors_preflight_handler()
  45. # Default authorization : Accept anything.
  46. configurator.set_authorization_policy(AcceptAllAuthorizationPolicy())
  47. configurator.set_authentication_policy(authn_policy)
  48. # INFO - GM - 11-04-2018 - set default perm
  49. # setting default perm is needed to force authentification
  50. # mecanism in all views.
  51. configurator.set_default_permission(TRACIM_DEFAULT_PERM)
  52. # Override default request
  53. configurator.set_request_factory(TracimRequest)
  54. # Pyramids "plugin" include.
  55. configurator.include('pyramid_jinja2')
  56. # Add SqlAlchemy DB
  57. configurator.include('.models')
  58. # set Hapic
  59. context = PyramidContext(
  60. configurator=configurator,
  61. default_error_builder=ErrorSchema(),
  62. debug=app_config.DEBUG,
  63. )
  64. hapic.set_context(context)
  65. context.handle_exception(NotFound, 404)
  66. context.handle_exception(OperationalError, 500)
  67. context.handle_exception(Exception, 500)
  68. # Add controllers
  69. session_controller = SessionController()
  70. system_controller = SystemController()
  71. user_controller = UserController()
  72. workspace_controller = WorkspaceController()
  73. comment_controller = CommentController()
  74. html_document_controller = HTMLDocumentController()
  75. thread_controller = ThreadController()
  76. configurator.include(session_controller.bind, route_prefix=BASE_API_V2)
  77. configurator.include(system_controller.bind, route_prefix=BASE_API_V2)
  78. configurator.include(user_controller.bind, route_prefix=BASE_API_V2)
  79. configurator.include(workspace_controller.bind, route_prefix=BASE_API_V2)
  80. configurator.include(comment_controller.bind, route_prefix=BASE_API_V2)
  81. configurator.include(html_document_controller.bind, route_prefix=BASE_API_V2) # nopep8
  82. configurator.include(thread_controller.bind, route_prefix=BASE_API_V2)
  83. hapic.add_documentation_view(
  84. '/api/v2/doc',
  85. 'Tracim v2 API',
  86. 'API of Tracim v2',
  87. )
  88. return configurator.make_wsgi_app()
  89. def webdav(global_config, **local_settings):
  90. settings = global_config
  91. settings.update(local_settings)
  92. app_factory = WebdavAppFactory(
  93. tracim_config_file_path=settings['__file__'],
  94. )
  95. return app_factory.get_wsgi_app()