mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
* Initial work on #13427 * Clarify documentation * Reference public models registry when populating models for ConfigTemplate context
This commit is contained in:
@@ -7,15 +7,13 @@ import feedparser
|
||||
import requests
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.cache import cache
|
||||
from django.db.models import Q
|
||||
from django.template.loader import render_to_string
|
||||
from django.urls import NoReverseMatch, resolve, reverse
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from core.models import ContentType
|
||||
from extras.choices import BookmarkOrderingChoices
|
||||
from extras.utils import FeatureQuery
|
||||
from utilities.choices import ButtonColorChoices
|
||||
from utilities.forms import BootstrapMixin
|
||||
from utilities.permissions import get_permission_for_model
|
||||
@@ -37,10 +35,7 @@ __all__ = (
|
||||
def get_content_type_labels():
|
||||
return [
|
||||
(content_type_identifier(ct), content_type_name(ct))
|
||||
for ct in ContentType.objects.filter(
|
||||
FeatureQuery('export_templates').get_query() | Q(app_label='extras', model='objectchange') |
|
||||
Q(app_label='extras', model='configcontext')
|
||||
).order_by('app_label', 'model')
|
||||
for ct in ContentType.objects.public().order_by('app_label', 'model')
|
||||
]
|
||||
|
||||
|
||||
|
@@ -1,9 +1,9 @@
|
||||
from django import forms
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.postgres.forms import SimpleArrayField
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from core.models import ContentType
|
||||
from extras.choices import *
|
||||
from extras.models import *
|
||||
from extras.utils import FeatureQuery
|
||||
@@ -40,8 +40,7 @@ class CustomFieldImportForm(CSVModelForm):
|
||||
)
|
||||
object_type = CSVContentTypeField(
|
||||
label=_('Object type'),
|
||||
queryset=ContentType.objects.all(),
|
||||
limit_choices_to=FeatureQuery('custom_fields'),
|
||||
queryset=ContentType.objects.public(),
|
||||
required=False,
|
||||
help_text=_("Object type (for object or multi-object fields)")
|
||||
)
|
||||
|
@@ -1,9 +1,8 @@
|
||||
from django import forms
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from core.models import DataFile, DataSource
|
||||
from core.models import ContentType, DataFile, DataSource
|
||||
from dcim.models import DeviceRole, DeviceType, Location, Platform, Region, Site, SiteGroup
|
||||
from extras.choices import *
|
||||
from extras.models import *
|
||||
@@ -196,7 +195,7 @@ class SavedFilterFilterForm(SavedFiltersMixin, FilterForm):
|
||||
)
|
||||
content_types = ContentTypeMultipleChoiceField(
|
||||
label=_('Content types'),
|
||||
queryset=ContentType.objects.filter(FeatureQuery('export_templates').get_query()),
|
||||
queryset=ContentType.objects.public(),
|
||||
required=False
|
||||
)
|
||||
enabled = forms.NullBooleanField(
|
||||
|
@@ -2,12 +2,11 @@ import json
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.db.models import Q
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from core.forms.mixins import SyncedDataMixin
|
||||
from core.models import ContentType
|
||||
from dcim.models import DeviceRole, DeviceType, Location, Platform, Region, Site, SiteGroup
|
||||
from extras.choices import *
|
||||
from extras.models import *
|
||||
@@ -49,9 +48,7 @@ class CustomFieldForm(BootstrapMixin, forms.ModelForm):
|
||||
)
|
||||
object_type = ContentTypeChoiceField(
|
||||
label=_('Object type'),
|
||||
queryset=ContentType.objects.all(),
|
||||
# TODO: Come up with a canonical way to register suitable models
|
||||
limit_choices_to=FeatureQuery('webhooks').get_query() | Q(app_label='auth', model__in=['user', 'group']),
|
||||
queryset=ContentType.objects.public(),
|
||||
required=False,
|
||||
help_text=_("Type of the related object (for object/multi-object fields only)")
|
||||
)
|
||||
|
@@ -260,12 +260,14 @@ class ConfigTemplate(SyncedDataMixin, ExportTemplatesMixin, TagsMixin, ChangeLog
|
||||
_context = dict()
|
||||
|
||||
# Populate the default template context with NetBox model classes, namespaced by app
|
||||
# TODO: Devise a canonical mechanism for identifying the models to include (see #13427)
|
||||
for app, model_names in registry['model_features']['custom_fields'].items():
|
||||
for app, model_names in registry['models'].items():
|
||||
_context.setdefault(app, {})
|
||||
for model_name in model_names:
|
||||
model = apps.get_registered_model(app, model_name)
|
||||
_context[app][model.__name__] = model
|
||||
try:
|
||||
model = apps.get_registered_model(app, model_name)
|
||||
_context[app][model.__name__] = model
|
||||
except LookupError:
|
||||
pass
|
||||
|
||||
# Add the provided context data, if any
|
||||
if context is not None:
|
||||
|
@@ -49,6 +49,8 @@ class CachedValue(models.Model):
|
||||
default=1000
|
||||
)
|
||||
|
||||
_netbox_private = True
|
||||
|
||||
class Meta:
|
||||
ordering = ('weight', 'object_type', 'value', 'object_id')
|
||||
verbose_name = _('cached value')
|
||||
|
@@ -75,6 +75,8 @@ class TaggedItem(GenericTaggedItemBase):
|
||||
on_delete=models.CASCADE
|
||||
)
|
||||
|
||||
_netbox_private = True
|
||||
|
||||
class Meta:
|
||||
indexes = [models.Index(fields=["content_type", "object_id"])]
|
||||
verbose_name = _('tagged item')
|
||||
|
@@ -67,6 +67,10 @@ def register_features(model, features):
|
||||
f"{feature} is not a valid model feature! Valid keys are: {registry['model_features'].keys()}"
|
||||
)
|
||||
|
||||
# Register public models
|
||||
if not getattr(model, '_netbox_private', False):
|
||||
registry['models'][app_label].add(model_name)
|
||||
|
||||
|
||||
def is_script(obj):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user