Browse Source

add generic endpoint to get content

Guénaël Muller 6 years ago
parent
commit
9f95a4f262
1 changed files with 39 additions and 2 deletions
  1. 39 2
      backend/tracim_backend/views/core_api/workspace_controller.py

+ 39 - 2
backend/tracim_backend/views/core_api/workspace_controller.py View File

34
 from tracim_backend.exceptions import ParentNotFound
34
 from tracim_backend.exceptions import ParentNotFound
35
 from tracim_backend.views.controllers import Controller
35
 from tracim_backend.views.controllers import Controller
36
 from tracim_backend.views.core_api.schemas import FilterContentQuerySchema
36
 from tracim_backend.views.core_api.schemas import FilterContentQuerySchema
37
+from tracim_backend.views.core_api.schemas import ContentIdPathSchema
37
 from tracim_backend.views.core_api.schemas import WorkspaceMemberCreationSchema
38
 from tracim_backend.views.core_api.schemas import WorkspaceMemberCreationSchema
38
 from tracim_backend.views.core_api.schemas import WorkspaceMemberInviteSchema
39
 from tracim_backend.views.core_api.schemas import WorkspaceMemberInviteSchema
39
 from tracim_backend.views.core_api.schemas import RoleUpdateSchema
40
 from tracim_backend.views.core_api.schemas import RoleUpdateSchema
312
     @require_workspace_role(UserRoleInWorkspace.READER)
313
     @require_workspace_role(UserRoleInWorkspace.READER)
313
     @hapic.input_path(WorkspaceAndContentIdPathSchema())
314
     @hapic.input_path(WorkspaceAndContentIdPathSchema())
314
     @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.FOUND)  # nopep8
315
     @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.FOUND)  # nopep8
315
-    def get_content(
316
+    def get_content_from_workspace(
316
             self,
317
             self,
317
             context,
318
             context,
318
             request: TracimRequest,
319
             request: TracimRequest,
335
         )
336
         )
336
 
337
 
337
     @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
338
     @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
339
+    @hapic.input_path(ContentIdPathSchema())
340
+    @hapic.output_body(NoContentSchema(), default_http_code=HTTPStatus.FOUND)  # nopep8
341
+    def get_content(
342
+            self,
343
+            context,
344
+            request: TracimRequest,
345
+            hapic_data=None,
346
+    ) -> None:
347
+        """
348
+        redirect to correct content file endpoint
349
+        """
350
+        app_config = request.registry.settings['CFG']
351
+        api = ContentApi(
352
+            current_user=request.current_user,
353
+            session=request.dbsession,
354
+            config=app_config,
355
+        )
356
+        content = api.get_one(
357
+            content_id=hapic_data.path['content_id'],
358
+            content_type=CONTENT_TYPES.Any_SLUG
359
+        )
360
+        content_type = CONTENT_TYPES.get_one_by_slug(content.type).slug
361
+        # TODO - G.M - 2018-08-03 - Jsonify redirect response ?
362
+        raise HTTPFound(
363
+            "{base_url}workspaces/{workspace_id}/{content_type}s/{content_id}".format(
364
+                base_url=BASE_API_V2,
365
+                workspace_id=content.workspace_id,
366
+                content_type=content_type,
367
+                content_id=content.content_id,
368
+            )
369
+        )
370
+
371
+    @hapic.with_api_doc(tags=[SWAGGER_TAG_WORKSPACE_ENDPOINTS])
338
     @hapic.handle_exception(WorkspacesDoNotMatch, HTTPStatus.BAD_REQUEST)
372
     @hapic.handle_exception(WorkspacesDoNotMatch, HTTPStatus.BAD_REQUEST)
339
     @require_workspace_role(UserRoleInWorkspace.CONTENT_MANAGER)
373
     @require_workspace_role(UserRoleInWorkspace.CONTENT_MANAGER)
340
     @require_candidate_workspace_role(UserRoleInWorkspace.CONTENT_MANAGER)
374
     @require_candidate_workspace_role(UserRoleInWorkspace.CONTENT_MANAGER)
544
         configurator.add_route('create_generic_content', '/workspaces/{workspace_id}/contents', request_method='POST')  # nopep8
578
         configurator.add_route('create_generic_content', '/workspaces/{workspace_id}/contents', request_method='POST')  # nopep8
545
         configurator.add_view(self.create_generic_empty_content, route_name='create_generic_content')  # nopep8
579
         configurator.add_view(self.create_generic_empty_content, route_name='create_generic_content')  # nopep8
546
         # Get Content
580
         # Get Content
547
-        configurator.add_route('get_content', '/workspaces/{workspace_id}/contents/{content_id}', request_method='GET')  # nopep8
581
+        configurator.add_route('get_content', '/contents/{content_id}', request_method='GET')  # nopep8
548
         configurator.add_view(self.get_content, route_name='get_content')
582
         configurator.add_view(self.get_content, route_name='get_content')
583
+        # Get Content From workspace
584
+        configurator.add_route('get_content_from_workspace', '/workspaces/{workspace_id}/contents/{content_id}', request_method='GET')  # nopep8
585
+        configurator.add_view(self.get_content_from_workspace, route_name='get_content_from_workspace')  # nopep8
549
         # Move Content
586
         # Move Content
550
         configurator.add_route('move_content', '/workspaces/{workspace_id}/contents/{content_id}/move', request_method='PUT')  # nopep8
587
         configurator.add_route('move_content', '/workspaces/{workspace_id}/contents/{content_id}/move', request_method='PUT')  # nopep8
551
         configurator.add_view(self.move_content, route_name='move_content')  # nopep8
588
         configurator.add_view(self.move_content, route_name='move_content')  # nopep8