Skip to content

Commit 963cc53

Browse files
authored
Merge pull request #205 from netboxlabs/feature
Merge feature -> main for 0.3.0
2 parents 5b0954b + 13423a0 commit 963cc53

26 files changed

+985
-205
lines changed

.github/workflows/lint-tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Setup Python
2424
uses: actions/setup-python@v5
2525
with:
26-
python-version: "3.10"
26+
python-version: "3.12"
2727
- name: Install dependencies
2828
run: |
2929
python -m pip install --upgrade pip
@@ -37,7 +37,7 @@ jobs:
3737
timeout-minutes: 10
3838
strategy:
3939
matrix:
40-
python-version: [ "3.10", "3.11", "3.12" ]
40+
python-version: [ "3.12" ]
4141
services:
4242
redis:
4343
image: redis

netbox_custom_objects/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from django.db.utils import DatabaseError, OperationalError, ProgrammingError
77
from netbox.plugins import PluginConfig
88

9+
from .constants import APP_LABEL as APP_LABEL
10+
911

1012
def is_running_migration():
1113
"""
@@ -106,11 +108,14 @@ def get_models(self, include_auto_created=False, include_swapped=False):
106108

107109
custom_object_types = CustomObjectType.objects.all()
108110
for custom_type in custom_object_types:
109-
# Only yield already cached models during discovery
110-
if CustomObjectType.is_model_cached(custom_type.id):
111-
model = CustomObjectType.get_cached_model(custom_type.id)
112-
if model:
113-
yield model
111+
model = custom_type.get_model()
112+
if model:
113+
yield model
114+
115+
# If include_auto_created is True, also yield through models
116+
if include_auto_created and hasattr(model, '_through_models'):
117+
for through_model in model._through_models:
118+
yield through_model
114119

115120

116121
config = CustomObjectsPluginConfig

netbox_custom_objects/api/serializers.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,16 @@ class Meta:
115115
"id",
116116
"url",
117117
"name",
118+
"verbose_name",
119+
"verbose_name_plural",
120+
"slug",
118121
"description",
119122
"tags",
120123
"created",
121124
"last_updated",
122125
"fields",
123126
]
124-
brief_fields = ("id", "url", "name", "description")
127+
brief_fields = ("id", "url", "name", "slug", "description")
125128

126129
def create(self, validated_data):
127130
return super().create(validated_data)
@@ -192,7 +195,7 @@ def get_url(self, obj):
192195
lookup_value = getattr(obj, "pk")
193196
kwargs = {
194197
"pk": lookup_value,
195-
"custom_object_type": obj.custom_object_type.name.lower(),
198+
"custom_object_type": obj.custom_object_type.slug,
196199
}
197200
request = self.context["request"]
198201
format = self.context.get("format")
@@ -203,7 +206,7 @@ def get_field_data(self, obj):
203206
return result
204207

205208

206-
def get_serializer_class(model):
209+
def get_serializer_class(model, skip_object_fields=False):
207210
model_fields = model.custom_object_type.fields.all()
208211

209212
# Create field list including all necessary fields
@@ -230,7 +233,7 @@ def get_url(self, obj):
230233
lookup_value = getattr(obj, "pk")
231234
kwargs = {
232235
"pk": lookup_value,
233-
"custom_object_type": obj.custom_object_type.name.lower(),
236+
"custom_object_type": obj.custom_object_type.slug,
234237
}
235238
request = self.context["request"]
236239
format = self.context.get("format")
@@ -251,6 +254,10 @@ def get_display(self, obj):
251254
}
252255

253256
for field in model_fields:
257+
if skip_object_fields and field.type in [
258+
CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT
259+
]:
260+
continue
254261
field_type = field_types.FIELD_TYPE_CLASS[field.type]()
255262
try:
256263
attrs[field.name] = field_type.get_serializer_field(field)

netbox_custom_objects/api/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get(self, request, *args, **kwargs):
5151
# Extra logic to populate roots for custom object type lists
5252
for custom_object_type in CustomObjectType.objects.all():
5353
local_kwargs = deepcopy(kwargs)
54-
cot_name = custom_object_type.name.lower()
54+
cot_name = custom_object_type.slug
5555
url_name = 'customobject-list'
5656
local_kwargs['custom_object_type'] = cot_name
5757
if namespace:

netbox_custom_objects/api/views.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.http import Http404
2+
from drf_spectacular.utils import extend_schema_view, extend_schema
23
from rest_framework.routers import APIRootView
34
from rest_framework.viewsets import ModelViewSet
45

@@ -18,13 +19,25 @@ class CustomObjectTypeViewSet(ModelViewSet):
1819
serializer_class = serializers.CustomObjectTypeSerializer
1920

2021

22+
# TODO: Need to remove this for now, check if work-around in the future.
23+
# There is a catch-22 spectacular get the queryset and serializer class without
24+
# params at startup. The suggested workaround is to return the model empty
25+
# queryset, but we can't get the model without params at startup.
26+
@extend_schema_view(
27+
list=extend_schema(exclude=True),
28+
retrieve=extend_schema(exclude=True),
29+
create=extend_schema(exclude=True),
30+
update=extend_schema(exclude=True),
31+
partial_update=extend_schema(exclude=True),
32+
destroy=extend_schema(exclude=True)
33+
)
2134
class CustomObjectViewSet(ModelViewSet):
2235
serializer_class = serializers.CustomObjectSerializer
2336
model = None
2437

2538
def get_view_name(self):
2639
if self.model:
27-
return self.model.custom_object_type.name
40+
return self.model.custom_object_type.display_name
2841
return 'Custom Object'
2942

3043
def get_serializer_class(self):
@@ -33,7 +46,7 @@ def get_serializer_class(self):
3346
def get_queryset(self):
3447
try:
3548
custom_object_type = CustomObjectType.objects.get(
36-
name__iexact=self.kwargs["custom_object_type"]
49+
slug=self.kwargs["custom_object_type"]
3750
)
3851
except CustomObjectType.DoesNotExist:
3952
raise Http404

0 commit comments

Comments
 (0)