__init__.py 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 tracim.extensions import hapic
  8. from tracim.config import CFG
  9. from tracim.lib.utils.request import TracimRequest
  10. from tracim.lib.utils.authentification import basic_auth_check_credentials
  11. from tracim.lib.utils.authentification import BASIC_AUTH_WEBUI_REALM
  12. from tracim.lib.utils.authorization import AcceptAllAuthorizationPolicy
  13. from tracim.lib.utils.authorization import TRACIM_DEFAULT_PERM
  14. from tracim.views import BASE_API_V2
  15. from tracim.views.core_api.session_controller import SessionController
  16. from tracim.views.errors import ErrorSchema
  17. def main(global_config, **settings):
  18. """ This function returns a Pyramid WSGI application.
  19. """
  20. # set CFG object
  21. app_config = CFG(settings)
  22. app_config.configure_filedepot()
  23. settings['CFG'] = app_config
  24. configurator = Configurator(settings=settings, autocommit=True)
  25. # Add BasicAuthPolicy
  26. authn_policy = BasicAuthAuthenticationPolicy(
  27. basic_auth_check_credentials,
  28. realm=BASIC_AUTH_WEBUI_REALM,
  29. )
  30. # Default authorization : Accept anything.
  31. configurator.set_authorization_policy(AcceptAllAuthorizationPolicy())
  32. configurator.set_authentication_policy(authn_policy)
  33. # INFO - GM - 11-04-2018 - set default perm
  34. # setting default perm is needed to force authentification
  35. # mecanism in all views.
  36. configurator.set_default_permission(TRACIM_DEFAULT_PERM)
  37. # Override default request
  38. configurator.set_request_factory(TracimRequest)
  39. # Pyramids "plugin" include.
  40. configurator.include('pyramid_jinja2')
  41. # Add SqlAlchemy DB
  42. configurator.include('.models')
  43. # set Hapic
  44. hapic.set_context(
  45. PyramidContext(
  46. configurator=configurator,
  47. default_error_builder=ErrorSchema()
  48. )
  49. )
  50. # Add controllers
  51. session_api = SessionController()
  52. configurator.include(session_api.bind, route_prefix=BASE_API_V2)
  53. hapic.add_documentation_view(
  54. '/api/v2/doc',
  55. 'Tracim v2 API',
  56. 'API of Tracim v2',
  57. )
  58. return configurator.make_wsgi_app()