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