Browse Source

Merge pull request #14 from tracim/feature/599_endpoint_redirection_for_content

Bastien Sevajol 6 years ago
parent
commit
1a72a86feb
No account linked to committer's email
1 changed files with 68 additions and 0 deletions
  1. 68 0
      backend/tracim_backend/views/core_api/workspace_controller.py

+ 68 - 0
backend/tracim_backend/views/core_api/workspace_controller.py View File

@@ -1,6 +1,7 @@
1 1
 import typing
2 2
 import transaction
3 3
 from pyramid.config import Configurator
4
+from pyramid.httpexceptions import HTTPFound
4 5
 
5 6
 from tracim_backend.lib.core.user import UserApi
6 7
 from tracim_backend.models.roles import WorkspaceRoles
@@ -11,6 +12,7 @@ except ImportError:
11 12
     from http import client as HTTPStatus
12 13
 
13 14
 from tracim_backend import hapic
15
+from tracim_backend import BASE_API_V2
14 16
 from tracim_backend import TracimRequest
15 17
 from tracim_backend.lib.core.workspace import WorkspaceApi
16 18
 from tracim_backend.lib.core.content import ContentApi
@@ -34,6 +36,7 @@ from tracim_backend.exceptions import ParentNotFound
34 36
 from tracim_backend.views.controllers import Controller
35 37
 from tracim_backend.lib.utils.utils import password_generator
36 38
 from tracim_backend.views.core_api.schemas import FilterContentQuerySchema
39
+from tracim_backend.views.core_api.schemas import ContentIdPathSchema
37 40
 from tracim_backend.views.core_api.schemas import WorkspaceMemberCreationSchema
38 41
 from tracim_backend.views.core_api.schemas import WorkspaceMemberInviteSchema
39 42
 from tracim_backend.views.core_api.schemas import RoleUpdateSchema
@@ -316,6 +319,65 @@ class WorkspaceController(Controller):
316 319
         return content
317 320
 
318 321
     @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
322
+    @require_workspace_role(UserRoleInWorkspace.READER)
323
+    @hapic.input_path(WorkspaceAndContentIdPathSchema())
324
+    @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.FOUND)  # nopep8
325
+    def get_content_from_workspace(
326
+            self,
327
+            context,
328
+            request: TracimRequest,
329
+            hapic_data=None,
330
+    ) -> None:
331
+        """
332
+        redirect to correct content file endpoint
333
+        """
334
+        app_config = request.registry.settings['CFG']
335
+        content = request.current_content
336
+        content_type = CONTENT_TYPES.get_one_by_slug(content.type).slug
337
+        # TODO - G.M - 2018-08-03 - Jsonify redirect response ?
338
+        raise HTTPFound(
339
+            "{base_url}workspaces/{workspace_id}/{content_type}s/{content_id}".format(
340
+                base_url=BASE_API_V2,
341
+                workspace_id=content.workspace_id,
342
+                content_type=content_type,
343
+                content_id=content.content_id,
344
+            )
345
+        )
346
+
347
+    @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
348
+    @hapic.input_path(ContentIdPathSchema())
349
+    @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.FOUND)  # nopep8
350
+    def get_content(
351
+            self,
352
+            context,
353
+            request: TracimRequest,
354
+            hapic_data=None,
355
+    ) -> None:
356
+        """
357
+        redirect to correct content file endpoint
358
+        """
359
+        app_config = request.registry.settings['CFG']
360
+        api = ContentApi(
361
+            current_user=request.current_user,
362
+            session=request.dbsession,
363
+            config=app_config,
364
+        )
365
+        content = api.get_one(
366
+            content_id=hapic_data.path['content_id'],
367
+            content_type=CONTENT_TYPES.Any_SLUG
368
+        )
369
+        content_type = CONTENT_TYPES.get_one_by_slug(content.type).slug
370
+        # TODO - G.M - 2018-08-03 - Jsonify redirect response ?
371
+        raise HTTPFound(
372
+            "{base_url}workspaces/{workspace_id}/{content_type}s/{content_id}".format(
373
+                base_url=BASE_API_V2,
374
+                workspace_id=content.workspace_id,
375
+                content_type=content_type,
376
+                content_id=content.content_id,
377
+            )
378
+        )
379
+
380
+    @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
319 381
     @hapic.handle_exception(WorkspacesDoNotMatch, HTTPStatus.BAD_REQUEST)
320 382
     @require_workspace_role(UserRoleInWorkspace.CONTENT_MANAGER)
321 383
     @require_candidate_workspace_role(UserRoleInWorkspace.CONTENT_MANAGER)
@@ -532,6 +594,12 @@ class WorkspaceController(Controller):
532 594
         # Create Generic Content
533 595
         configurator.add_route('create_generic_content', '/workspaces/{workspace_id}/contents', request_method='POST')  # nopep8
534 596
         configurator.add_view(self.create_generic_empty_content, route_name='create_generic_content')  # nopep8
597
+        # Get Content
598
+        configurator.add_route('get_content', '/contents/{content_id}', request_method='GET')  # nopep8
599
+        configurator.add_view(self.get_content, route_name='get_content')
600
+        # Get Content From workspace
601
+        configurator.add_route('get_content_from_workspace', '/workspaces/{workspace_id}/contents/{content_id}', request_method='GET')  # nopep8
602
+        configurator.add_view(self.get_content_from_workspace, route_name='get_content_from_workspace')  # nopep8
535 603
         # Move Content
536 604
         configurator.add_route('move_content', '/workspaces/{workspace_id}/contents/{content_id}/move', request_method='PUT')  # nopep8
537 605
         configurator.add_view(self.move_content, route_name='move_content')  # nopep8