threads_controller.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. show_archived=True,
  42. show_deleted=True,
  43. current_user=request.current_user,
  44. session=request.dbsession,
  45. config=app_config,
  46. )
  47. content = api.get_one(
  48. hapic_data.path.content_id,
  49. content_type=CONTENT_TYPES.Any_SLUG
  50. )
  51. return api.get_content_in_context(content)
  52. @hapic.with_api_doc(tags=[SWAGGER_TAG__THREAD_ENDPOINTS])
  53. @hapic.handle_exception(EmptyLabelNotAllowed, HTTPStatus.BAD_REQUEST)
  54. @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
  55. @require_content_types([thread_type])
  56. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  57. @hapic.input_body(TextBasedContentModifySchema())
  58. @hapic.output_body(TextBasedContentSchema())
  59. def update_thread(self, context, request: TracimRequest, hapic_data=None) -> ContentInContext: # nopep8
  60. """
  61. update thread
  62. """
  63. app_config = request.registry.settings['CFG']
  64. api = ContentApi(
  65. show_archived=True,
  66. show_deleted=True,
  67. current_user=request.current_user,
  68. session=request.dbsession,
  69. config=app_config,
  70. )
  71. content = api.get_one(
  72. hapic_data.path.content_id,
  73. content_type=CONTENT_TYPES.Any_SLUG
  74. )
  75. with new_revision(
  76. session=request.dbsession,
  77. tm=transaction.manager,
  78. content=content
  79. ):
  80. api.update_content(
  81. item=content,
  82. new_label=hapic_data.body.label,
  83. new_content=hapic_data.body.raw_content,
  84. )
  85. api.save(content)
  86. return api.get_content_in_context(content)
  87. @hapic.with_api_doc(tags=[SWAGGER_TAG__THREAD_ENDPOINTS])
  88. @require_workspace_role(UserRoleInWorkspace.READER)
  89. @require_content_types([thread_type])
  90. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  91. @hapic.output_body(TextBasedRevisionSchema(many=True))
  92. def get_thread_revisions(
  93. self,
  94. context,
  95. request: TracimRequest,
  96. hapic_data=None
  97. ) -> typing.List[RevisionInContext]:
  98. """
  99. get thread revisions
  100. """
  101. app_config = request.registry.settings['CFG']
  102. api = ContentApi(
  103. show_archived=True,
  104. show_deleted=True,
  105. current_user=request.current_user,
  106. session=request.dbsession,
  107. config=app_config,
  108. )
  109. content = api.get_one(
  110. hapic_data.path.content_id,
  111. content_type=CONTENT_TYPES.Any_SLUG
  112. )
  113. revisions = content.revisions
  114. return [
  115. api.get_revision_in_context(revision)
  116. for revision in revisions
  117. ]
  118. @hapic.with_api_doc(tags=[SWAGGER_TAG__THREAD_ENDPOINTS])
  119. @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
  120. @require_content_types([thread_type])
  121. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  122. @hapic.input_body(SetContentStatusSchema())
  123. @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT) # nopep8
  124. def set_thread_status(self, context, request: TracimRequest, hapic_data=None) -> None: # nopep8
  125. """
  126. set thread status
  127. """
  128. app_config = request.registry.settings['CFG']
  129. api = ContentApi(
  130. show_archived=True,
  131. show_deleted=True,
  132. current_user=request.current_user,
  133. session=request.dbsession,
  134. config=app_config,
  135. )
  136. content = api.get_one(
  137. hapic_data.path.content_id,
  138. content_type=CONTENT_TYPES.Any_SLUG
  139. )
  140. with new_revision(
  141. session=request.dbsession,
  142. tm=transaction.manager,
  143. content=content
  144. ):
  145. api.set_status(
  146. content,
  147. hapic_data.body.status,
  148. )
  149. api.save(content)
  150. return
  151. def bind(self, configurator: Configurator) -> None:
  152. # Get thread
  153. configurator.add_route(
  154. 'thread',
  155. '/workspaces/{workspace_id}/threads/{content_id}',
  156. request_method='GET'
  157. )
  158. configurator.add_view(self.get_thread, route_name='thread') # nopep8
  159. # update thread
  160. configurator.add_route(
  161. 'update_thread',
  162. '/workspaces/{workspace_id}/threads/{content_id}',
  163. request_method='PUT'
  164. ) # nopep8
  165. configurator.add_view(self.update_thread, route_name='update_thread') # nopep8
  166. # get thread revisions
  167. configurator.add_route(
  168. 'thread_revisions',
  169. '/workspaces/{workspace_id}/threads/{content_id}/revisions', # nopep8
  170. request_method='GET'
  171. )
  172. configurator.add_view(self.get_thread_revisions, route_name='thread_revisions') # nopep8
  173. # get thread revisions
  174. configurator.add_route(
  175. 'set_thread_status',
  176. '/workspaces/{workspace_id}/threads/{content_id}/status', # nopep8
  177. request_method='PUT'
  178. )
  179. configurator.add_view(self.set_thread_status, route_name='set_thread_status') # nopep8