signals.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import random
  2. from datetime import timedelta
  3. from cacheops.signals import cache_invalidated, cache_read
  4. from django.conf import settings
  5. from django.contrib.contenttypes.models import ContentType
  6. from django.db import DEFAULT_DB_ALIAS
  7. from django.db.models.signals import m2m_changed, post_save, pre_delete
  8. from django.dispatch import receiver
  9. from django.utils import timezone
  10. from django_prometheus.models import model_deletes, model_inserts, model_updates
  11. from prometheus_client import Counter
  12. from netbox.signals import post_clean
  13. from .choices import ObjectChangeActionChoices
  14. from .models import CustomField, ObjectChange
  15. from .webhooks import enqueue_object, get_snapshots, serialize_for_webhook
  16. #
  17. # Change logging/webhooks
  18. #
  19. def _handle_changed_object(request, webhook_queue, sender, instance, **kwargs):
  20. """
  21. Fires when an object is created or updated.
  22. """
  23. def is_same_object(instance, webhook_data):
  24. return (
  25. ContentType.objects.get_for_model(instance) == webhook_data['content_type'] and
  26. instance.pk == webhook_data['object_id'] and
  27. request.id == webhook_data['request_id']
  28. )
  29. if not hasattr(instance, 'to_objectchange'):
  30. return
  31. m2m_changed = False
  32. # Determine the type of change being made
  33. if kwargs.get('created'):
  34. action = ObjectChangeActionChoices.ACTION_CREATE
  35. elif 'created' in kwargs:
  36. action = ObjectChangeActionChoices.ACTION_UPDATE
  37. elif kwargs.get('action') in ['post_add', 'post_remove'] and kwargs['pk_set']:
  38. # m2m_changed with objects added or removed
  39. m2m_changed = True
  40. action = ObjectChangeActionChoices.ACTION_UPDATE
  41. else:
  42. return
  43. # Record an ObjectChange if applicable
  44. if hasattr(instance, 'to_objectchange'):
  45. if m2m_changed:
  46. ObjectChange.objects.filter(
  47. changed_object_type=ContentType.objects.get_for_model(instance),
  48. changed_object_id=instance.pk,
  49. request_id=request.id
  50. ).update(
  51. postchange_data=instance.to_objectchange(action).postchange_data
  52. )
  53. else:
  54. objectchange = instance.to_objectchange(action)
  55. objectchange.user = request.user
  56. objectchange.request_id = request.id
  57. objectchange.save()
  58. # If this is an M2M change, update the previously queued webhook (from post_save)
  59. if m2m_changed and webhook_queue and is_same_object(instance, webhook_queue[-1]):
  60. instance.refresh_from_db() # Ensure that we're working with fresh M2M assignments
  61. webhook_queue[-1]['data'] = serialize_for_webhook(instance)
  62. webhook_queue[-1]['snapshots']['postchange'] = get_snapshots(instance, action)['postchange']
  63. else:
  64. enqueue_object(webhook_queue, instance, request.user, request.id, action)
  65. # Increment metric counters
  66. if action == ObjectChangeActionChoices.ACTION_CREATE:
  67. model_inserts.labels(instance._meta.model_name).inc()
  68. elif action == ObjectChangeActionChoices.ACTION_UPDATE:
  69. model_updates.labels(instance._meta.model_name).inc()
  70. # Housekeeping: 0.1% chance of clearing out expired ObjectChanges
  71. if settings.CHANGELOG_RETENTION and random.randint(1, 1000) == 1:
  72. cutoff = timezone.now() - timedelta(days=settings.CHANGELOG_RETENTION)
  73. ObjectChange.objects.filter(time__lt=cutoff)._raw_delete(using=DEFAULT_DB_ALIAS)
  74. def _handle_deleted_object(request, webhook_queue, sender, instance, **kwargs):
  75. """
  76. Fires when an object is deleted.
  77. """
  78. if not hasattr(instance, 'to_objectchange'):
  79. return
  80. # Record an ObjectChange if applicable
  81. if hasattr(instance, 'to_objectchange'):
  82. objectchange = instance.to_objectchange(ObjectChangeActionChoices.ACTION_DELETE)
  83. objectchange.user = request.user
  84. objectchange.request_id = request.id
  85. objectchange.save()
  86. # Enqueue webhooks
  87. enqueue_object(webhook_queue, instance, request.user, request.id, ObjectChangeActionChoices.ACTION_DELETE)
  88. # Increment metric counters
  89. model_deletes.labels(instance._meta.model_name).inc()
  90. #
  91. # Custom fields
  92. #
  93. def handle_cf_removed_obj_types(instance, action, pk_set, **kwargs):
  94. """
  95. Handle the cleanup of old custom field data when a CustomField is removed from one or more ContentTypes.
  96. """
  97. if action == 'post_remove':
  98. instance.remove_stale_data(ContentType.objects.filter(pk__in=pk_set))
  99. def handle_cf_renamed(instance, created, **kwargs):
  100. """
  101. Handle the renaming of custom field data on objects when a CustomField is renamed.
  102. """
  103. if not created and instance.name != instance._name:
  104. instance.rename_object_data(old_name=instance._name, new_name=instance.name)
  105. def handle_cf_deleted(instance, **kwargs):
  106. """
  107. Handle the cleanup of old custom field data when a CustomField is deleted.
  108. """
  109. instance.remove_stale_data(instance.content_types.all())
  110. m2m_changed.connect(handle_cf_removed_obj_types, sender=CustomField.content_types.through)
  111. post_save.connect(handle_cf_renamed, sender=CustomField)
  112. pre_delete.connect(handle_cf_deleted, sender=CustomField)
  113. #
  114. # Custom validation
  115. #
  116. @receiver(post_clean)
  117. def run_custom_validators(sender, instance, **kwargs):
  118. model_name = f'{sender._meta.app_label}.{sender._meta.model_name}'
  119. validators = settings.CUSTOM_VALIDATORS.get(model_name, [])
  120. for validator in validators:
  121. validator(instance)
  122. #
  123. # Caching
  124. #
  125. cacheops_cache_hit = Counter('cacheops_cache_hit', 'Number of cache hits')
  126. cacheops_cache_miss = Counter('cacheops_cache_miss', 'Number of cache misses')
  127. cacheops_cache_invalidated = Counter('cacheops_cache_invalidated', 'Number of cache invalidations')
  128. def cache_read_collector(sender, func, hit, **kwargs):
  129. if hit:
  130. cacheops_cache_hit.inc()
  131. else:
  132. cacheops_cache_miss.inc()
  133. def cache_invalidated_collector(sender, obj_dict, **kwargs):
  134. cacheops_cache_invalidated.inc()
  135. cache_read.connect(cache_read_collector)
  136. cache_invalidated.connect(cache_invalidated_collector)