default_controller.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # coding=utf-8
  2. from pyramid.httpexceptions import HTTPNotFound
  3. from tracim import TracimRequest
  4. from tracim.extensions import hapic
  5. from tracim.views.controllers import Controller
  6. from tracim.views.errors import Error
  7. from pyramid.config import Configurator
  8. class DefaultController(Controller):
  9. def notfound_view(
  10. self,
  11. exception: HTTPNotFound,
  12. request: TracimRequest
  13. ):
  14. """
  15. Catch Not Found Exception
  16. :param exception: Exception Object
  17. :param request: current Request
  18. :return: 500 Internal Server Error with same format as others errors
  19. """
  20. request.response.status = 404
  21. return hapic.context.get_default_error_builder().build_from_exception(
  22. exception=exception
  23. )
  24. def exception_view(
  25. self,
  26. exception: Exception,
  27. request: TracimRequest
  28. ):
  29. """
  30. Catch all exceptions not handled in view
  31. :param exception: Exception Object
  32. :param request: current Request
  33. :return: 500 Internal Server Error with same format as others errors
  34. """
  35. request.response.status = 500
  36. return hapic.context.get_default_error_builder().build_from_exception(
  37. exception=exception
  38. )
  39. def swagger_doc(self, request: TracimRequest):
  40. return hapic.generate_doc(
  41. title='Tracim v2 API',
  42. description='API of Tracim v2',
  43. )
  44. def bind(self, configurator: Configurator):
  45. configurator.add_view(
  46. self.notfound_view,
  47. renderer='json',
  48. context=HTTPNotFound,
  49. )
  50. configurator.add_view(
  51. self.exception_view,
  52. renderer='json',
  53. context=Exception,
  54. )
  55. configurator.add_route(
  56. 'swagger_doc',
  57. '/swagger_doc',
  58. request_method='GET',
  59. )
  60. configurator.add_view(
  61. self.swagger_doc,
  62. route_name='swagger_doc',
  63. renderer='json',
  64. )