signal_handlers.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
  2. # Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
  3. # Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from django.db.models import signals
  17. from django.db import connection
  18. from django.dispatch import receiver
  19. from taiga.base.utils.db import get_typename_for_model_instance
  20. from . import middleware as mw
  21. from . import events
  22. def on_save_any_model(sender, instance, created, **kwargs):
  23. # Ignore any object that can not have project_id
  24. content_type = get_typename_for_model_instance(instance)
  25. # Ignore any other events
  26. if content_type not in events.watched_types:
  27. return
  28. sesionid = mw.get_current_session_id()
  29. type = "change"
  30. if created:
  31. type = "create"
  32. emit_event = lambda: events.emit_event_for_model(instance, sessionid=sesionid, type=type)
  33. connection.on_commit(emit_event)
  34. def on_delete_any_model(sender, instance, **kwargs):
  35. # Ignore any object that can not have project_id
  36. content_type = get_typename_for_model_instance(instance)
  37. # Ignore any other changes
  38. if content_type not in events.watched_types:
  39. return
  40. sesionid = mw.get_current_session_id()
  41. emit_event = lambda: events.emit_event_for_model(instance, sessionid=sesionid, type="delete")
  42. connection.on_commit(emit_event)