user.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. import threading
  3. from smtplib import SMTPException
  4. import transaction
  5. import typing as typing
  6. from tracim.exceptions import NotificationNotSend
  7. from tracim.lib.mail_notifier.notifier import get_email_manager
  8. from tracim.models.auth import User
  9. class UserApi(object):
  10. def __init__(self, current_user: typing.Optional[User], session, config):
  11. self._session = session
  12. self._user = current_user
  13. self._config = config
  14. def get_all(self):
  15. return self._session.query(User).order_by(User.display_name).all()
  16. def _base_query(self):
  17. return self._session.query(User)
  18. def get_one(self, user_id: int):
  19. return self._base_query().filter(User.user_id==user_id).one()
  20. def get_one_by_email(self, email: str):
  21. return self._base_query().filter(User.email==email).one()
  22. def get_one_by_id(self, id: int) -> User:
  23. return self._base_query().filter(User.user_id==id).one()
  24. def update(
  25. self,
  26. user: User,
  27. name: str=None,
  28. email: str=None,
  29. password: str=None,
  30. timezone: str='',
  31. do_save=True,
  32. ):
  33. if name is not None:
  34. user.display_name = name
  35. if email is not None:
  36. user.email = email
  37. if password is not None:
  38. user.password = password
  39. user.timezone = timezone
  40. if do_save:
  41. self.save(user)
  42. def user_with_email_exists(self, email: str):
  43. try:
  44. self.get_one_by_email(email)
  45. return True
  46. # TODO - G.M - 09-04-2018 - Better exception
  47. except:
  48. return False
  49. def create_user(
  50. self,
  51. email,
  52. password: str = None,
  53. name: str = None,
  54. timezone: str = '',
  55. groups=[],
  56. do_save: bool=True,
  57. do_notify: bool=True,
  58. ) -> User:
  59. new_user = self.create_minimal_user(email, groups, save_now=False)
  60. self.update(
  61. user=new_user,
  62. name=name,
  63. email=email,
  64. password=password,
  65. timezone=timezone,
  66. do_save=False,
  67. )
  68. if do_notify:
  69. try:
  70. email_manager = get_email_manager(self._config, self._session)
  71. email_manager.notify_created_account(
  72. new_user,
  73. password=password
  74. )
  75. except SMTPException as e:
  76. raise NotificationNotSend()
  77. if do_save:
  78. self.save(new_user)
  79. return new_user
  80. def create_minimal_user(
  81. self,
  82. email,
  83. groups=[],
  84. save_now=False
  85. ) -> User:
  86. """Previous create_user method"""
  87. user = User()
  88. user.email = email
  89. for group in groups:
  90. user.groups.append(group)
  91. self._session.add(user)
  92. if save_now:
  93. self._session.flush()
  94. return user
  95. def save(self, user: User):
  96. self._session.flush()
  97. def execute_created_user_actions(self, created_user: User) -> None:
  98. """
  99. Execute actions when user just been created
  100. :return:
  101. """
  102. # NOTE: Cyclic import
  103. # TODO - G.M - 28-03-2018 - [Calendar] Reenable Calendar stuff
  104. #from tracim.lib.calendar import CalendarManager
  105. #from tracim.model.organisational import UserCalendar
  106. # TODO - G.M - 04-04-2018 - [auth]
  107. # Check if this is already needed with
  108. # new auth system
  109. created_user.ensure_auth_token(
  110. session=self._session,
  111. validity_seconds=self._config.USER_AUTH_TOKEN_VALIDITY
  112. )
  113. # Ensure database is up-to-date
  114. self._session.flush()
  115. transaction.commit()
  116. # TODO - G.M - 28-03-2018 - [Calendar] Reenable Calendar stuff
  117. # calendar_manager = CalendarManager(created_user)
  118. # calendar_manager.create_then_remove_fake_event(
  119. # calendar_class=UserCalendar,
  120. # related_object_id=created_user.user_id,
  121. # )