Misc cleanup

This commit is contained in:
Jeremy Stretch 2025-07-24 08:25:55 -04:00
parent 34d9ecb7f3
commit 533fe555ea
4 changed files with 11 additions and 8 deletions

View File

@ -9,9 +9,9 @@ from django.utils.translation import gettext as _
from django_rq import get_queue
from core.events import *
from core.models import ObjectType
from netbox.config import get_config
from netbox.constants import RQ_QUEUE_DEFAULT
from netbox.models.features import has_feature
from users.models import User
from utilities.api import get_serializer_for_model
from utilities.rqworker import get_rq_retry
@ -56,7 +56,7 @@ def enqueue_event(queue, instance, user, request_id, event_type):
events once the request has completed.
"""
# Bail if this type of object does not support event rules
if 'event_rules' not in ObjectType.objects.get_for_model(instance).features:
if not has_feature(instance, 'event_rules'):
return
app_label = instance._meta.app_label

View File

@ -8,6 +8,7 @@ from core.signals import job_end, job_start
from extras.events import process_event_rules
from extras.models import EventRule, Notification, Subscription
from netbox.config import get_config
from netbox.models.features import has_feature
from netbox.signals import post_clean
from utilities.exceptions import AbortRequest
from .models import CustomField, TaggedItem
@ -149,13 +150,14 @@ def notify_object_changed(sender, instance, **kwargs):
event_type = OBJECT_DELETED
# Skip unsupported object types
object_type = ObjectType.objects.get_for_model(instance)
if 'notifications' not in object_type.features:
if not has_feature(instance, 'notifications'):
return
ct = ContentType.objects.get_for_model(instance)
# Find all subscribed Users
subscribed_users = Subscription.objects.filter(
object_type=object_type,
object_type=ct,
object_id=instance.pk
).values_list('user', flat=True)
if not subscribed_users:
@ -163,7 +165,7 @@ def notify_object_changed(sender, instance, **kwargs):
# Delete any existing Notifications for the object
Notification.objects.filter(
object_type=object_type,
object_type=ct,
object_id=instance.pk,
user__in=subscribed_users
).delete()

View File

@ -658,6 +658,7 @@ def has_feature(model, feature):
"""
Returns True if the model supports the specified feature.
"""
# Resolve a ContentType to its model class
if type(model) is ContentType:
model = model.model_class()
ot = ObjectType.objects.get_for_model(model)

View File

@ -24,8 +24,8 @@ class PluginTest(TestCase):
self.assertIn('netbox.tests.dummy_plugin.DummyPluginConfig', settings.INSTALLED_APPS)
def test_model_registration(self):
self.assertIsNone(
ObjectType.objects.filter(app_label='dummy_plugin', model='dummymodel')
self.assertTrue(
ObjectType.objects.get(app_label='dummy_plugin', model='dummymodel').exists()
)
def test_models(self):