PR updates to webhook signal refactor

This commit is contained in:
John Anderson 2018-07-30 13:56:00 -04:00
parent fad7fd731c
commit 4775ebb5e3
5 changed files with 16 additions and 16 deletions

View File

@ -103,9 +103,6 @@ class CustomFieldModelSerializer(ValidatedModelSerializer):
for cfv in instance.custom_field_values.all():
if cfv.field.type == CF_TYPE_SELECT:
custom_fields[cfv.field.name] = CustomFieldChoiceSerializer(cfv.value).data
elif cfv.field.type == CF_TYPE_DATE:
# convert datetime object to str
custom_fields[cfv.field.name] = str(cfv.value)
else:
custom_fields[cfv.field.name] = cfv.value
instance.custom_fields = custom_fields

View File

@ -22,12 +22,10 @@ _thread_locals = threading.local()
def mark_object_changed(instance, **kwargs):
"""
Mark an object as having been created, saved, or updated. At the end of the request, this change will be recorded.
We have to wait until the *end* of the request to the serialize the object, because related fields like tags and
custom fields have not yet been updated when the post_save signal is emitted.
Mark an object as having been created, saved, or updated. At the end of the request, this change will be recorded
and/or associated webhooks fired. We have to wait until the *end* of the request to the serialize the object,
because related fields like tags and custom fields have not yet been updated when the post_save signal is emitted.
"""
if hasattr(instance, 'log_change') and instance.__class__._meta.verbose_name not in WEBHOOK_MODELS:
return
# Determine what action is being performed. The post_save signal sends a `created` boolean, whereas post_delete
# does not.
@ -39,7 +37,12 @@ def mark_object_changed(instance, **kwargs):
_thread_locals.changed_objects.append((instance, action))
class ChangeLoggingAndWebhookMiddleware(object):
class ObjectChangeMiddleware(object):
"""
This middleware intercepts all requests to connects object signals to the Django runtime. The signals collect all
changed objects into a local thread by way of the `mark_object_changed()` receiver. At the end of the request,
the middleware iterates over the objects to process change events like Change Logging and Webhooks.
"""
def __init__(self, get_response):
self.get_response = get_response
@ -67,7 +70,7 @@ class ChangeLoggingAndWebhookMiddleware(object):
obj.log_change(request.user, request.id, action)
# Enqueue Webhooks if they are enabled
if settings.WEBHOOKS_ENABLED and obj.__class__._meta.verbose_name in WEBHOOK_MODELS:
if settings.WEBHOOKS_ENABLED and obj.__class__.__name__.lower() in WEBHOOK_MODELS:
enqueue_webhooks(obj, action)
# Housekeeping: 1% chance of clearing out expired ObjectChanges

View File

@ -177,7 +177,7 @@ class CustomFieldAPITest(HttpStatusMixin, APITestCase):
(self.cf_text, 'Test string'),
(self.cf_integer, 1234),
(self.cf_boolean, True),
(self.cf_date, "2016-06-23"),
(self.cf_date, date(2016, 6, 23)),
(self.cf_url, 'http://example.com/'),
(self.cf_select, self.cf_select_choice1.pk),
]

View File

@ -13,9 +13,9 @@ def enqueue_webhooks(instance, action):
Find Webhook(s) assigned to this instance + action and enqueue them
to be processed
"""
type_create = True if action == OBJECTCHANGE_ACTION_CREATE else False
type_update = True if action == OBJECTCHANGE_ACTION_UPDATE else False
type_delete = True if action == OBJECTCHANGE_ACTION_DELETE else False
type_create = action == OBJECTCHANGE_ACTION_CREATE
type_update = action == OBJECTCHANGE_ACTION_UPDATE
type_delete = action == OBJECTCHANGE_ACTION_DELETE
# Find assigned webhooks
obj_type = ContentType.objects.get_for_model(instance.__class__)
@ -37,7 +37,7 @@ def enqueue_webhooks(instance, action):
}
serializer = serializer_class(instance, context=serializer_context)
# We must only use django_rq if the Webhooks feature is enabled.
# We must only import django_rq if the Webhooks feature is enabled.
# Only if we have gotten to ths point, is the feature enabled
from django_rq import get_queue
webhook_queue = get_queue('default')

View File

@ -175,7 +175,7 @@ MIDDLEWARE = (
'utilities.middleware.ExceptionHandlingMiddleware',
'utilities.middleware.LoginRequiredMiddleware',
'utilities.middleware.APIVersionMiddleware',
'extras.middleware.ChangeLoggingAndWebhookMiddleware',
'extras.middleware.ObjectChangeMiddleware',
)
ROOT_URLCONF = 'netbox.urls'