context_models.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # coding=utf-8
  2. import typing
  3. from datetime import datetime
  4. from slugify import slugify
  5. from sqlalchemy.orm import Session
  6. from tracim import CFG
  7. from tracim.models import User
  8. from tracim.models.auth import Profile
  9. from tracim.models.data import Workspace
  10. from tracim.models.workspace_menu_entries import default_workspace_menu_entry, \
  11. WorkspaceMenuEntry
  12. class LoginCredentials(object):
  13. """
  14. Login credentials model for login
  15. """
  16. def __init__(self, email: str, password: str):
  17. self.email = email
  18. self.password = password
  19. class UserInContext(object):
  20. """
  21. Interface to get User data and User data related to context.
  22. """
  23. def __init__(self, user: User, dbsession: Session, config: CFG):
  24. self.user = user
  25. self.dbsession = dbsession
  26. self.config = config
  27. # Default
  28. @property
  29. def email(self) -> str:
  30. return self.user.email
  31. @property
  32. def user_id(self) -> int:
  33. return self.user.user_id
  34. @property
  35. def display_name(self) -> str:
  36. return self.user.display_name
  37. @property
  38. def created(self) -> datetime:
  39. return self.user.created
  40. @property
  41. def is_active(self) -> bool:
  42. return self.user.is_active
  43. @property
  44. def timezone(self) -> str:
  45. return self.user.timezone
  46. @property
  47. def profile(self) -> Profile:
  48. return self.user.profile
  49. # Context related
  50. @property
  51. def calendar_url(self) -> typing.Optional[str]:
  52. # TODO - G-M - 20-04-2018 - [Calendar] Replace calendar code to get
  53. # url calendar url.
  54. #
  55. # from tracim.lib.calendar import CalendarManager
  56. # calendar_manager = CalendarManager(None)
  57. # return calendar_manager.get_workspace_calendar_url(self.workspace_id)
  58. return None
  59. @property
  60. def avatar_url(self) -> typing.Optional[str]:
  61. # TODO - G-M - 20-04-2018 - [Avatar] Add user avatar feature
  62. return None
  63. class WorkspaceInContext(object):
  64. """
  65. Interface to get Workspace data and Workspace data related to context.
  66. """
  67. def __init__(self, workspace: Workspace, dbsession: Session, config: CFG):
  68. self.workspace = workspace
  69. self.dbsession = dbsession
  70. self.config = config
  71. @property
  72. def workspace_id(self) -> int:
  73. """
  74. numeric id of the workspace.
  75. """
  76. return self.workspace.workspace_id
  77. @property
  78. def id(self) -> int:
  79. """
  80. alias of workspace_id
  81. """
  82. return self.workspace_id
  83. @property
  84. def label(self) -> str:
  85. """
  86. get workspace label
  87. """
  88. return self.workspace.label
  89. @property
  90. def description(self) -> str:
  91. """
  92. get workspace description
  93. """
  94. return self.workspace.description
  95. @property
  96. def slug(self) -> str:
  97. """
  98. get workspace slug
  99. """
  100. return slugify(self.workspace.label)
  101. @property
  102. def sidebar_entries(self) -> typing.List[WorkspaceMenuEntry]:
  103. """
  104. get sidebar entries, those depends on activated apps.
  105. """
  106. # TODO - G.M - 22-05-2018 - Rework on this in
  107. # order to not use hardcoded list
  108. # list should be able to change (depending on activated/disabled
  109. # apps)
  110. return default_workspace_menu_entry(self.workspace)