Skip to content

Commit 68edba8

Browse files
committed
Reference ObjectType records instead of registry for feature support
1 parent 28e5543 commit 68edba8

File tree

8 files changed

+37
-26
lines changed

8 files changed

+37
-26
lines changed

netbox/core/migrations/0016_concrete_objecttype.py renamed to netbox/core/migrations/0017_concrete_objecttype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Migration(migrations.Migration):
2525

2626
dependencies = [
2727
('contenttypes', '0002_remove_content_type_name'),
28-
('core', '0015_remove_redundant_indexes'),
28+
('core', '0016_job_log_entries'),
2929
]
3030

3131
operations = [

netbox/extras/events.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from django_rq import get_queue
1010

1111
from core.events import *
12+
from core.models import ObjectType
1213
from netbox.config import get_config
1314
from netbox.constants import RQ_QUEUE_DEFAULT
14-
from netbox.registry import registry
1515
from users.models import User
1616
from utilities.api import get_serializer_for_model
1717
from utilities.rqworker import get_rq_retry
@@ -55,11 +55,12 @@ def enqueue_event(queue, instance, user, request_id, event_type):
5555
Enqueue a serialized representation of a created/updated/deleted object for the processing of
5656
events once the request has completed.
5757
"""
58-
# Determine whether this type of object supports event rules
58+
# Bail if this type of object does not support event rules
59+
if 'event_rules' not in ObjectType.objects.get_for_model(instance).features:
60+
return
61+
5962
app_label = instance._meta.app_label
6063
model_name = instance._meta.model_name
61-
if model_name not in registry['model_features']['event_rules'].get(app_label, []):
62-
return
6364

6465
assert instance.pk is not None
6566
key = f'{app_label}.{model_name}:{instance.pk}'

netbox/extras/migrations/0130_concrete_objecttype.py renamed to netbox/extras/migrations/0131_concrete_objecttype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class Migration(migrations.Migration):
66
dependencies = [
7-
('core', '0016_concrete_objecttype'),
8-
('extras', '0129_fix_script_paths'),
7+
('core', '0017_concrete_objecttype'),
8+
('extras', '0130_imageattachment_description'),
99
]
1010

1111
operations = [

netbox/extras/models/configs.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from django.apps import apps
1+
from collections import defaultdict
2+
23
from django.conf import settings
34
from django.core.validators import ValidationError
45
from django.db import models
56
from django.urls import reverse
67
from django.utils.translation import gettext_lazy as _
78

9+
from core.models import ObjectType
810
from extras.models.mixins import RenderTemplateMixin
911
from extras.querysets import ConfigContextQuerySet
1012
from netbox.models import ChangeLoggedModel
1113
from netbox.models.features import CloningMixin, CustomLinksMixin, ExportTemplatesMixin, SyncedDataMixin, TagsMixin
12-
from netbox.registry import registry
1314
from utilities.data import deepmerge
1415

1516
__all__ = (
@@ -239,15 +240,12 @@ def sync_data(self):
239240
sync_data.alters_data = True
240241

241242
def get_context(self, context=None, queryset=None):
242-
_context = dict()
243-
for app, model_names in registry['models'].items():
244-
_context.setdefault(app, {})
245-
for model_name in model_names:
246-
try:
247-
model = apps.get_registered_model(app, model_name)
248-
_context[app][model.__name__] = model
249-
except LookupError:
250-
pass
243+
_context = defaultdict(dict)
244+
245+
# Populate all public models for reference within the template
246+
for object_type in ObjectType.objects.public():
247+
if model := object_type.model_class():
248+
_context[object_type.app_label][model.__name__] = model
251249

252250
# Apply the provided context data, if any
253251
if context is not None:

netbox/extras/signals.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from extras.events import process_event_rules
99
from extras.models import EventRule, Notification, Subscription
1010
from netbox.config import get_config
11-
from netbox.registry import registry
1211
from netbox.signals import post_clean
1312
from utilities.exceptions import AbortRequest
1413
from .models import CustomField, TaggedItem
@@ -150,17 +149,24 @@ def notify_object_changed(sender, instance, **kwargs):
150149
event_type = OBJECT_DELETED
151150

152151
# Skip unsupported object types
153-
ct = ContentType.objects.get_for_model(instance)
154-
if ct.model not in registry['model_features']['notifications'].get(ct.app_label, []):
152+
object_type = ObjectType.objects.get_for_model(instance)
153+
if 'notifications' not in object_type.features:
155154
return
156155

157156
# Find all subscribed Users
158-
subscribed_users = Subscription.objects.filter(object_type=ct, object_id=instance.pk).values_list('user', flat=True)
157+
subscribed_users = Subscription.objects.filter(
158+
object_type=object_type,
159+
object_id=instance.pk
160+
).values_list('user', flat=True)
159161
if not subscribed_users:
160162
return
161163

162164
# Delete any existing Notifications for the object
163-
Notification.objects.filter(object_type=ct, object_id=instance.pk, user__in=subscribed_users).delete()
165+
Notification.objects.filter(
166+
object_type=object_type,
167+
object_id=instance.pk,
168+
user__in=subscribed_users
169+
).delete()
164170

165171
# Create Notifications for Subscribers
166172
Notification.objects.bulk_create([

netbox/netbox/models/features.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'CustomValidationMixin',
3333
'EventRulesMixin',
3434
'ExportTemplatesMixin',
35+
'FEATURES_MAP',
3536
'ImageAttachmentsMixin',
3637
'JobsMixin',
3738
'JournalingMixin',
@@ -633,6 +634,7 @@ def sync_data(self):
633634
'tags': TagsMixin,
634635
}
635636

637+
# TODO: Remove in NetBox v4.5
636638
registry['model_features'].update({
637639
feature: defaultdict(set) for feature in FEATURES_MAP.keys()
638640
})
@@ -653,10 +655,12 @@ def register_models(*models):
653655
for model in models:
654656
app_label, model_name = model._meta.label_lower.split('.')
655657

658+
# TODO: Remove in NetBox v4.5
656659
# Register public models
657660
if not getattr(model, '_netbox_private', False):
658661
registry['models'][app_label].add(model_name)
659662

663+
# TODO: Remove in NetBox v4.5
660664
# Record each applicable feature for the model in the registry
661665
features = {
662666
feature for feature, cls in FEATURES_MAP.items() if issubclass(model, cls)

netbox/netbox/tests/test_plugins.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.urls import reverse
77

88
from core.choices import JobIntervalChoices
9+
from core.models import ObjectType
910
from netbox.tests.dummy_plugin import config as dummy_config
1011
from netbox.tests.dummy_plugin.data_backends import DummyBackend
1112
from netbox.tests.dummy_plugin.jobs import DummySystemJob
@@ -23,8 +24,9 @@ def test_config(self):
2324
self.assertIn('netbox.tests.dummy_plugin.DummyPluginConfig', settings.INSTALLED_APPS)
2425

2526
def test_model_registration(self):
26-
self.assertIn('dummy_plugin', registry['models'])
27-
self.assertIn('dummymodel', registry['models']['dummy_plugin'])
27+
self.assertIsNone(
28+
ObjectType.objects.filter(app_label='dummy_plugin', model='dummymodel')
29+
)
2830

2931
def test_models(self):
3032
from netbox.tests.dummy_plugin.models import DummyModel

netbox/users/migrations/0010_concrete_objecttype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class Migration(migrations.Migration):
55

66
dependencies = [
7-
('core', '0016_concrete_objecttype'),
7+
('core', '0017_concrete_objecttype'),
88
('users', '0009_update_group_perms'),
99
]
1010

0 commit comments

Comments
 (0)