This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.py
406 lines (378 loc) · 15.1 KB
/
request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# -*- coding: utf-8 -*-
from pyramid.request import Request
from pyramid.response import Response
from sqlalchemy.orm.exc import NoResultFound
from tracim_backend.exceptions import NotAuthenticated
from tracim_backend.exceptions import UserNotActive
from tracim_backend.exceptions import ContentNotFound
from tracim_backend.exceptions import InvalidUserId
from tracim_backend.exceptions import InvalidWorkspaceId
from tracim_backend.exceptions import InvalidContentId
from tracim_backend.exceptions import InvalidCommentId
from tracim_backend.exceptions import ContentNotFoundInTracimRequest
from tracim_backend.exceptions import WorkspaceNotFoundInTracimRequest
from tracim_backend.exceptions import UserNotFoundInTracimRequest
from tracim_backend.exceptions import UserDoesNotExist
from tracim_backend.exceptions import WorkspaceNotFound
from tracim_backend.exceptions import ImmutableAttribute
from tracim_backend.app_models.contents import CONTENT_TYPES
from tracim_backend.lib.core.content import ContentApi
from tracim_backend.lib.core.user import UserApi
from tracim_backend.lib.core.workspace import WorkspaceApi
from tracim_backend.lib.utils.authorization import JSONDecodeError
from tracim_backend.models import User
from tracim_backend.models.data import Workspace
from tracim_backend.models.data import Content
class TracimRequest(Request):
"""
Request with tracim specific params/methods
"""
def __init__(
self,
environ,
charset=None,
unicode_errors=None,
decode_param_names=None,
**kw
):
super().__init__(
environ,
charset,
unicode_errors,
decode_param_names,
**kw
)
# Current comment, found in request path
self._current_comment = None # type: Content
# Current content, found in request path
self._current_content = None # type: Content
# Current workspace, found in request path
self._current_workspace = None # type: Workspace
# Candidate workspace found in request body
self._candidate_workspace = None # type: Workspace
# Authenticated user
self._current_user = None # type: User
# User found from request headers, content, distinct from authenticated
# user
self._candidate_user = None # type: User
# INFO - G.M - 18-05-2018 - Close db at the end of the request
self.add_finished_callback(self._cleanup)
@property
def current_workspace(self) -> Workspace:
"""
Get current workspace of the request according to authentification and
request headers (to retrieve workspace). Setted by default value the
first time if not configured.
:return: Workspace of the request
"""
if self._current_workspace is None:
self._current_workspace = self._get_current_workspace(self.current_user, self) # nopep8
return self._current_workspace
@current_workspace.setter
def current_workspace(self, workspace: Workspace) -> None:
"""
Setting current_workspace
:param workspace:
:return:
"""
if self._current_workspace is not None:
raise ImmutableAttribute(
"Can't modify already setted current_workspace"
)
self._current_workspace = workspace
@property
def current_user(self) -> User:
"""
Get user from authentication mecanism.
"""
if self._current_user is None:
self.current_user = self._get_auth_safe_user(self)
return self._current_user
@current_user.setter
def current_user(self, user: User) -> None:
if self._current_user is not None:
raise ImmutableAttribute(
"Can't modify already setted current_user"
)
self._current_user = user
@property
def current_content(self) -> Content:
"""
Get current content from path
"""
if self._current_content is None:
self._current_content = self._get_current_content(
self.current_user,
self.current_workspace,
self
)
return self._current_content
@current_content.setter
def current_content(self, content: Content) -> None:
if self._current_content is not None:
raise ImmutableAttribute(
"Can't modify already setted current_content"
)
self._current_content = content
@property
def current_comment(self) -> Content:
"""
Get current comment from path
"""
if self._current_comment is None:
self._current_comment = self._get_current_comment(
self.current_user,
self.current_workspace,
self.current_content,
self
)
return self._current_comment
@current_comment.setter
def current_comment(self, content: Content) -> None:
if self._current_comment is not None:
raise ImmutableAttribute(
"Can't modify already setted current_content"
)
self._current_comment = content
# TODO - G.M - 24-05-2018 - Find a better naming for this ?
@property
def candidate_user(self) -> User:
"""
Get user from headers/body request. This user is not
the one found by authentication mecanism. This user
can help user to know about who one page is about in
a similar way as current_workspace.
"""
if self._candidate_user is None:
self.candidate_user = self._get_candidate_user(self)
return self._candidate_user
@property
def candidate_workspace(self) -> Workspace:
"""
Get workspace from headers/body request. This workspace is not
the one found from path. Its the one from json body.
"""
if self._candidate_workspace is None:
self._candidate_workspace = self._get_candidate_workspace(
self.current_user,
self
)
return self._candidate_workspace
def _cleanup(self, request: 'TracimRequest') -> None:
"""
Close dbsession at the end of the request in order to avoid exception
about not properly closed session or "object created in another thread"
issue
see https://github.com/tracim/tracim_backend/issues/62
:param request: same as self, request
:return: nothing.
"""
self._current_user = None
self._current_workspace = None
self.dbsession.close()
@candidate_user.setter
def candidate_user(self, user: User) -> None:
if self._candidate_user is not None:
raise ImmutableAttribute(
"Can't modify already setted candidate_user"
)
self._candidate_user = user
###
# Utils for TracimRequest
###
def _get_current_comment(
self,
user: User,
workspace: Workspace,
content: Content,
request: 'TracimRequest'
) -> Content:
"""
Get current content from request
:param user: User who want to check the workspace
:param workspace: Workspace of the content
:param content: comment is related to this content
:param request: pyramid request
:return: current content
"""
comment_id = ''
try:
if 'comment_id' in request.matchdict:
comment_id_str = request.matchdict['content_id']
if not isinstance(comment_id_str, str) or not comment_id_str.isdecimal(): # nopep8
raise InvalidCommentId('comment_id is not a correct integer') # nopep8
comment_id = int(request.matchdict['comment_id'])
if not comment_id:
raise ContentNotFoundInTracimRequest('No comment_id property found in request') # nopep8
api = ContentApi(
current_user=user,
session=request.dbsession,
show_deleted=True,
show_archived=True,
config=request.registry.settings['CFG']
)
comment = api.get_one(
comment_id,
content_type=CONTENT_TYPES.Comment.slug,
workspace=workspace,
parent=content,
)
except NoResultFound as exc:
raise ContentNotFound(
'Comment {} does not exist '
'or is not visible for this user'.format(comment_id)
) from exc
return comment
def _get_current_content(
self,
user: User,
workspace: Workspace,
request: 'TracimRequest'
) -> Content:
"""
Get current content from request
:param user: User who want to check the workspace
:param workspace: Workspace of the content
:param request: pyramid request
:return: current content
"""
content_id = ''
try:
if 'content_id' in request.matchdict:
content_id_str = request.matchdict['content_id']
if not isinstance(content_id_str, str) or not content_id_str.isdecimal(): # nopep8
raise InvalidContentId('content_id is not a correct integer') # nopep8
content_id = int(request.matchdict['content_id'])
if not content_id:
raise ContentNotFoundInTracimRequest('No content_id property found in request') # nopep8
api = ContentApi(
current_user=user,
show_deleted=True,
show_archived=True,
session=request.dbsession,
config=request.registry.settings['CFG']
)
content = api.get_one(content_id=content_id, workspace=workspace, content_type=CONTENT_TYPES.Any_SLUG) # nopep8
except NoResultFound as exc:
raise ContentNotFound(
'Content {} does not exist '
'or is not visible for this user'.format(content_id)
) from exc
return content
def _get_candidate_user(
self,
request: 'TracimRequest',
) -> User:
"""
Get candidate user
:param request: pyramid request
:return: user found from header/body
"""
app_config = request.registry.settings['CFG']
uapi = UserApi(None, show_deleted=True, session=request.dbsession, config=app_config)
login = ''
try:
login = None
if 'user_id' in request.matchdict:
user_id_str = request.matchdict['user_id']
if not isinstance(user_id_str, str) or not user_id_str.isdecimal():
raise InvalidUserId('user_id is not a correct integer') # nopep8
login = int(request.matchdict['user_id'])
if not login:
raise UserNotFoundInTracimRequest('You request a candidate user but the context not permit to found one') # nopep8
user = uapi.get_one(login)
except UserNotFoundInTracimRequest as exc:
raise UserDoesNotExist('User {} not found'.format(login)) from exc
return user
def _get_auth_safe_user(
self,
request: 'TracimRequest',
) -> User:
"""
Get current pyramid authenticated user from request
:param request: pyramid request
:return: current authenticated user
"""
app_config = request.registry.settings['CFG']
uapi = UserApi(None, session=request.dbsession, config=app_config)
login = ''
try:
login = request.authenticated_userid
if not login:
raise UserNotFoundInTracimRequest('You request a current user but the context not permit to found one') # nopep8
user = uapi.get_one(login)
if not user.is_active:
raise UserNotActive('User {} is not active'.format(login))
except (UserDoesNotExist, UserNotFoundInTracimRequest) as exc:
raise NotAuthenticated('User {} not found'.format(login)) from exc
return user
def _get_current_workspace(
self,
user: User,
request: 'TracimRequest'
) -> Workspace:
"""
Get current workspace from request
:param user: User who want to check the workspace
:param request: pyramid request
:return: current workspace
"""
workspace_id = ''
try:
if 'workspace_id' in request.matchdict:
workspace_id_str = request.matchdict['workspace_id']
if not isinstance(workspace_id_str, str) or not workspace_id_str.isdecimal(): # nopep8
raise InvalidWorkspaceId('workspace_id is not a correct integer') # nopep8
workspace_id = int(request.matchdict['workspace_id'])
if not workspace_id:
raise WorkspaceNotFoundInTracimRequest('No workspace_id property found in request') # nopep8
wapi = WorkspaceApi(
current_user=user,
session=request.dbsession,
config=request.registry.settings['CFG'],
show_deleted=True,
)
workspace = wapi.get_one(workspace_id)
except NoResultFound as exc:
raise WorkspaceNotFound(
'Workspace {} does not exist '
'or is not visible for this user'.format(workspace_id)
) from exc
return workspace
def _get_candidate_workspace(
self,
user: User,
request: 'TracimRequest'
) -> Workspace:
"""
Get current workspace from request
:param user: User who want to check the workspace
:param request: pyramid request
:return: current workspace
"""
workspace_id = ''
try:
if 'new_workspace_id' in request.json_body:
workspace_id = request.json_body['new_workspace_id']
if not isinstance(workspace_id, int):
if workspace_id.isdecimal():
workspace_id = int(workspace_id)
else:
raise InvalidWorkspaceId('workspace_id is not a correct integer') # nopep8
if not workspace_id:
raise WorkspaceNotFoundInTracimRequest('No new_workspace_id property found in body') # nopep8
wapi = WorkspaceApi(
current_user=user,
session=request.dbsession,
config=request.registry.settings['CFG'],
show_deleted=True,
)
workspace = wapi.get_one(workspace_id)
except JSONDecodeError as exc:
raise WorkspaceNotFound('Invalid JSON content') from exc
except NoResultFound as exc:
raise WorkspaceNotFound(
'Workspace {} does not exist '
'or is not visible for this user'.format(workspace_id)
) from exc
return workspace