comment_controller.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # coding=utf-8
  2. import transaction
  3. from pyramid.config import Configurator
  4. try: # Python 3.5+
  5. from http import HTTPStatus
  6. except ImportError:
  7. from http import client as HTTPStatus
  8. from tracim import TracimRequest
  9. from tracim.extensions import hapic
  10. from tracim.lib.core.content import ContentApi
  11. from tracim.lib.core.workspace import WorkspaceApi
  12. from tracim.views.controllers import Controller
  13. from tracim.views.core_api.schemas import CommentSchema
  14. from tracim.views.core_api.schemas import CommentsPathSchema
  15. from tracim.views.core_api.schemas import SetCommentSchema
  16. from tracim.views.core_api.schemas import WorkspaceAndContentIdPathSchema
  17. from tracim.views.core_api.schemas import NoContentSchema
  18. from tracim.exceptions import WorkspaceNotFound
  19. from tracim.exceptions import InsufficientUserProfile
  20. from tracim.exceptions import NotAuthenticated
  21. from tracim.exceptions import AuthenticationFailed
  22. from tracim.models.contents import ContentTypeLegacy as ContentType
  23. from tracim.models.revision_protection import new_revision
  24. COMMENT_ENDPOINTS_TAG = 'Comments'
  25. class CommentController(Controller):
  26. pass
  27. @hapic.with_api_doc(tags=[COMMENT_ENDPOINTS_TAG])
  28. @hapic.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
  29. @hapic.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
  30. @hapic.handle_exception(WorkspaceNotFound, HTTPStatus.FORBIDDEN)
  31. @hapic.handle_exception(AuthenticationFailed, HTTPStatus.BAD_REQUEST)
  32. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  33. @hapic.output_body(CommentSchema(many=True),)
  34. def content_comments(self, context, request: TracimRequest, hapic_data=None):
  35. """
  36. Get all comments related to a content in asc order (first is the oldest)
  37. """
  38. # login = hapic_data.body
  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=ContentType.Any
  48. )
  49. comments = content.get_comments()
  50. comments.sort(key=lambda comment: comment.created)
  51. return [api.get_content_in_context(comment)
  52. for comment in comments
  53. ]
  54. @hapic.with_api_doc(tags=[COMMENT_ENDPOINTS_TAG])
  55. @hapic.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
  56. @hapic.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
  57. @hapic.handle_exception(WorkspaceNotFound, HTTPStatus.FORBIDDEN)
  58. @hapic.handle_exception(AuthenticationFailed, HTTPStatus.BAD_REQUEST)
  59. @hapic.input_path(WorkspaceAndContentIdPathSchema())
  60. @hapic.input_body(SetCommentSchema())
  61. @hapic.output_body(CommentSchema(),)
  62. def add_comment(self, context, request: TracimRequest, hapic_data=None):
  63. """
  64. Add new comment
  65. """
  66. # login = hapic_data.body
  67. app_config = request.registry.settings['CFG']
  68. api = ContentApi(
  69. current_user=request.current_user,
  70. session=request.dbsession,
  71. config=app_config,
  72. )
  73. content = api.get_one(
  74. hapic_data.path.content_id,
  75. content_type=ContentType.Any
  76. )
  77. comment = api.create_comment(
  78. content.workspace,
  79. content,
  80. hapic_data.body.raw_content,
  81. do_save=True,
  82. )
  83. return api.get_content_in_context(comment)
  84. @hapic.with_api_doc(tags=[COMMENT_ENDPOINTS_TAG])
  85. @hapic.handle_exception(NotAuthenticated, HTTPStatus.UNAUTHORIZED)
  86. @hapic.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
  87. @hapic.handle_exception(WorkspaceNotFound, HTTPStatus.FORBIDDEN)
  88. @hapic.handle_exception(AuthenticationFailed, HTTPStatus.BAD_REQUEST)
  89. @hapic.input_path(CommentsPathSchema())
  90. @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.NO_CONTENT) # nopep8
  91. def delete_comment(self, context, request: TracimRequest, hapic_data=None):
  92. """
  93. Delete comment
  94. """
  95. app_config = request.registry.settings['CFG']
  96. api = ContentApi(
  97. current_user=request.current_user,
  98. session=request.dbsession,
  99. config=app_config,
  100. )
  101. wapi = WorkspaceApi(
  102. current_user=request.current_user,
  103. session=request.dbsession,
  104. config=app_config,
  105. )
  106. workspace = wapi.get_one(hapic_data.path.workspace_id)
  107. parent = api.get_one(
  108. hapic_data.path.content_id,
  109. content_type=ContentType.Any,
  110. workspace=workspace
  111. )
  112. comment = api.get_one(
  113. hapic_data.path.comment_id,
  114. content_type=ContentType.Comment,
  115. workspace=workspace,
  116. parent=parent,
  117. )
  118. with new_revision(
  119. session=request.dbsession,
  120. tm=transaction.manager,
  121. content=comment
  122. ):
  123. api.delete(comment)
  124. return
  125. def bind(self, configurator: Configurator):
  126. # Get comments
  127. configurator.add_route(
  128. 'content_comments',
  129. '/workspaces/{workspace_id}/contents/{content_id}/comments',
  130. request_method='GET'
  131. )
  132. configurator.add_view(self.content_comments, route_name='content_comments')
  133. # Add comments
  134. configurator.add_route(
  135. 'add_comment',
  136. '/workspaces/{workspace_id}/contents/{content_id}/comments',
  137. request_method='POST'
  138. ) # nopep8
  139. configurator.add_view(self.add_comment, route_name='add_comment')
  140. # delete comments
  141. configurator.add_route(
  142. 'delete_comment',
  143. '/workspaces/{workspace_id}/contents/{content_id}/comments/{comment_id}', # nopep8
  144. request_method='DELETE'
  145. )
  146. configurator.add_view(self.delete_comment, route_name='delete_comment')