userworkspace.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. def __init__(
  65. self,
  66. session: Session,
  67. current_user: typing.Optional[User],
  68. config: CFG,
  69. )-> None:
  70. self._session = session
  71. self._user = current_user
  72. self._config = config
  73. def _get_one_rsc(self, user_id: int, workspace_id: int) -> Query:
  74. """
  75. :param user_id:
  76. :param workspace_id:
  77. :return: a Query object, filtered query but without fetching the object.
  78. """
  79. return self._session.query(UserRoleInWorkspace).\
  80. filter(UserRoleInWorkspace.workspace_id == workspace_id).\
  81. filter(UserRoleInWorkspace.user_id == user_id)
  82. def get_one(self, user_id: int, workspace_id: int) -> UserRoleInWorkspace:
  83. return self._get_one_rsc(user_id, workspace_id).one()
  84. def create_one(
  85. self,
  86. user: User,
  87. workspace: Workspace,
  88. role_level: int,
  89. with_notif: bool,
  90. flush: bool=True
  91. ) -> UserRoleInWorkspace:
  92. role = UserRoleInWorkspace()
  93. role.user_id = user.user_id
  94. role.workspace = workspace
  95. role.role = role_level
  96. role.do_notify = with_notif
  97. if flush:
  98. self._session.flush()
  99. return role
  100. def delete_one(self, user_id: int, workspace_id: int, flush=True) -> None:
  101. self._get_one_rsc(user_id, workspace_id).delete()
  102. if flush:
  103. self._session.flush()
  104. def get_all_for_workspace(
  105. self,
  106. workspace:Workspace
  107. ) -> typing.List[UserRoleInWorkspace]:
  108. return self._session.query(UserRoleInWorkspace)\
  109. .filter(UserRoleInWorkspace.workspace_id==workspace.workspace_id)\
  110. .all()
  111. def save(self, role: UserRoleInWorkspace) -> None:
  112. self._session.flush()
  113. # TODO - G.M - 29-06-2018 - [Cleanup] Drop this
  114. # @classmethod
  115. # def role_can_read_member_role(cls, reader_role: int, tested_role: int) \
  116. # -> bool:
  117. # """
  118. # :param reader_role: role as viewer
  119. # :param tested_role: role as viwed
  120. # :return: True if given role can view member role in workspace.
  121. # """
  122. # if reader_role in cls.members_read_rights:
  123. # return tested_role in cls.members_read_rights[reader_role]
  124. # return False
  125. # def _get_all_for_user(self, user_id) -> typing.List[UserRoleInWorkspace]:
  126. # return self._session.query(UserRoleInWorkspace)\
  127. # .filter(UserRoleInWorkspace.user_id == user_id)
  128. #
  129. # def get_all_for_user(self, user: User) -> typing.List[UserRoleInWorkspace]:
  130. # return self._get_all_for_user(user.user_id).all()
  131. #
  132. # def get_all_for_user_order_by_workspace(
  133. # self,
  134. # user_id: int
  135. # ) -> typing.List[UserRoleInWorkspace]:
  136. # return self._get_all_for_user(user_id)\
  137. # .join(UserRoleInWorkspace.workspace).order_by(Workspace.label).all()
  138. # TODO - G.M - 07-06-2018 - [Cleanup] Check if this method is already needed
  139. # @classmethod
  140. # def get_roles_for_select_field(cls) -> typing.List[RoleType]:
  141. # """
  142. #
  143. # :return: list of DictLikeClass instances representing available Roles
  144. # (to be used in select fields)
  145. # """
  146. # result = list()
  147. #
  148. # for role_id in UserRoleInWorkspace.get_all_role_values():
  149. # role = RoleType(role_id)
  150. # result.append(role)
  151. #
  152. # return result