notifications.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. from sqlalchemy.orm import Session
  3. from tracim import CFG
  4. from tracim.lib.utils.logger import logger
  5. from tracim.models.auth import User
  6. from tracim.models.data import Content
  7. class INotifier(object):
  8. """
  9. Interface for Notifier instances
  10. """
  11. def __init__(self,
  12. config: CFG,
  13. session: Session,
  14. current_user: User=None,
  15. ) -> None:
  16. pass
  17. def notify_content_update(self, content: Content):
  18. raise NotImplementedError
  19. class NotifierFactory(object):
  20. @classmethod
  21. def create(cls, config, session, current_user: User=None) -> INotifier:
  22. if not config.EMAIL_NOTIFICATION_ACTIVATED:
  23. return DummyNotifier(config, session, current_user)
  24. from tracim.lib.mail_notifier.notifier import EmailNotifier
  25. return EmailNotifier(config, session, current_user)
  26. class DummyNotifier(INotifier):
  27. send_count = 0
  28. def __init__(
  29. self,
  30. config: CFG,
  31. session: Session,
  32. current_user: User=None
  33. ) -> None:
  34. INotifier.__init__(
  35. self,
  36. config,
  37. session,
  38. current_user,
  39. )
  40. logger.info(self, 'Instantiating Dummy Notifier')
  41. def notify_content_update(self, content: Content):
  42. type(self).send_count += 1
  43. logger.info(
  44. self,
  45. 'Fake notifier, do not send notification for update of content {}'.format(content.content_id) # nopep8
  46. )