|
@@ -0,0 +1,237 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+import typing
|
|
3
|
+
|
|
4
|
+from sqlalchemy.orm import Query
|
|
5
|
+from sqlalchemy.orm import Session
|
|
6
|
+from tracim.translation import fake_translator as _
|
|
7
|
+
|
|
8
|
+from tracim.lib.userworkspace import RoleApi
|
|
9
|
+from tracim.models.auth import Group
|
|
10
|
+from tracim.models.auth import User
|
|
11
|
+from tracim.models.data import UserRoleInWorkspace
|
|
12
|
+from tracim.models.data import Workspace
|
|
13
|
+
|
|
14
|
+__author__ = 'damien'
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+class WorkspaceApi(object):
|
|
18
|
+
|
|
19
|
+ def __init__(
|
|
20
|
+ self,
|
|
21
|
+ session: Session,
|
|
22
|
+ current_user: User,
|
|
23
|
+ force_role: bool=False
|
|
24
|
+ ):
|
|
25
|
+ """
|
|
26
|
+ :param current_user: Current user of context
|
|
27
|
+ :param force_role: If True, app role in queries even if admin
|
|
28
|
+ """
|
|
29
|
+ self._session = session
|
|
30
|
+ self._user = current_user
|
|
31
|
+ self._force_role = force_role
|
|
32
|
+
|
|
33
|
+ def _base_query_without_roles(self):
|
|
34
|
+ return self._session.query(Workspace).filter(Workspace.is_deleted == False)
|
|
35
|
+
|
|
36
|
+ def _base_query(self):
|
|
37
|
+ if not self._force_role and self._user.profile.id>=Group.TIM_ADMIN:
|
|
38
|
+ return self._base_query_without_roles()
|
|
39
|
+
|
|
40
|
+ return self._session.query(Workspace).\
|
|
41
|
+ join(Workspace.roles).\
|
|
42
|
+ filter(UserRoleInWorkspace.user_id == self._user.user_id).\
|
|
43
|
+ filter(Workspace.is_deleted == False)
|
|
44
|
+
|
|
45
|
+ def create_workspace(
|
|
46
|
+ self,
|
|
47
|
+ label: str='',
|
|
48
|
+ description: str='',
|
|
49
|
+ calendar_enabled: bool=False,
|
|
50
|
+ save_now: bool=False,
|
|
51
|
+ ) -> Workspace:
|
|
52
|
+ if not label:
|
|
53
|
+ label = self.generate_label()
|
|
54
|
+
|
|
55
|
+ workspace = Workspace()
|
|
56
|
+ workspace.label = label
|
|
57
|
+ workspace.description = description
|
|
58
|
+ workspace.calendar_enabled = calendar_enabled
|
|
59
|
+
|
|
60
|
+ # By default, we force the current user to be the workspace manager
|
|
61
|
+ # And to receive email notifications
|
|
62
|
+ role_api = RoleApi(
|
|
63
|
+ session=self._session,
|
|
64
|
+ current_user=self._user,
|
|
65
|
+ )
|
|
66
|
+
|
|
67
|
+ role = role_api.create_one(
|
|
68
|
+ self._user,
|
|
69
|
+ workspace,
|
|
70
|
+ UserRoleInWorkspace.WORKSPACE_MANAGER,
|
|
71
|
+ with_notif=True,
|
|
72
|
+ )
|
|
73
|
+
|
|
74
|
+ self._session.add(workspace)
|
|
75
|
+ self._session.add(role)
|
|
76
|
+
|
|
77
|
+ if save_now:
|
|
78
|
+ self._session.flush()
|
|
79
|
+
|
|
80
|
+ # TODO - G.M - 28-03-2018 - [Calendar] Reenable calendar stuff
|
|
81
|
+ # if calendar_enabled:
|
|
82
|
+ # self._ensure_calendar_exist(workspace)
|
|
83
|
+ # else:
|
|
84
|
+ # self._disable_calendar(workspace)
|
|
85
|
+
|
|
86
|
+ return workspace
|
|
87
|
+
|
|
88
|
+ def get_one(self, id):
|
|
89
|
+ return self._base_query().filter(Workspace.workspace_id == id).one()
|
|
90
|
+
|
|
91
|
+ def get_one_by_label(self, label: str) -> Workspace:
|
|
92
|
+ return self._base_query().filter(Workspace.label == label).one()
|
|
93
|
+
|
|
94
|
+ """
|
|
95
|
+ def get_one_for_current_user(self, id):
|
|
96
|
+ return self._base_query().filter(Workspace.workspace_id==id).\
|
|
97
|
+ session.query(ZKContact).filter(ZKContact.groups.any(ZKGroup.id.in_([1,2,3])))
|
|
98
|
+ filter(sqla.).one()
|
|
99
|
+ """
|
|
100
|
+
|
|
101
|
+ def get_all(self):
|
|
102
|
+ return self._base_query().all()
|
|
103
|
+
|
|
104
|
+ def get_all_for_user(self, user: User, ignored_ids=None):
|
|
105
|
+ workspaces = []
|
|
106
|
+
|
|
107
|
+ for role in user.roles:
|
|
108
|
+ if not role.workspace.is_deleted:
|
|
109
|
+ if not ignored_ids:
|
|
110
|
+ workspaces.append(role.workspace)
|
|
111
|
+ elif role.workspace.workspace_id not in ignored_ids:
|
|
112
|
+ workspaces.append(role.workspace)
|
|
113
|
+ else:
|
|
114
|
+ pass # do not return workspace
|
|
115
|
+
|
|
116
|
+ workspaces.sort(key=lambda workspace: workspace.label.lower())
|
|
117
|
+ return workspaces
|
|
118
|
+
|
|
119
|
+ def get_all_manageable(self) -> typing.List[Workspace]:
|
|
120
|
+ """Get all workspaces the current user has manager rights on."""
|
|
121
|
+ workspaces = [] # type: typing.List[Workspace]
|
|
122
|
+ if self._user.profile.id == Group.TIM_ADMIN:
|
|
123
|
+ workspaces = self._base_query().order_by(Workspace.label).all()
|
|
124
|
+ elif self._user.profile.id == Group.TIM_MANAGER:
|
|
125
|
+ workspaces = self._base_query() \
|
|
126
|
+ .filter(
|
|
127
|
+ UserRoleInWorkspace.role ==
|
|
128
|
+ UserRoleInWorkspace.WORKSPACE_MANAGER
|
|
129
|
+ ) \
|
|
130
|
+ .order_by(Workspace.label) \
|
|
131
|
+ .all()
|
|
132
|
+ return workspaces
|
|
133
|
+
|
|
134
|
+ def disable_notifications(self, user: User, workspace: Workspace):
|
|
135
|
+ for role in user.roles:
|
|
136
|
+ if role.workspace==workspace:
|
|
137
|
+ role.do_notify = False
|
|
138
|
+
|
|
139
|
+ def enable_notifications(self, user: User, workspace: Workspace):
|
|
140
|
+ for role in user.roles:
|
|
141
|
+ if role.workspace==workspace:
|
|
142
|
+ role.do_notify = True
|
|
143
|
+
|
|
144
|
+ def get_notifiable_roles(self, workspace: Workspace) -> [UserRoleInWorkspace]:
|
|
145
|
+ roles = []
|
|
146
|
+ for role in workspace.roles:
|
|
147
|
+ if role.do_notify==True \
|
|
148
|
+ and role.user!=self._user \
|
|
149
|
+ and role.user.is_active:
|
|
150
|
+ roles.append(role)
|
|
151
|
+ return roles
|
|
152
|
+
|
|
153
|
+ def save(self, workspace: Workspace):
|
|
154
|
+ self._session.flush()
|
|
155
|
+
|
|
156
|
+ def delete_one(self, workspace_id, flush=True):
|
|
157
|
+ workspace = self.get_one(workspace_id)
|
|
158
|
+ workspace.is_deleted = True
|
|
159
|
+
|
|
160
|
+ if flush:
|
|
161
|
+ self._session.flush()
|
|
162
|
+
|
|
163
|
+ def restore_one(self, workspace_id, flush=True):
|
|
164
|
+ workspace = self._session.query(Workspace)\
|
|
165
|
+ .filter(Workspace.is_deleted==True)\
|
|
166
|
+ .filter(Workspace.workspace_id==workspace_id).one()
|
|
167
|
+ workspace.is_deleted = False
|
|
168
|
+
|
|
169
|
+ if flush:
|
|
170
|
+ self._session.flush()
|
|
171
|
+
|
|
172
|
+ return workspace
|
|
173
|
+
|
|
174
|
+ def execute_created_workspace_actions(self, workspace: Workspace) -> None:
|
|
175
|
+ pass
|
|
176
|
+ # TODO - G.M - 28-03-2018 - [Calendar] Re-enable this calendar stuff
|
|
177
|
+ # self.ensure_calendar_exist(workspace)
|
|
178
|
+
|
|
179
|
+ # TODO - G.M - 28-03-2018 - [Calendar] Re-enable this calendar stuff
|
|
180
|
+ # def ensure_calendar_exist(self, workspace: Workspace) -> None:
|
|
181
|
+ # # Note: Cyclic imports
|
|
182
|
+ # from tracim.lib.calendar import CalendarManager
|
|
183
|
+ # from tracim.model.organisational import WorkspaceCalendar
|
|
184
|
+ #
|
|
185
|
+ # calendar_manager = CalendarManager(self._user)
|
|
186
|
+ #
|
|
187
|
+ # try:
|
|
188
|
+ # calendar_manager.enable_calendar_file(
|
|
189
|
+ # calendar_class=WorkspaceCalendar,
|
|
190
|
+ # related_object_id=workspace.workspace_id,
|
|
191
|
+ # raise_=True,
|
|
192
|
+ # )
|
|
193
|
+ # # If previous calendar file no exist, calendar must be created
|
|
194
|
+ # except FileNotFoundError:
|
|
195
|
+ # self._user.ensure_auth_token()
|
|
196
|
+ #
|
|
197
|
+ # # Ensure database is up-to-date
|
|
198
|
+ # self.session.flush()
|
|
199
|
+ # transaction.commit()
|
|
200
|
+ #
|
|
201
|
+ # calendar_manager.create_then_remove_fake_event(
|
|
202
|
+ # calendar_class=WorkspaceCalendar,
|
|
203
|
+ # related_object_id=workspace.workspace_id,
|
|
204
|
+ # )
|
|
205
|
+ #
|
|
206
|
+ # def disable_calendar(self, workspace: Workspace) -> None:
|
|
207
|
+ # # Note: Cyclic imports
|
|
208
|
+ # from tracim.lib.calendar import CalendarManager
|
|
209
|
+ # from tracim.model.organisational import WorkspaceCalendar
|
|
210
|
+ #
|
|
211
|
+ # calendar_manager = CalendarManager(self._user)
|
|
212
|
+ # calendar_manager.disable_calendar_file(
|
|
213
|
+ # calendar_class=WorkspaceCalendar,
|
|
214
|
+ # related_object_id=workspace.workspace_id,
|
|
215
|
+ # raise_=False,
|
|
216
|
+ # )
|
|
217
|
+
|
|
218
|
+ def get_base_query(self) -> Query:
|
|
219
|
+ return self._base_query()
|
|
220
|
+
|
|
221
|
+ def generate_label(self) -> str:
|
|
222
|
+ """
|
|
223
|
+ :return: Generated workspace label
|
|
224
|
+ """
|
|
225
|
+ query = self._base_query_without_roles() \
|
|
226
|
+ .filter(Workspace.label.ilike('{0}%'.format(
|
|
227
|
+ _('Workspace'),
|
|
228
|
+ )))
|
|
229
|
+
|
|
230
|
+ return _('Workspace {}').format(
|
|
231
|
+ query.count() + 1,
|
|
232
|
+ )
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+class UnsafeWorkspaceApi(WorkspaceApi):
|
|
236
|
+ def _base_query(self):
|
|
237
|
+ return self.session.query(Workspace).filter(Workspace.is_deleted==False)
|