mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-17 01:02:18 -06:00
* Convert ObjectType to a concrete child model of ContentType * Add public flag to ObjectType * Catch post_migrate signal to update ObjectTypes * Reference ObjectType records instead of registry for feature support * Automatically create ObjectTypes * Introduce has_feature() utility function * ObjectTypeManager should not inherit from ContentTypeManager * Misc cleanup * Don't populate ObjectTypes during migration * Don't automatically create ObjectTypes when a ContentType is created * Fix test * Extend has_feature() to accept a model or OT/CT * Misc cleanup * Deprecate get_for_id() on ObjectTypeManager * Rename contenttypes.py to object_types.py * Add index to features ArrayField * Keep FK & M2M fields pointing to ContentType * Add get_for_models() to ObjectTypeManager * Add tests for manager methods & utility functions * Fix migrations for M2M relations to ObjectType * model_is_public() should return False for non-core & non-plugin models * Order ObjectType by app_label & model name * Resolve migrations conflict
This commit is contained in:
@@ -2,9 +2,9 @@ import logging
|
||||
from threading import local
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.exceptions import ObjectDoesNotExist, ValidationError
|
||||
from django.db.models.fields.reverse_related import ManyToManyRel, ManyToOneRel
|
||||
from django.db.models.signals import m2m_changed, post_save, pre_delete
|
||||
from django.db.models.signals import m2m_changed, post_migrate, post_save, pre_delete
|
||||
from django.dispatch import receiver, Signal
|
||||
from django.core.signals import request_finished
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -12,11 +12,12 @@ from django_prometheus.models import model_deletes, model_inserts, model_updates
|
||||
|
||||
from core.choices import JobStatusChoices, ObjectChangeActionChoices
|
||||
from core.events import *
|
||||
from core.models import ObjectType
|
||||
from extras.events import enqueue_event
|
||||
from extras.utils import run_validators
|
||||
from netbox.config import get_config
|
||||
from netbox.context import current_request, events_queue
|
||||
from netbox.models.features import ChangeLoggingMixin
|
||||
from netbox.models.features import ChangeLoggingMixin, get_model_features, model_is_public
|
||||
from utilities.exceptions import AbortRequest
|
||||
from .models import ConfigRevision, DataSource, ObjectChange
|
||||
|
||||
@@ -40,6 +41,37 @@ post_sync = Signal()
|
||||
clear_events = Signal()
|
||||
|
||||
|
||||
#
|
||||
# Object types
|
||||
#
|
||||
|
||||
@receiver(post_migrate)
|
||||
def update_object_types(sender, **kwargs):
|
||||
"""
|
||||
Create or update the corresponding ObjectType for each model within the migrated app.
|
||||
"""
|
||||
for model in sender.get_models():
|
||||
app_label, model_name = model._meta.label_lower.split('.')
|
||||
|
||||
# Determine whether model is public and its supported features
|
||||
is_public = model_is_public(model)
|
||||
features = get_model_features(model)
|
||||
|
||||
# Create/update the ObjectType for the model
|
||||
try:
|
||||
ot = ObjectType.objects.get_by_natural_key(app_label=app_label, model=model_name)
|
||||
ot.public = is_public
|
||||
ot.features = features
|
||||
ot.save()
|
||||
except ObjectDoesNotExist:
|
||||
ObjectType.objects.create(
|
||||
app_label=app_label,
|
||||
model=model_name,
|
||||
public=is_public,
|
||||
features=features,
|
||||
)
|
||||
|
||||
|
||||
#
|
||||
# Change logging & event handling
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user