|
3 | 3 | import json |
4 | 4 | import re |
5 | 5 | from datetime import datetime, date |
| 6 | +from copy import deepcopy |
6 | 7 |
|
7 | 8 | import django_filters |
8 | 9 | from django import forms |
9 | 10 | from django.conf import settings |
10 | 11 | from django.db import models |
11 | | -from django.db.models import F, Func, Value |
| 12 | +from django.db.models import F, Func, Value, Q, QuerySet |
12 | 13 | from django.db.models.expressions import RawSQL |
13 | 14 | from django.apps import apps |
14 | 15 | from django.contrib.contenttypes.models import ContentType |
|
17 | 18 | from django.utils.html import escape |
18 | 19 | from django.utils.safestring import mark_safe |
19 | 20 | from django.utils.translation import gettext_lazy as _ |
| 21 | +from core.models.contenttypes import ObjectTypeManager |
20 | 22 | from netbox.models import NetBoxModel, ChangeLoggedModel |
21 | 23 | from netbox.models.features import ( |
22 | 24 | BookmarksMixin, |
|
31 | 33 | TagsMixin, |
32 | 34 | EventRulesMixin, |
33 | 35 | ) |
| 36 | +from netbox.registry import registry |
34 | 37 | from extras.choices import ( |
35 | 38 | CustomFieldTypeChoices, CustomFieldFilterLogicChoices, CustomFieldUIVisibleChoices, CustomFieldUIEditableChoices |
36 | 39 | ) |
@@ -948,3 +951,28 @@ class CustomObjectRelation(models.Model): |
948 | 951 | def instance(self): |
949 | 952 | model_class = self.field.related_object_type.model_class() |
950 | 953 | return model_class.objects.get(pk=self.object_id) |
| 954 | + |
| 955 | + |
| 956 | +class CustomObjectObjectTypeManager(ObjectTypeManager): |
| 957 | + |
| 958 | + def public(self): |
| 959 | + """ |
| 960 | + Filter the base queryset to return only ContentTypes corresponding to "public" models; those which are listed |
| 961 | + in registry['models'] and intended for reference by other objects. |
| 962 | + """ |
| 963 | + q = Q() |
| 964 | + model_registry = deepcopy(registry['models']) |
| 965 | + model_registry['netbox_custom_objects'] = self.get_queryset().values_list('model', flat=True) |
| 966 | + for app_label, models in model_registry.items(): |
| 967 | + q |= Q(app_label=app_label, model__in=models) |
| 968 | + return self.get_queryset().filter(q) |
| 969 | + |
| 970 | + |
| 971 | +class CustomObjectObjectType(ContentType): |
| 972 | + """ |
| 973 | + Wrap Django's native ContentType model to use our custom manager. |
| 974 | + """ |
| 975 | + objects = CustomObjectObjectTypeManager() |
| 976 | + |
| 977 | + class Meta: |
| 978 | + proxy = True |
0 commit comments