Browse Source

add endpoint for new_workspace_and_update_workspace

Guénaël Muller 6 years ago
parent
commit
8e1dcd2439

+ 23 - 0
tracim/lib/core/workspace.py View File

105
 
105
 
106
         return workspace
106
         return workspace
107
 
107
 
108
+    def update_workspace(
109
+            self,
110
+            workspace: Workspace,
111
+            label: str,
112
+            description: str,
113
+            save_now: bool=False,
114
+    ) -> Workspace:
115
+        """
116
+        Update workspace
117
+        :param workspace: workspace to update
118
+        :param label: new label of workspace
119
+        :param description: new description
120
+        :param save_now: database flush
121
+        :return: updated workspace
122
+        """
123
+        workspace.label = label
124
+        workspace.description = description
125
+
126
+        if save_now:
127
+            self.save(workspace)
128
+
129
+        return workspace
130
+
108
     def get_one(self, id):
131
     def get_one(self, id):
109
         return self._base_query().filter(Workspace.workspace_id == id).one()
132
         return self._base_query().filter(Workspace.workspace_id == id).one()
110
 
133
 

+ 13 - 0
tracim/models/context_models.py View File

76
         self.show_active = bool(show_active)
76
         self.show_active = bool(show_active)
77
 
77
 
78
 
78
 
79
+class WorkspaceUpdate(object):
80
+    """
81
+    Update workspace
82
+    """
83
+    def __init__(
84
+        self,
85
+        label: str,
86
+        description: str,
87
+    ):
88
+        self.label = label
89
+        self.description = description
90
+
91
+
79
 class ContentCreation(object):
92
 class ContentCreation(object):
80
     """
93
     """
81
     Content creation model
94
     Content creation model

+ 85 - 0
tracim/tests/functional/test_workspaces.py View File

83
         assert sidebar_entry['hexcolor'] == "#757575"
83
         assert sidebar_entry['hexcolor'] == "#757575"
84
         assert sidebar_entry['fa_icon'] == "calendar"
84
         assert sidebar_entry['fa_icon'] == "calendar"
85
 
85
 
86
+    def test_api__update_workspace__ok_200__nominal_case(self) -> None:
87
+        """
88
+        Test update workspace
89
+        """
90
+        self.testapp.authorization = (
91
+            'Basic',
92
+            (
93
+                'admin@admin.admin',
94
+                'admin@admin.admin'
95
+            )
96
+        )
97
+        params = {
98
+            'label': 'superworkspace',
99
+            'description': 'mysuperdescription'
100
+        }
101
+        # Before
102
+        res = self.testapp.get(
103
+            '/api/v2/workspaces/1',
104
+            status=200
105
+        )
106
+        assert res.json_body
107
+        workspace = res.json_body
108
+        assert workspace['workspace_id'] == 1
109
+        assert workspace['slug'] == 'business'
110
+        assert workspace['label'] == 'Business'
111
+        assert workspace['description'] == 'All importants documents'
112
+        assert len(workspace['sidebar_entries']) == 7
113
+
114
+        # modify workspace
115
+        res = self.testapp.put_json(
116
+            '/api/v2/workspaces/1',
117
+            status=200,
118
+            params=params,
119
+        )
120
+        assert res.json_body
121
+        workspace = res.json_body
122
+        assert workspace['workspace_id'] == 1
123
+        assert workspace['slug'] == 'superworkspace'
124
+        assert workspace['label'] == 'superworkspace'
125
+        assert workspace['description'] == 'mysuperdescription'
126
+        assert len(workspace['sidebar_entries']) == 7
127
+
128
+        # after
129
+        res = self.testapp.get(
130
+            '/api/v2/workspaces/1',
131
+            status=200
132
+        )
133
+        assert res.json_body
134
+        workspace = res.json_body
135
+        assert workspace['workspace_id'] == 1
136
+        assert workspace['slug'] == 'superworkspace'
137
+        assert workspace['label'] == 'superworkspace'
138
+        assert workspace['description'] == 'mysuperdescription'
139
+        assert len(workspace['sidebar_entries']) == 7
140
+
141
+    def test_api__create_workspace__ok_200__nominal_case(self) -> None:
142
+        """
143
+        Test create workspace
144
+        """
145
+        self.testapp.authorization = (
146
+            'Basic',
147
+            (
148
+                'admin@admin.admin',
149
+                'admin@admin.admin'
150
+            )
151
+        )
152
+        params = {
153
+            'label': 'superworkspace',
154
+            'description': 'mysuperdescription'
155
+        }
156
+        res = self.testapp.post_json(
157
+            '/api/v2/workspaces',
158
+            status=200,
159
+            params=params,
160
+        )
161
+        assert res.json_body
162
+        workspace = res.json_body
163
+        workspace_id = res.json_body['workspace_id']
164
+        res = self.testapp.get(
165
+            '/api/v2/workspaces/{}'.format(workspace_id),
166
+            status=200
167
+        )
168
+        workspace_2 = res.json_body
169
+        assert workspace == workspace_2
170
+
86
     def test_api__get_workspace__err_400__unallowed_user(self) -> None:
