This commit is contained in:
anhduy-tech
2025-12-31 16:44:06 +07:00
parent 8425d84102
commit 633a401aa6
6 changed files with 81 additions and 33 deletions

View File

@@ -59,8 +59,10 @@ class DataConsumer(AsyncJsonWebsocketConsumer):
from .views import get_serializer
payload = event["payload"]
change_type = payload.get("change_type")
record_id = payload["record"].get("id")
if not record_id:
if not record_id or not change_type:
return
model_name_lower = payload["name"]
@@ -71,7 +73,19 @@ class DataConsumer(AsyncJsonWebsocketConsumer):
if not client_params:
return # This client is not subscribed to this model.
# 2. Check if the updated record ID could possibly match the client's filter
# 2. Handle DELETION event
if change_type == 'deleted':
await self.send_json({
"type": "realtime_update",
"payload": {
"name": model_name_lower,
"change_type": "deleted",
"record": {"id": record_id}
}
})
return
# 3. Handle CREATION and UPDATE events
Model, _ = get_serializer(model_name_lower)
if not Model:
return
@@ -80,36 +94,37 @@ class DataConsumer(AsyncJsonWebsocketConsumer):
filter_q = Q()
filter_dict = client_params.get('filter')
if isinstance(filter_dict, dict):
for key, value in filter_dict.items():
filter_q.add(Q(**{key: value}), Q.AND)
filter_q = Q(**filter_dict)
# Combine the client's filter with the specific record's ID
combined_filter = Q(pk=record_id) & filter_q
# Check if the object actually exists with the combined filter
record_exists = await database_sync_to_async(Model.objects.filter(combined_filter).exists)()
if not record_exists:
return # The record does not match the client's filter, so don't send.
record_exists = await database_sync_to_async(
Model.objects.filter(Q(pk=record_id) & filter_q).exists
)()
# 3. Re-run the original query for just this single object.
# This correctly applies 'values', 'distinct_values', 'summary', etc.
single_record_params = client_params.copy()
single_record_params['filter'] = {'pk': record_id}
data = await database_sync_to_async(execute_data_query)(model_name_capitalized, single_record_params)
if record_exists:
# If record matches filter, re-run the query to get the correctly shaped data
single_record_params = client_params.copy()
single_record_params['filter'] = {'pk': record_id}
data = await database_sync_to_async(execute_data_query)(model_name_capitalized, single_record_params)
# If the query returns no data (e.g., record was deleted or no longer matches), do nothing.
if not data or not data.get('rows'):
return
# 4. Build the final payload with the correctly-shaped record
payload_for_client = {
"name": model_name_lower,
"record": data['rows'][0]
}
# 5. Send the final, tailored payload to the client
await self.send_json({
"type": "realtime_update",
"payload": payload_for_client
})
if data and data.get('rows'):
payload_for_client = {
"name": model_name_lower,
"change_type": change_type,
"record": data['rows'][0]
}
await self.send_json({
"type": "realtime_update",
"payload": payload_for_client
})
else:
# If the record exists but no longer matches the filter, treat as a deletion for this client
await self.send_json({
"type": "realtime_update",
"payload": {
"name": model_name_lower,
"change_type": "deleted",
"record": {"id": record_id}
}
})