Fix reporting of custom fields in webhook data on object deletion

This commit is contained in:
Jeremy Stretch 2019-10-22 16:12:25 -04:00
parent ccb9f7bfe2
commit 0ebc2e4ac0
2 changed files with 13 additions and 8 deletions

View File

@ -97,13 +97,13 @@ class CustomFieldModelSerializer(ValidatedModelSerializer):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
def _populate_custom_fields(instance, fields): def _populate_custom_fields(instance, fields):
custom_fields = {f.name: None for f in fields} instance.custom_fields = {}
for cfv in instance.custom_field_values.all(): for field in fields:
if cfv.field.type == CF_TYPE_SELECT: value = instance.cf.get(field.name)
custom_fields[cfv.field.name] = CustomFieldChoiceSerializer(cfv.value).data if field.type == CF_TYPE_SELECT and value is not None:
instance.custom_fields[field.name] = CustomFieldChoiceSerializer(value).data
else: else:
custom_fields[cfv.field.name] = cfv.value instance.custom_fields[field.name] = value
instance.custom_fields = custom_fields
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)

View File

@ -22,7 +22,7 @@ def handle_changed_object(sender, instance, **kwargs):
""" """
Fires when an object is created or updated. Fires when an object is created or updated.
""" """
# Queue a copy of the object for processing once the request completes # Queue the object for processing once the request completes
action = OBJECTCHANGE_ACTION_CREATE if kwargs['created'] else OBJECTCHANGE_ACTION_UPDATE action = OBJECTCHANGE_ACTION_CREATE if kwargs['created'] else OBJECTCHANGE_ACTION_UPDATE
_thread_locals.changed_objects.append( _thread_locals.changed_objects.append(
(instance, action) (instance, action)
@ -44,7 +44,7 @@ def handle_deleted_object(sender, instance, **kwargs):
if hasattr(instance, 'tags'): if hasattr(instance, 'tags'):
copy.tags = DummyQuerySet(instance.tags.all()) copy.tags = DummyQuerySet(instance.tags.all())
# Queue a copy of the object for processing once the request completes # Queue the copy of the object for processing once the request completes
_thread_locals.changed_objects.append( _thread_locals.changed_objects.append(
(copy, OBJECTCHANGE_ACTION_DELETE) (copy, OBJECTCHANGE_ACTION_DELETE)
) )
@ -100,6 +100,11 @@ class ObjectChangeMiddleware(object):
# Create records for any cached objects that were changed. # Create records for any cached objects that were changed.
for instance, action in _thread_locals.changed_objects: for instance, action in _thread_locals.changed_objects:
# Refresh cached custom field values
if action in [OBJECTCHANGE_ACTION_CREATE, OBJECTCHANGE_ACTION_UPDATE]:
if hasattr(instance, 'cache_custom_fields'):
instance.cache_custom_fields()
# Record an ObjectChange if applicable # Record an ObjectChange if applicable
if hasattr(instance, 'to_objectchange'): if hasattr(instance, 'to_objectchange'):
objectchange = instance.to_objectchange(action) objectchange = instance.to_objectchange(action)