171
     def test_api__get_workspace__err_400__unallowed_user(self) -> None:
87
         """
172
         """
88
         Check obtain workspace unreachable for user
173
         Check obtain workspace unreachable for user

+ 18 - 0
tracim/views/core_api/schemas.py View File

10
 from tracim.models.contents import ContentTypeLegacy as ContentType
10
 from tracim.models.contents import ContentTypeLegacy as ContentType
11
 from tracim.models.contents import ContentStatusLegacy as ContentStatus
11
 from tracim.models.contents import ContentStatusLegacy as ContentStatus
12
 from tracim.models.context_models import ContentCreation
12
 from tracim.models.context_models import ContentCreation
13
+from tracim.models.context_models import WorkspaceUpdate
13
 from tracim.models.context_models import CommentCreation
14
 from tracim.models.context_models import CommentCreation
14
 from tracim.models.context_models import TextBasedContentUpdate
15
 from tracim.models.context_models import TextBasedContentUpdate
15
 from tracim.models.context_models import SetContentStatus
16
 from tracim.models.context_models import SetContentStatus
173
     expire_after = marshmallow.fields.String()
174
     expire_after = marshmallow.fields.String()
174
 
175
 
175
 
176
 
177
+class WorkspaceModifySchema(marshmallow.Schema):
178
+    label = marshmallow.fields.String(
179
+        example='My Workspace',
180
+    )
181
+    description = marshmallow.fields.String(
182
+        example='A super description of my workspace.',
183
+    )
184
+
185
+    @post_load
186
+    def make_workspace_modifications(self, data):
187
+        return WorkspaceUpdate(**data)
188
+
189
+
190
+class WorkspaceCreationSchema(WorkspaceModifySchema):
191
+    pass
192
+
193
+
176
 class NoContentSchema(marshmallow.Schema):
194
 class NoContentSchema(marshmallow.Schema):
177
 
195
 
178
     class Meta:
196
     class Meta:

+ 54 - 1
tracim/views/core_api/workspace_controller.py View File

12
 from tracim.lib.core.content import ContentApi
12
 from tracim.lib.core.content import ContentApi
13
 from tracim.lib.core.userworkspace import RoleApi
13
 from tracim.lib.core.userworkspace import RoleApi
14
 from tracim.lib.utils.authorization import require_workspace_role
14
 from tracim.lib.utils.authorization import require_workspace_role
15
+from tracim.lib.utils.authorization import require_profile
16
+from tracim.models import Group
15
 from tracim.lib.utils.authorization import require_candidate_workspace_role
17
 from tracim.lib.utils.authorization import require_candidate_workspace_role
16
 from tracim.models.data import UserRoleInWorkspace
18
 from tracim.models.data import UserRoleInWorkspace
17
 from tracim.models.data import ActionDescription
19
 from tracim.models.data import ActionDescription
21
 from tracim.exceptions import WorkspacesDoNotMatch
23
 from tracim.exceptions import WorkspacesDoNotMatch
22
 from tracim.views.controllers import Controller
24
 from tracim.views.controllers import Controller
23
 from tracim.views.core_api.schemas import FilterContentQuerySchema
25
 from tracim.views.core_api.schemas import FilterContentQuerySchema
26
+from tracim.views.core_api.schemas import WorkspaceCreationSchema
27
+from tracim.views.core_api.schemas import WorkspaceModifySchema
24
 from tracim.views.core_api.schemas import ContentMoveSchema
28
 from tracim.views.core_api.schemas import ContentMoveSchema
25
 from tracim.views.core_api.schemas import NoContentSchema
29
 from tracim.views.core_api.schemas import NoContentSchema
26
 from tracim.views.core_api.schemas import ContentCreationSchema
30
 from tracim.views.core_api.schemas import ContentCreationSchema
45
         """
49
         """
46
         Get workspace informations
50
         Get workspace informations
