Skip to content

Commit fbbb519

Browse files
committed
T50470: push websocket updates upon saving models
1 parent 0c2f669 commit fbbb519

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

binder/models.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from decimal import Decimal
1010

1111
from django import forms
12-
from django.db import models
12+
from django.db import models, transaction
1313
from django.db.models import Value
1414
from django.db.models.fields.files import FieldFile, FileField
1515
from django.contrib.postgres.fields import CITextField, ArrayField, DateTimeRangeField as DTRangeField
@@ -28,6 +28,7 @@
2828
from binder.json import jsonloads
2929

3030
from binder.exceptions import BinderRequestError
31+
from binder.websocket import trigger
3132

3233
from . import history
3334

@@ -614,8 +615,11 @@ class Meta:
614615
ordering = ['pk']
615616

616617
def save(self, *args, **kwargs):
618+
from binder.views import determine_model_resource_name
617619
self.full_clean() # Never allow saving invalid models!
618-
return super().save(*args, **kwargs)
620+
result = super().save(*args, **kwargs)
621+
transaction.on_commit(lambda: trigger(self.pk, [{ 'target': 'auto-updates/' + determine_model_resource_name(self.__class__.__name__)}]))
622+
return result
619623

620624

621625
# This can be overridden in your model when there are special

binder/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ def prefix_q_expression(value, prefix, antiprefix=None, model=None):
342342
children.append((prefix + '__' + child[0], child[1]))
343343
return Q(*children, _connector=value.connector, _negated=value.negated)
344344

345+
def determine_model_resource_name(mn: str):
346+
return ''.join((x + '_' if x.islower() and y.isupper() else x.lower() for x, y in zip(mn, mn[1:] + 'x')))
345347

346348
class ModelView(View):
347349
# Model this is a view for. Use None for views not tied to a particular model.
@@ -578,9 +580,7 @@ def get_field_filter(self, field_class, reset=False):
578580
# Like model._meta.model_name, except it converts camelcase to underscores
579581
@classmethod
580582
def _model_name(cls):
581-
mn = cls.model.__name__
582-
return ''.join((x + '_' if x.islower() and y.isupper() else x.lower() for x, y in zip(mn, mn[1:] + 'x')))
583-
583+
return determine_model_resource_name(cls.model.__name__)
584584

585585

586586
# Use this to instantiate other views you need. It returns a properly initialized view instance.

0 commit comments

Comments
 (0)