threads_controller.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # coding=utf-8
  2. import typing
  3. import transaction
  4. from pyramid.config import Configurator
  5. from tracim_backend.models.data import UserRoleInWorkspace
  6. try: # Python 3.5+
  7. from http import HTTPStatus
  8. except ImportError:
  9. from http import client as HTTPStatus
  10. from tracim_backend import TracimRequest
  11. from tracim_backend.extensions import hapic
  12. from tracim_backend.lib.core.content import ContentApi
  13. from tracim_backend.views.controllers import Controller
  14. from tracim_backend.views.core_api.schemas import TextBasedContentSchema
  15. from tracim_backend.views.core_api.schemas import TextBasedRevisionSchema
  16. from tracim_backend.views.core_api.schemas import SetContentStatusSchema
  17. from tracim_backend.views.core_api.schemas import TextBasedContentModifySchema
  18. from tracim_backend.views.core_api.schemas import WorkspaceAndContentIdPathSchema
  19. from tracim_backend.views.core_api.schemas import NoContentSchema
  20. from tracim_backend.lib.utils.authorization import require_content_types
  21. from tracim_backend.lib.utils.authorization import require_workspace_role
  22. from tracim_backend.exceptions import EmptyLabelNotAllowed
  23. from tracim_backend.models.context_models import ContentInContext
  24. from tracim_backend.models.context_models import RevisionInContext
  25. from tracim_backend.models.contents import CONTENT_TYPES
  26. from tracim_backend.models.contents import thread_type
  27. from tracim_backend.models.revision_protection import new_revision
  28. SWAGGER_TAG__THREAD_ENDPOINTS = 'Threads'
  29. class ThreadController(Controller):
  30. @hapic.with_api_doc(tags=[SWAGGER_TAG__THREAD_ENDPOINTS])
  31. @require_workspace_role(UserRoleInWorkspace.READER)
  32. @require_content_types([thread_type])
  33. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  34. @hapic.output_body(TextBasedContentSchema())
  35. def get_thread(self, context, request: TracimRequest, hapic_data=None) -> ContentInContext: # nopep8
  36. """
  37. Get thread content
  38. """
  39. app_config = request.registry.settings['CFG']
  40. api = ContentApi(
  41. current_user=request.current_user,
  42. session=request.dbsession,
  43. config=app_config,
  44. )
  45. content = api.get_one(
  46. hapic_data.path.content_id,
  47. content_type=CONTENT_TYPES.Any_SLUG
  48. )
  49. return api.get_content_in_context(content)
  50. @hapic.with_api_doc(tags=[SWAGGER_TAG__THREAD_ENDPOINTS])
  51. @hapic.handle_exception(EmptyLabelNotAllowed, HTTPStatus.BAD_REQUEST)
  52. @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
  53. @require_content_types([thread_type])
  54. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  55. @hapic.input_body(TextBasedContentModifySchema())
  56. @hapic.output_body(TextBasedContentSchema())
  57. def update_thread(self, context, request: TracimRequest, hapic_data=None) -> ContentInContext: # nopep8
  58. """
  59. update thread
  60. """
  61. app_config = request.registry.settings['CFG']
  62. api = ContentApi(
  63. current_user=request.current_user,
  64. session=request.dbsession,
  65. config=app_config,
  66. )
  67. content = api.get_one(
  68. hapic_data.path.content_id,
  69. content_type=CONTENT_TYPES.Any_SLUG
  70. )
  71. with new_revision(
  72. session=request.dbsession,
  73. tm=transaction.manager,
  74. content=content
  75. ):
  76. api.update_content(
  77. item=content,
  78. new_label=hapic_data.body.label,
  79. new_content=hapic_data.body.raw_content,
  80. )
  81. api.save(content)
  82. return api.get_content_in_context(content)
  83. @hapic.with_api_doc(tags=[SWAGGER_TAG__THREAD_ENDPOINTS])
  84. @require_workspace_role(UserRoleInWorkspace.READER)
  85. @require_content_types([thread_type])
  86. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  87. @hapic.output_body(TextBasedRevisionSchema(many=True))
  88. def get_thread_revisions(
  89. self,
  90. context,
  91. request: TracimRequest,
  92. hapic_data=None
  93. ) -> typing.List[RevisionInContext]:
  94. """
  95. get thread revisions
  96. """
  97. app_config = request.registry.settings['CFG']
  98. api = ContentApi(
  99. current_user=request.current_user,
  100. session=request.dbsession,
  101. config=app_config,
  102. )
  103. content = api.get_one(
  104. hapic_data.path.content_id,
  105. content_type=CONTENT_TYPES.Any_SLUG
  106. )
  107. revisions = content.revisions
  108. return [
  109. api.get_revision_in_context(revision)
  110. for revision in revisions
  111. ]
  112. @hapic.with_api_doc(tags=[SWAGGER_TAG__THREAD_ENDPOINTS])
  113. @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
  114. @require_content_types([thread_type])
  115. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  116. @hapic.input_body(SetContentStatusSchema())
  117. @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT) # nopep8
  118. def set_thread_status(self, context, request: TracimRequest, hapic_data=None) -> None: # nopep8
  119. """
  120. set thread status
  121. """
  122. app_config = request.registry.settings['CFG']
  123. api = ContentApi(
  124. current_user=request.current_user,
  125. session=request.dbsession,
  126. config=app_config,
  127. )
  128. content = api.get_one(
  129. hapic_data.path.content_id,
  130. content_type=CONTENT_TYPES.Any_SLUG
  131. )
  132. with new_revision(
  133. session=request.dbsession,
  134. tm=transaction.manager,
  135. content=content
  136. ):
  137. api.set_status(
  138. content,
  139. hapic_data.body.status,
  140. )
  141. api.save(content)
  142. return
  143. def bind(self, configurator: Configurator) -> None:
  144. # Get thread
  145. configurator.add_route(
  146. 'thread',
  147. '/workspaces/{workspace_id}/threads/{content_id}',
  148. request_method='GET'
  149. )
  150. configurator.add_view(self.get_thread, route_name='thread') # nopep8
  151. # update thread
  152. configurator.add_route(
  153. 'update_thread',
  154. '/workspaces/{workspace_id}/threads/{content_id}',
  155. request_method='PUT'
  156. ) # nopep8
  157. configurator.add_view(self.update_thread, route_name='update_thread') # nopep8
  158. # get thread revisions
  159. configurator.add_route(
  160. 'thread_revisions',
  161. '/workspaces/{workspace_id}/threads/{content_id}/revisions', # nopep8
  162. request_method='GET'
  163. )
  164. configurator.add_view(self.get_thread_revisions, route_name='thread_revisions') # nopep8
  165. # get thread revisions
  166. configurator.add_route(
  167. 'set_thread_status',
  168. '/workspaces/{workspace_id}/threads/{content_id}/status', # nopep8
  169. request_method='PUT'
  170. )
  171. configurator.add_view(self.set_thread_status, route_name='set_thread_status') # nopep8