workspace.py 7.8KB

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