Browse Source

Use current user in WorkspaceApi.get_all_manageable

Adrien Panay 7 years ago
parent
commit
e4c82cfc3f

+ 1 - 1
tracim/tracim/controllers/admin/workspace.py View File

159
     def get_all(self, *args, **kw):
159
     def get_all(self, *args, **kw):
160
         user = tmpl_context.current_user
160
         user = tmpl_context.current_user
161
         workspace_api_controller = WorkspaceApi(user)
161
         workspace_api_controller = WorkspaceApi(user)
162
-        workspaces = workspace_api_controller.get_all_manageable_for_user(user)
162
+        workspaces = workspace_api_controller.get_all_manageable()
163
         current_user_content = Context(CTX.CURRENT_USER).toDict(user)
163
         current_user_content = Context(CTX.CURRENT_USER).toDict(user)
164
         fake_api = Context(CTX.ADMIN_WORKSPACE) \
164
         fake_api = Context(CTX.ADMIN_WORKSPACE) \
165
             .toDict(
165
             .toDict(

+ 9 - 9
tracim/tracim/lib/workspace.py View File

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
-import transaction
2
+import typing
3
 
3
 
4
 from sqlalchemy.orm import Query
4
 from sqlalchemy.orm import Query
5
 from tg.i18n import ugettext as _
5
 from tg.i18n import ugettext as _
6
-from typing import List
6
+import transaction
7
 
7
 
8
 from tracim.lib.userworkspace import RoleApi
8
 from tracim.lib.userworkspace import RoleApi
9
+from tracim.model import DBSession
9
 from tracim.model.auth import Group
10
 from tracim.model.auth import Group
10
 from tracim.model.auth import User
11
 from tracim.model.auth import User
11
-from tracim.model.data import Workspace
12
 from tracim.model.data import UserRoleInWorkspace
12
 from tracim.model.data import UserRoleInWorkspace
13
-from tracim.model import DBSession
13
+from tracim.model.data import Workspace
14
 
14
 
15
 __author__ = 'damien'
15
 __author__ = 'damien'
16
 
16
 
102
         workspaces.sort(key=lambda workspace: workspace.label.lower())
102
         workspaces.sort(key=lambda workspace: workspace.label.lower())
103
         return workspaces
103
         return workspaces
104
 
104
 
105
-    def get_all_manageable_for_user(self, user: User) -> List[Workspace]:
106
-        """Get all workspaces the given user has manager rights on."""
107
-        workspaces = []
108
-        if user.profile.id == Group.TIM_ADMIN:
105
+    def get_all_manageable(self) -> typing.List[Workspace]:
106
+        """Get all workspaces the current user has manager rights on."""
107
+        workspaces = []  # type: typing.List[Workspace]
108
+        if self._user.profile.id == Group.TIM_ADMIN:
109
             workspaces = self._base_query().order_by(Workspace.label).all()
109
             workspaces = self._base_query().order_by(Workspace.label).all()
110
-        elif user.profile.id == Group.TIM_MANAGER:
110
+        elif self._user.profile.id == Group.TIM_MANAGER:
111
             workspaces = self._base_query() \
111
             workspaces = self._base_query() \
112
                 .filter(
112
                 .filter(
113
                     UserRoleInWorkspace.role ==
113
                     UserRoleInWorkspace.role ==

+ 5 - 5
tracim/tracim/tests/library/test_workspace.py View File

44
         u.is_active = False
44
         u.is_active = False
45
         eq_([], wapi.get_notifiable_roles(workspace=w))
45
         eq_([], wapi.get_notifiable_roles(workspace=w))
46
 
46
 
47
-    def test_unit__get_all_admin_for_user(self):
47
+    def test_unit__get_all_manageable(self):
48
         admin = DBSession.query(User) \
48
         admin = DBSession.query(User) \
49
             .filter(User.email == 'admin@admin.admin').one()
49
             .filter(User.email == 'admin@admin.admin').one()
50
         uapi = UserApi(admin)
50
         uapi = UserApi(admin)
51
         # Checks a case without workspaces.
51
         # Checks a case without workspaces.
52
         wapi = WorkspaceApi(current_user=admin)
52
         wapi = WorkspaceApi(current_user=admin)
53
-        eq_([], wapi.get_all_manageable_for_user(user=admin))
53
+        eq_([], wapi.get_all_manageable())
54
         # Checks an admin gets all workspaces.
54
         # Checks an admin gets all workspaces.
55
         w4 = wapi.create_workspace(label='w4')
55
         w4 = wapi.create_workspace(label='w4')
56
         w3 = wapi.create_workspace(label='w3')
56
         w3 = wapi.create_workspace(label='w3')
57
         w2 = wapi.create_workspace(label='w2')
57
         w2 = wapi.create_workspace(label='w2')
58
         w1 = wapi.create_workspace(label='w1')
58
         w1 = wapi.create_workspace(label='w1')
59
-        eq_([w1, w2, w3, w4], wapi.get_all_manageable_for_user(user=admin))
59
+        eq_([w1, w2, w3, w4], wapi.get_all_manageable())
60
         # Checks a regular user gets none workspace.
60
         # Checks a regular user gets none workspace.
61
         gapi = GroupApi(None)
61
         gapi = GroupApi(None)
62
         u = uapi.create_user('u.s@e.r', [gapi.get_one(Group.TIM_USER)], True)
62
         u = uapi.create_user('u.s@e.r', [gapi.get_one(Group.TIM_USER)], True)
67
         rapi.create_one(u, w3, UserRoleInWorkspace.CONTRIBUTOR, off)
67
         rapi.create_one(u, w3, UserRoleInWorkspace.CONTRIBUTOR, off)
68
         rapi.create_one(u, w2, UserRoleInWorkspace.CONTENT_MANAGER, off)
68
         rapi.create_one(u, w2, UserRoleInWorkspace.CONTENT_MANAGER, off)
69
         rapi.create_one(u, w1, UserRoleInWorkspace.WORKSPACE_MANAGER, off)
69
         rapi.create_one(u, w1, UserRoleInWorkspace.WORKSPACE_MANAGER, off)
70
-        eq_([], wapi.get_all_manageable_for_user(user=u))
70
+        eq_([], wapi.get_all_manageable())
71
         # Checks a manager gets only its own workspaces.
71
         # Checks a manager gets only its own workspaces.
72
         u.groups.append(gapi.get_one(Group.TIM_MANAGER))
72
         u.groups.append(gapi.get_one(Group.TIM_MANAGER))
73
         rapi.delete_one(u.user_id, w2.workspace_id)
73
         rapi.delete_one(u.user_id, w2.workspace_id)
74
         rapi.create_one(u, w2, UserRoleInWorkspace.WORKSPACE_MANAGER, off)
74
         rapi.create_one(u, w2, UserRoleInWorkspace.WORKSPACE_MANAGER, off)
75
-        eq_([w1, w2], wapi.get_all_manageable_for_user(user=u))
75
+        eq_([w1, w2], wapi.get_all_manageable())