Fix sending events accidentally before commit

remotes/origin/issue/4795/notification_even_they_are_disabled
Jesús Espino 2016-11-24 09:56:14 +01:00 committed by David Barragán Merino
parent 2a8927c06a
commit ad8b2549d7
3 changed files with 14 additions and 12 deletions

View File

@ -17,10 +17,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import collections import collections
from django.contrib.contenttypes.models import ContentType from django.db import connection
from taiga.base.utils import json from taiga.base.utils import json
from taiga.base.utils.db import get_typename_for_model_instance from taiga.base.utils.db import get_typename_for_model_instance
@ -39,7 +38,8 @@ watched_types = set([
def emit_event(data:dict, routing_key:str, *, def emit_event(data:dict, routing_key:str, *,
sessionid:str=None, channel:str="events"): sessionid:str=None, channel:str="events",
on_commit:bool=True):
if not sessionid: if not sessionid:
sessionid = mw.get_current_session_id() sessionid = mw.get_current_session_id()
@ -47,9 +47,14 @@ def emit_event(data:dict, routing_key:str, *,
"data": data} "data": data}
backend = backends.get_events_backend() backend = backends.get_events_backend()
return backend.emit_event(message=json.dumps(data),
routing_key=routing_key, def backend_emit_event():
channel=channel) backend.emit_event(message=json.dumps(data), routing_key=routing_key, channel=channel)
if on_commit:
connection.on_commit(backend_emit_event)
else:
backend_emit_event()
def emit_event_for_model(obj, *, type:str="change", channel:str="events", def emit_event_for_model(obj, *, type:str="change", channel:str="events",

View File

@ -33,4 +33,4 @@ class Command(BaseCommand):
"desc": options["description"], "desc": options["description"],
} }
routing_key = "notifications" routing_key = "notifications"
emit_event(data, routing_key) emit_event(data, routing_key, on_commit=False)

View File

@ -17,7 +17,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.db.models import signals from django.db.models import signals
from django.db import connection
from django.dispatch import receiver from django.dispatch import receiver
@ -43,8 +42,7 @@ def on_save_any_model(sender, instance, created, **kwargs):
if created: if created:
type = "create" type = "create"
emit_event = lambda: events.emit_event_for_model(instance, sessionid=sesionid, type=type) events.emit_event_for_model(instance, sessionid=sesionid, type=type)
connection.on_commit(emit_event)
def on_delete_any_model(sender, instance, **kwargs): def on_delete_any_model(sender, instance, **kwargs):
@ -57,5 +55,4 @@ def on_delete_any_model(sender, instance, **kwargs):
sesionid = mw.get_current_session_id() sesionid = mw.get_current_session_id()
emit_event = lambda: events.emit_event_for_model(instance, sessionid=sesionid, type="delete") events.emit_event_for_model(instance, sessionid=sesionid, type="delete")
connection.on_commit(emit_event)