47
         """
51
         """
48
-        wid = hapic_data.path['workspace_id']
49
         app_config = request.registry.settings['CFG']
52
         app_config = request.registry.settings['CFG']
50
         wapi = WorkspaceApi(
53
         wapi = WorkspaceApi(
51
             current_user=request.current_user,  # User
54
             current_user=request.current_user,  # User
55
         return wapi.get_workspace_with_context(request.current_workspace)
58
         return wapi.get_workspace_with_context(request.current_workspace)
56
 
59
 
57
     @hapic.with_api_doc(tags=[WORKSPACE_ENDPOINTS_TAG])
60
     @hapic.with_api_doc(tags=[WORKSPACE_ENDPOINTS_TAG])
61
+    @require_workspace_role(UserRoleInWorkspace.WORKSPACE_MANAGER)
62
+    @hapic.input_path(WorkspaceIdPathSchema())
63
+    @hapic.input_body(WorkspaceModifySchema())
64
+    @hapic.output_body(WorkspaceSchema())
65
+    def update_workspace(self, context, request: TracimRequest, hapic_data=None):  # nopep8
66
+        """
67
+        Update workspace informations
68
+        """
69
+        app_config = request.registry.settings['CFG']
70
+        wapi = WorkspaceApi(
71
+            current_user=request.current_user,  # User
72
+            session=request.dbsession,
73
+            config=app_config,
74
+        )
75
+        wapi.update_workspace(
76
+            request.current_workspace,
77
+            label=hapic_data.body.label,
78
+            description=hapic_data.body.description,
79
+            save_now=True,
80
+        )
81
+        return wapi.get_workspace_with_context(request.current_workspace)
82
+
83
+    @hapic.with_api_doc(tags=[WORKSPACE_ENDPOINTS_TAG])
84
+    @require_profile(Group.TIM_MANAGER)
85
+    @hapic.input_body(WorkspaceCreationSchema())
86
+    @hapic.output_body(WorkspaceSchema())
87
+    def create_workspace(self, context, request: TracimRequest, hapic_data=None):  # nopep8
88
+        """
89
+        create workspace
90
+        """
91
+        app_config = request.registry.settings['CFG']
92
+        wapi = WorkspaceApi(
93
+            current_user=request.current_user,  # User
94
+            session=request.dbsession,
95
+            config=app_config,
96
+        )
97
+        workspace = wapi.create_workspace(
98
+            label=hapic_data.body.label,
99
+            description=hapic_data.body.description,
100
+            save_now=True,
101
+        )
102
+        return wapi.get_workspace_with_context(workspace)
103
+
104
+    @hapic.with_api_doc(tags=[WORKSPACE_ENDPOINTS_TAG])
58
     @require_workspace_role(UserRoleInWorkspace.READER)
105
     @require_workspace_role(UserRoleInWorkspace.READER)
59
     @hapic.input_path(WorkspaceIdPathSchema())
106
     @hapic.input_path(WorkspaceIdPathSchema())
60
     @hapic.output_body(WorkspaceMemberSchema(many=True))
107
     @hapic.output_body(WorkspaceMemberSchema(many=True))
332
         # Workspace
379
         # Workspace
333
         configurator.add_route('workspace', '/workspaces/{workspace_id}', request_method='GET')  # nopep8
380
         configurator.add_route('workspace', '/workspaces/{workspace_id}', request_method='GET')  # nopep8
334
         configurator.add_view(self.workspace, route_name='workspace')
381
         configurator.add_view(self.workspace, route_name='workspace')
382
+        # Create workspace
383
+        configurator.add_route('create_workspace', '/workspaces', request_method='POST')  # nopep8
384
+        configurator.add_view(self.create_workspace, route_name='create_workspace')  # nopep8
385
+        # Update Workspace
386
+        configurator.add_route('update_workspace', '/workspaces/{workspace_id}', request_method='PUT')  # nopep8
387
+        configurator.add_view(self.update_workspace, route_name='update_workspace')  # nopep8
335
         # Workspace Members (Roles)
388
         # Workspace Members (Roles)
336
         configurator.add_route('workspace_members', '/workspaces/{workspace_id}/members', request_method='GET')  # nopep8
389
         configurator.add_route('workspace_members', '/workspaces/{workspace_id}/members', request_method='GET')  # nopep8
337
         configurator.add_view(self.workspaces_members, route_name='workspace_members')  # nopep8
390
         configurator.add_view(self.workspaces_members, route_name='workspace_members')  # nopep8