events.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU Affero General Public License as
  4. # published by the Free Software Foundation, either version 3 of the
  5. # License, or (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU Affero General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU Affero General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. import json
  15. import collections
  16. from django.contrib.contenttypes.models import ContentType
  17. from taiga.base.utils import json
  18. from taiga.base.utils.db import get_typename_for_model_instance
  19. from . import middleware as mw
  20. from . import backends
  21. # The complete list of content types
  22. # of allowed models for change events
  23. watched_types = set([
  24. "userstories.userstory",
  25. "issues.issue",
  26. "tasks.task",
  27. "wiki.wiki_page",
  28. "milestones.milestone",
  29. ])
  30. def emit_event(data:dict, routing_key:str, *,
  31. sessionid:str=None, channel:str="events"):
  32. if not sessionid:
  33. sessionid = mw.get_current_session_id()
  34. data = {"session_id": sessionid,
  35. "data": data}
  36. backend = backends.get_events_backend()
  37. return backend.emit_event(message=json.dumps(data),
  38. routing_key=routing_key,
  39. channel=channel)
  40. def emit_event_for_model(obj, *, type:str="change", channel:str="events",
  41. content_type:str=None, sessionid:str=None):
  42. """
  43. Sends a model change event.
  44. """
  45. if obj._importing:
  46. return None
  47. assert type in set(["create", "change", "delete"])
  48. assert hasattr(obj, "project_id")
  49. if not content_type:
  50. content_type = get_typename_for_model_instance(obj)
  51. projectid = getattr(obj, "project_id")
  52. pk = getattr(obj, "pk", None)
  53. app_name, model_name = content_type.split(".", 1)
  54. routing_key = "changes.project.{0}.{1}".format(projectid, app_name)
  55. data = {"type": type,
  56. "matches": content_type,
  57. "pk": pk}
  58. return emit_event(routing_key=routing_key,
  59. channel=channel,
  60. sessionid=sessionid,
  61. data=data)
  62. def emit_event_for_ids(ids, content_type:str, projectid:int, *,
  63. type:str="change", channel:str="events", sessionid:str=None):
  64. assert type in set(["create", "change", "delete"])
  65. assert isinstance(ids, collections.Iterable)
  66. assert content_type, "'content_type' parameter is mandatory"
  67. app_name, model_name = content_type.split(".", 1)
  68. routing_key = "changes.project.{0}.{1}".format(projectid, app_name)
  69. data = {"type": type,
  70. "matches": content_type,
  71. "pk": ids}
  72. return emit_event(routing_key=routing_key,
  73. channel=channel,
  74. sessionid=sessionid,
  75. data=data)