folder_controller.py 7.0KB

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