folder_controller.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 FolderContentModifySchema
  16. from tracim_backend.views.core_api.schemas import TextBasedRevisionSchema
  17. from tracim_backend.views.core_api.schemas import SetContentStatusSchema
  18. from tracim_backend.views.core_api.schemas import WorkspaceAndContentIdPathSchema # nopep8
  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.app_models.contents import CONTENT_TYPES
  26. from tracim_backend.app_models.contents import FOLDER_TYPE
  27. from tracim_backend.models.revision_protection import new_revision
  28. SWAGGER_TAG__Folders_ENDPOINTS = 'Folders'
  29. class FolderController(Controller):
  30. @hapic.with_api_doc(tags=[SWAGGER_TAG__Folders_ENDPOINTS])
  31. @require_workspace_role(UserRoleInWorkspace.READER)
  32. @require_content_types([FOLDER_TYPE])
  33. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  34. @hapic.output_body(TextBasedContentSchema())
  35. def get_folder(self, context, request: TracimRequest, hapic_data=None) -> ContentInContext: # nopep8
  36. """
  37. Get folder info
  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__Folders_ENDPOINTS])
  53. @hapic.handle_exception(EmptyLabelNotAllowed, HTTPStatus.BAD_REQUEST)
  54. @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
  55. @require_content_types([FOLDER_TYPE])
  56. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  57. @hapic.input_body(FolderContentModifySchema())
  58. @hapic.output_body(TextBasedContentSchema())
  59. def update_folder(self, context, request: TracimRequest, hapic_data=None) -> ContentInContext: # nopep8
  60. """
  61. update folder
  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.set_allowed_content(
  86. content=content,
  87. allowed_content_type_slug_list=hapic_data.body.sub_content_types # nopep8
  88. )
  89. api.save(content)
  90. return api.get_content_in_context(content)
  91. @hapic.with_api_doc(tags=[SWAGGER_TAG__Folders_ENDPOINTS])
  92. @require_workspace_role(UserRoleInWorkspace.READER)
  93. @require_content_types([FOLDER_TYPE])
  94. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  95. @hapic.output_body(TextBasedRevisionSchema(many=True))
  96. def get_folder_revisions(
  97. self,
  98. context,
  99. request: TracimRequest,
  100. hapic_data=None
  101. ) -> typing.List[RevisionInContext]:
  102. """
  103. get folder revisions
  104. """
  105. app_config = request.registry.settings['CFG']
  106. api = ContentApi(
  107. show_archived=True,
  108. show_deleted=True,
  109. current_user=request.current_user,
  110. session=request.dbsession,
  111. config=app_config,
  112. )
  113. content = api.get_one(
  114. hapic_data.path.content_id,
  115. content_type=CONTENT_TYPES.Any_SLUG
  116. )
  117. revisions = content.revisions
  118. return [
  119. api.get_revision_in_context(revision)
  120. for revision in revisions
  121. ]
  122. @hapic.with_api_doc(tags=[SWAGGER_TAG__Folders_ENDPOINTS])
  123. @require_workspace_role(UserRoleInWorkspace.CONTRIBUTOR)
  124. @require_content_types([FOLDER_TYPE])
  125. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  126. @hapic.input_body(SetContentStatusSchema())
  127. @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT) # nopep8
  128. def set_folder_status(self, context, request: TracimRequest, hapic_data=None) -> None: # nopep8
  129. """
  130. set folder status
  131. """
  132. app_config = request.registry.settings['CFG']
  133. api = ContentApi(
  134. show_archived=True,
  135. show_deleted=True,
  136. current_user=request.current_user,
  137. session=request.dbsession,
  138. config=app_config,
  139. )
  140. content = api.get_one(
  141. hapic_data.path.content_id,
  142. content_type=CONTENT_TYPES.Any_SLUG
  143. )
  144. with new_revision(
  145. session=request.dbsession,
  146. tm=transaction.manager,
  147. content=content
  148. ):
  149. api.set_status(
  150. content,
  151. hapic_data.body.status,
  152. )
  153. api.save(content)
  154. return
  155. def bind(self, configurator: Configurator) -> None:
  156. # Get folder
  157. configurator.add_route(
  158. 'folder',
  159. '/workspaces/{workspace_id}/folders/{content_id}',
  160. request_method='GET'
  161. )
  162. configurator.add_view(self.get_folder, route_name='folder') # nopep8
  163. # update folder
  164. configurator.add_route(
  165. 'update_folder',
  166. '/workspaces/{workspace_id}/folders/{content_id}',
  167. request_method='PUT'
  168. ) # nopep8
  169. configurator.add_view(self.update_folder, route_name='update_folder') # nopep8
  170. # get folder revisions
  171. configurator.add_route(
  172. 'folder_revisions',
  173. '/workspaces/{workspace_id}/folders/{content_id}/revisions', # nopep8
  174. request_method='GET'
  175. )
  176. configurator.add_view(self.get_folder_revisions, route_name='folder_revisions') # nopep8
  177. # get folder revisions
  178. configurator.add_route(
  179. 'set_folder_status',
  180. '/workspaces/{workspace_id}/folders/{content_id}/status', # nopep8
  181. request_method='PUT'
  182. )
  183. configurator.add_view(self.set_folder_status, route_name='set_folder_status') # nopep8