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,6 +105,29 @@ class WorkspaceApi(object):
105 105
 
106 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 131
     def get_one(self, id):
109 132
         return self._base_query().filter(Workspace.workspace_id == id).one()
110 133
 

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

@@ -76,6 +76,19 @@ class ContentFilter(object):
76 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 92
 class ContentCreation(object):
80 93
     """
81 94
     Content creation model

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

@@ -83,6 +83,91 @@ class TestWorkspaceEndpoint(FunctionalTest):
83 83
         assert sidebar_entry['hexcolor'] == "#757575"
84 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 171
     def test_api__get_workspace__err_400__unallowed_user(self) -> None:
87 172
         """
88 173
         Check obtain workspace unreachable for user

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

@@ -10,6 +10,7 @@ from tracim.models.contents import open_status
10 10
 from tracim.models.contents import ContentTypeLegacy as ContentType
11 11
 from tracim.models.contents import ContentStatusLegacy as ContentStatus
12 12
 from tracim.models.context_models import ContentCreation
13
+from tracim.models.context_models import WorkspaceUpdate
13 14
 from tracim.models.context_models import CommentCreation
14 15
 from tracim.models.context_models import TextBasedContentUpdate
15 16
 from tracim.models.context_models import SetContentStatus
@@ -173,6 +174,23 @@ class LoginOutputHeaders(marshmallow.Schema):
173 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 194
 class NoContentSchema(marshmallow.Schema):
177 195
 
178 196
     class Meta:

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

@@ -12,6 +12,8 @@ from tracim.lib.core.workspace import WorkspaceApi
12 12
 from tracim.lib.core.content import ContentApi
13 13
 from tracim.lib.core.userworkspace import RoleApi
14 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 17
 from tracim.lib.utils.authorization import require_candidate_workspace_role
16 18
 from tracim.models.data import UserRoleInWorkspace
17 19
 from tracim.models.data import ActionDescription
@@ -21,6 +23,8 @@ from tracim.exceptions import EmptyLabelNotAllowed
21 23
 from tracim.exceptions import WorkspacesDoNotMatch
22 24
 from tracim.views.controllers import Controller
23 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 28
 from tracim.views.core_api.schemas import ContentMoveSchema
25 29
 from tracim.views.core_api.schemas import NoContentSchema
26 30
 from tracim.views.core_api.schemas import ContentCreationSchema
@@ -45,7 +49,6 @@ class WorkspaceController(Controller):
45 49
         """
46 50
         Get workspace informations
47 51
         """
48
-        wid = hapic_data.path['workspace_id']
49 52
         app_config = request.registry.settings['CFG']
50 53
         wapi = WorkspaceApi(
51 54
             current_user=request.current_user,  # User
@@ -55,6 +58,50 @@ class WorkspaceController(Controller):
55 58
         return wapi.get_workspace_with_context(request.current_workspace)
56 59
 
57 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 105
     @require_workspace_role(UserRoleInWorkspace.READER)
59 106
     @hapic.input_path(WorkspaceIdPathSchema())
60 107
     @hapic.output_body(WorkspaceMemberSchema(many=True))
@@ -332,6 +379,12 @@ class WorkspaceController(Controller):
332 379
         # Workspace
333 380
         configurator.add_route('workspace', '/workspaces/{workspace_id}', request_method='GET')  # nopep8
334 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 388
         # Workspace Members (Roles)
336 389
         configurator.add_route('workspace_members', '/workspaces/{workspace_id}/members', request_method='GET')  # nopep8
337 390
         configurator.add_view(self.workspaces_members, route_name='workspace_members')  # nopep8