userworkspace.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. from tracim import CFG
  4. from tracim.models.context_models import UserRoleWorkspaceInContext
  5. __author__ = 'damien'
  6. from sqlalchemy.orm import Session
  7. from sqlalchemy.orm import Query
  8. from tracim.models.auth import User
  9. from tracim.models.data import Workspace
  10. from tracim.models.data import UserRoleInWorkspace
  11. class RoleApi(object):
  12. # TODO - G.M - 29-06-2018 - [Cleanup] Drop this
  13. # ALL_ROLE_VALUES = UserRoleInWorkspace.get_all_role_values()
  14. # Dict containing readable members roles for given role
  15. # members_read_rights = {
  16. # UserRoleInWorkspace.NOT_APPLICABLE: [],
  17. # UserRoleInWorkspace.READER: [
  18. # UserRoleInWorkspace.WORKSPACE_MANAGER,
  19. # ],
  20. # UserRoleInWorkspace.CONTRIBUTOR: [
  21. # UserRoleInWorkspace.WORKSPACE_MANAGER,
  22. # UserRoleInWorkspace.CONTENT_MANAGER,
  23. # UserRoleInWorkspace.CONTRIBUTOR,
  24. # ],
  25. # UserRoleInWorkspace.CONTENT_MANAGER: [
  26. # UserRoleInWorkspace.WORKSPACE_MANAGER,
  27. # UserRoleInWorkspace.CONTENT_MANAGER,
  28. # UserRoleInWorkspace.CONTRIBUTOR,
  29. # UserRoleInWorkspace.READER,
  30. # ],
  31. # UserRoleInWorkspace.WORKSPACE_MANAGER: [
  32. # UserRoleInWorkspace.WORKSPACE_MANAGER,
  33. # UserRoleInWorkspace.CONTENT_MANAGER,
  34. # UserRoleInWorkspace.CONTRIBUTOR,
  35. # UserRoleInWorkspace.READER,
  36. # ],
  37. # }
  38. # TODO - G.M - 29-06-2018 - [Cleanup] Drop this
  39. # @classmethod
  40. # def role_can_read_member_role(cls, reader_role: int, tested_role: int) \
  41. # -> bool:
  42. # """
  43. # :param reader_role: role as viewer
  44. # :param tested_role: role as viwed
  45. # :return: True if given role can view member role in workspace.
  46. # """
  47. # if reader_role in cls.members_read_rights:
  48. # return tested_role in cls.members_read_rights[reader_role]
  49. # return False
  50. def get_user_role_workspace_with_context(
  51. self,
  52. user_role: UserRoleInWorkspace
  53. ) -> UserRoleWorkspaceInContext:
  54. """
  55. Return WorkspaceInContext object from Workspace
  56. """
  57. assert self._config
  58. workspace = UserRoleWorkspaceInContext(
  59. user_role=user_role,
  60. dbsession=self._session,
  61. config=self._config,
  62. )
  63. return workspace
  64. @classmethod
  65. def create_role(cls) -> UserRoleInWorkspace:
  66. role = UserRoleInWorkspace()
  67. return role
  68. def __init__(
  69. self,
  70. session: Session,
  71. current_user: typing.Optional[User],
  72. config: CFG,
  73. )-> None:
  74. self._session = session
  75. self._user = current_user
  76. self._config = config
  77. def _get_one_rsc(self, user_id: int, workspace_id: int) -> Query:
  78. """
  79. :param user_id:
  80. :param workspace_id:
  81. :return: a Query object, filtered query but without fetching the object.
  82. """
  83. return self._session.query(UserRoleInWorkspace).\
  84. filter(UserRoleInWorkspace.workspace_id == workspace_id).\
  85. filter(UserRoleInWorkspace.user_id == user_id)
  86. def get_one(self, user_id: int, workspace_id: int) -> UserRoleInWorkspace:
  87. return self._get_one_rsc(user_id, workspace_id).one()
  88. def create_one(
  89. self,
  90. user: User,
  91. workspace: Workspace,
  92. role_level: int,
  93. with_notif: bool,
  94. flush: bool=True
  95. ) -> UserRoleInWorkspace:
  96. role = self.create_role()
  97. role.user_id = user.user_id
  98. role.workspace = workspace
  99. role.role = role_level
  100. role.do_notify = with_notif
  101. if flush:
  102. self._session.flush()
  103. return role
  104. def delete_one(self, user_id: int, workspace_id: int, flush=True) -> None:
  105. self._get_one_rsc(user_id, workspace_id).delete()
  106. if flush:
  107. self._session.flush()
  108. def get_all_for_workspace(
  109. self,
  110. workspace:Workspace
  111. ) -> typing.List[UserRoleInWorkspace]:
  112. return self._session.query(UserRoleInWorkspace)\
  113. .filter(UserRoleInWorkspace.workspace_id==workspace.workspace_id)\
  114. .all()
  115. def save(self, role: UserRoleInWorkspace) -> None:
  116. self._session.flush()
  117. # TODO - G.M - 29-06-2018 - [Cleanup] Drop this
  118. # @classmethod
  119. # def role_can_read_member_role(cls, reader_role: int, tested_role: int) \
  120. # -> bool:
  121. # """
  122. # :param reader_role: role as viewer
  123. # :param tested_role: role as viwed
  124. # :return: True if given role can view member role in workspace.
  125. # """
  126. # if reader_role in cls.members_read_rights:
  127. # return tested_role in cls.members_read_rights[reader_role]
  128. # return False
  129. # def _get_all_for_user(self, user_id) -> typing.List[UserRoleInWorkspace]:
  130. # return self._session.query(UserRoleInWorkspace)\
  131. # .filter(UserRoleInWorkspace.user_id == user_id)
  132. #
  133. # def get_all_for_user(self, user: User) -> typing.List[UserRoleInWorkspace]:
  134. # return self._get_all_for_user(user.user_id).all()
  135. #
  136. # def get_all_for_user_order_by_workspace(
  137. # self,
  138. # user_id: int
  139. # ) -> typing.List[UserRoleInWorkspace]:
  140. # return self._get_all_for_user(user_id)\
  141. # .join(UserRoleInWorkspace.workspace).order_by(Workspace.label).all()
  142. # TODO - G.M - 07-06-2018 - [Cleanup] Check if this method is already needed
  143. # @classmethod
  144. # def get_roles_for_select_field(cls) -> typing.List[RoleType]:
  145. # """
  146. #
  147. # :return: list of DictLikeClass instances representing available Roles
  148. # (to be used in select fields)
  149. # """
  150. # result = list()
  151. #
  152. # for role_id in UserRoleInWorkspace.get_all_role_values():
  153. # role = RoleType(role_id)
  154. # result.append(role)
  155. #
  156. # return result