default_controller.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # coding=utf-8
  2. from tracim.views.controllers import Controller
  3. from pyramid.config import Configurator
  4. from pyramid.response import Response
  5. from pyramid.exceptions import NotFound
  6. from pyramid.httpexceptions import HTTPUnauthorized
  7. from pyramid.httpexceptions import HTTPForbidden
  8. from pyramid.security import forget
  9. from tracim.lib.utils.auth import MANAGE_CONTENT_PERM
  10. from tracim.lib.utils.auth import MANAGE_WORKSPACE_PERM
  11. from tracim.lib.utils.auth import MANAGE_GLOBAL_PERM
  12. from tracim.lib.utils.auth import READ_PERM
  13. from tracim.lib.utils.auth import CONTRIBUTE_PERM
  14. from tracim.lib.utils.auth import ADMIN_PERM
  15. from tracim.lib.utils.auth import USER_PERM
  16. class DefaultController(Controller):
  17. @classmethod
  18. def notfound_view(cls, request):
  19. request.response.status = 404
  20. return {}
  21. @classmethod
  22. def forbidden_view(cls, request):
  23. if request.authenticated_userid is None:
  24. response = HTTPUnauthorized()
  25. response.headers.update(forget(request))
  26. # user is logged in but doesn't have permissions, reject wholesale
  27. else:
  28. response = HTTPForbidden()
  29. return response
  30. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  31. @classmethod
  32. def test_config(cls, request):
  33. try:
  34. app_config = request.registry.settings['CFG']
  35. project = app_config.WEBSITE_TITLE
  36. except Exception as e:
  37. return Response(e, content_type='text/plain', status=500)
  38. return {'project': project}
  39. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  40. @classmethod
  41. def test_contributor_page(cls, request):
  42. try:
  43. app_config = request.registry.settings['CFG']
  44. project = 'contributor'
  45. except Exception as e:
  46. return Response(e, content_type='text/plain', status=500)
  47. return {'project': project}
  48. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  49. @classmethod
  50. def test_admin_page(cls, request):
  51. try:
  52. app_config = request.registry.settings['CFG']
  53. project = 'admin'
  54. except Exception as e:
  55. return Response(e, content_type='text/plain', status=500)
  56. return {'project': project}
  57. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  58. @classmethod
  59. def test_manager_page(cls, request):
  60. try:
  61. app_config = request.registry.settings['CFG']
  62. project = 'manager'
  63. except Exception as e:
  64. return Response(e, content_type='text/plain', status=500)
  65. return {'project': project}
  66. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  67. @classmethod
  68. def test_user_page(cls, request):
  69. try:
  70. app_config = request.registry.settings['CFG']
  71. project = 'user'
  72. except Exception as e:
  73. return Response(e, content_type='text/plain', status=500)
  74. return {'project': project}
  75. def bind(self, configurator: Configurator):
  76. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop static files
  77. configurator.add_static_view('static', 'static', cache_max_age=3600)
  78. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Do not rely
  79. # on static file for 404 view
  80. configurator.add_view(
  81. self.notfound_view,
  82. renderer='tracim:templates/404.jinja2',
  83. context=NotFound,
  84. )
  85. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  86. configurator.add_route('test_config', '/')
  87. configurator.add_view(
  88. self.test_config,
  89. route_name='test_config',
  90. renderer='tracim:templates/mytemplate.jinja2',
  91. )
  92. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  93. configurator.add_route('test_contributor', '/test_contributor')
  94. configurator.add_view(
  95. self.test_contributor_page,
  96. route_name='test_contributor',
  97. renderer='tracim:templates/mytemplate.jinja2',
  98. permission=CONTRIBUTE_PERM,
  99. )
  100. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  101. configurator.add_route('test_admin', '/test_admin')
  102. configurator.add_view(
  103. self.test_admin_page,
  104. route_name='test_admin',
  105. renderer='tracim:templates/mytemplate.jinja2',
  106. permission=ADMIN_PERM,
  107. )
  108. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  109. configurator.add_route('test_manager', '/test_manager')
  110. configurator.add_view(
  111. self.test_user_page,
  112. route_name='test_manager',
  113. renderer='tracim:templates/mytemplate.jinja2',
  114. permission=MANAGE_GLOBAL_PERM,
  115. )
  116. # TODO - G.M - 10-04-2018 - [cleanup][tempExample] - Drop this method
  117. configurator.add_route('test_user', '/test_user')
  118. configurator.add_view(
  119. self.test_user_page,
  120. route_name='test_user',
  121. renderer='tracim:templates/mytemplate.jinja2',
  122. permission=USER_PERM,
  123. )
  124. configurator.add_forbidden_view(self.forbidden_view)