|
4 | 4 | import swapper |
5 | 5 | from asgiref.sync import async_to_sync |
6 | 6 | from django.conf import settings |
| 7 | +from django.db import transaction |
7 | 8 | from django.db.models import Case, Count, Sum, When |
8 | 9 | from django.db.models.signals import post_save |
9 | 10 | from django.utils.translation import gettext_lazy as _ |
@@ -131,26 +132,27 @@ def _load_receivers(self): |
131 | 132 | def _location_post_save_websocket_receiver( |
132 | 133 | self, sender, instance, created, **kwargs |
133 | 134 | ): |
134 | | - """ |
135 | | - Sends location updates over websockets to organization specific channel group. |
136 | | - """ |
137 | 135 | if created or not instance.geometry: |
138 | 136 | return |
139 | | - channel_layer = channels.layers.get_channel_layer() |
140 | | - async_to_sync(channel_layer.group_send)( |
141 | | - f"loci.mobile-location.organization.{instance.organization_id}", |
142 | | - { |
143 | | - "type": "send_message", |
144 | | - "message": { |
145 | | - "id": str(instance.pk), |
146 | | - "geometry": json.loads(instance.geometry.geojson), |
147 | | - "address": instance.address, |
148 | | - "name": instance.name, |
149 | | - "type": instance.type, |
150 | | - "is_mobile": instance.is_mobile, |
| 137 | + |
| 138 | + def _send(): |
| 139 | + channel_layer = channels.layers.get_channel_layer() |
| 140 | + async_to_sync(channel_layer.group_send)( |
| 141 | + f"loci.mobile-location.organization.{instance.organization_id}", |
| 142 | + { |
| 143 | + "type": "send_message", |
| 144 | + "message": { |
| 145 | + "id": str(instance.pk), |
| 146 | + "geometry": json.loads(instance.geometry.geojson), |
| 147 | + "address": instance.address, |
| 148 | + "name": instance.name, |
| 149 | + "type": instance.type, |
| 150 | + "is_mobile": instance.is_mobile, |
| 151 | + }, |
151 | 152 | }, |
152 | | - }, |
153 | | - ) |
| 153 | + ) |
| 154 | + |
| 155 | + transaction.on_commit(_send) |
154 | 156 |
|
155 | 157 |
|
156 | 158 | del LociConfig |
0 commit comments