28
28
from . import field_types , filtersets , forms , tables
29
29
from .models import CustomObject , CustomObjectType , CustomObjectTypeField
30
30
from extras .choices import CustomFieldTypeChoices
31
+ from netbox_custom_objects .api .serializers import get_serializer_class
31
32
from netbox_custom_objects .constants import APP_LABEL
32
33
33
34
logger = logging .getLogger ("netbox_custom_objects.views" )
@@ -141,6 +142,14 @@ def get_table(self, data, request, bulk_actions=True):
141
142
return super ().get_table (data , request , bulk_actions = bulk_actions )
142
143
143
144
145
+ class CustomObjectModelMixin :
146
+ def get_model (self , custom_object_type ):
147
+ model = custom_object_type .get_model ()
148
+ get_serializer_class (model )
149
+ custom_object_type .register_custom_object_search_index (model )
150
+ return model
151
+
152
+
144
153
#
145
154
# Custom Object Types
146
155
#
@@ -155,17 +164,17 @@ class CustomObjectTypeListView(generic.ObjectListView):
155
164
156
165
157
166
@register_model_view (CustomObjectType )
158
- class CustomObjectTypeView (CustomObjectTableMixin , generic .ObjectView ):
167
+ class CustomObjectTypeView (CustomObjectTableMixin , CustomObjectModelMixin , generic .ObjectView ):
159
168
queryset = CustomObjectType .objects .all ()
160
169
161
170
def get_table (self , data , request , bulk_actions = True ):
162
171
self .custom_object_type = self .get_object (** self .kwargs )
163
- model = self .custom_object_type . get_model ()
172
+ model = self .get_model (self . custom_object_type )
164
173
data = model .objects .all ()
165
174
return super ().get_table (data , request , bulk_actions = False )
166
175
167
176
def get_extra_context (self , request , instance ):
168
- model = instance .get_model ()
177
+ model = self .get_model (instance )
169
178
170
179
# Get fields and group them by group_name
171
180
fields = instance .fields .all ().order_by ("group_name" , "weight" , "name" )
@@ -193,13 +202,13 @@ class CustomObjectTypeEditView(generic.ObjectEditView):
193
202
194
203
195
204
@register_model_view (CustomObjectType , "delete" )
196
- class CustomObjectTypeDeleteView (generic .ObjectDeleteView ):
205
+ class CustomObjectTypeDeleteView (CustomObjectModelMixin , generic .ObjectDeleteView ):
197
206
queryset = CustomObjectType .objects .all ()
198
207
default_return_url = "plugins:netbox_custom_objects:customobjecttype_list"
199
208
200
209
def _get_dependent_objects (self , obj ):
201
210
dependent_objects = super ()._get_dependent_objects (obj )
202
- model = obj .get_model ()
211
+ model = self .get_model (obj )
203
212
dependent_objects [model ] = list (model .objects .all ())
204
213
205
214
# Find CustomObjectTypeFields that reference this CustomObjectType
@@ -226,7 +235,7 @@ class CustomObjectTypeFieldEditView(generic.ObjectEditView):
226
235
227
236
228
237
@register_model_view (CustomObjectTypeField , "delete" )
229
- class CustomObjectTypeFieldDeleteView (generic .ObjectDeleteView ):
238
+ class CustomObjectTypeFieldDeleteView (CustomObjectModelMixin , generic .ObjectDeleteView ):
230
239
template_name = "netbox_custom_objects/field_delete.html"
231
240
queryset = CustomObjectTypeField .objects .all ()
232
241
@@ -243,7 +252,7 @@ def get(self, request, *args, **kwargs):
243
252
obj = self .get_object (** kwargs )
244
253
form = ConfirmationForm (initial = request .GET )
245
254
246
- model = obj . custom_object_type . get_model ()
255
+ model = self . get_model (obj . custom_object_type )
247
256
kwargs = {
248
257
f"{ obj .name } __isnull" : False ,
249
258
}
@@ -280,7 +289,7 @@ def get(self, request, *args, **kwargs):
280
289
281
290
def _get_dependent_objects (self , obj ):
282
291
dependent_objects = super ()._get_dependent_objects (obj )
283
- model = obj . custom_object_type . get_model ()
292
+ model = self . get_model (obj . custom_object_type )
284
293
kwargs = {
285
294
f"{ obj .name } __isnull" : False ,
286
295
}
@@ -314,7 +323,7 @@ class CustomObjectTypeBulkDeleteView(generic.BulkDeleteView):
314
323
#
315
324
316
325
317
- class CustomObjectListView (CustomObjectTableMixin , generic .ObjectListView ):
326
+ class CustomObjectListView (CustomObjectTableMixin , CustomObjectModelMixin , generic .ObjectListView ):
318
327
queryset = None
319
328
custom_object_type = None
320
329
template_name = "netbox_custom_objects/custom_object_list.html"
@@ -332,7 +341,7 @@ def get_queryset(self, request):
332
341
self .custom_object_type = get_object_or_404 (
333
342
CustomObjectType , slug = custom_object_type
334
343
)
335
- model = self .custom_object_type . get_model ()
344
+ model = self .get_model (self . custom_object_type )
336
345
return model .objects .all ()
337
346
338
347
def get_filterset (self ):
@@ -370,23 +379,23 @@ def get_extra_context(self, request):
370
379
371
380
372
381
@register_model_view (CustomObject )
373
- class CustomObjectView (generic .ObjectView ):
382
+ class CustomObjectView (CustomObjectModelMixin , generic .ObjectView ):
374
383
template_name = "netbox_custom_objects/customobject.html"
375
384
376
385
def get_queryset (self , request ):
377
386
custom_object_type = self .kwargs .get ("custom_object_type" , None )
378
387
object_type = get_object_or_404 (
379
388
CustomObjectType , slug = custom_object_type
380
389
)
381
- model = object_type .get_model ()
390
+ model = self .get_model (object_type )
382
391
return model .objects .all ()
383
392
384
393
def get_object (self , ** kwargs ):
385
394
custom_object_type = self .kwargs .get ("custom_object_type" , None )
386
395
object_type = get_object_or_404 (
387
396
CustomObjectType , slug = custom_object_type
388
397
)
389
- model = object_type .get_model ()
398
+ model = self .get_model (object_type )
390
399
# Filter out custom_object_type from kwargs for the object lookup
391
400
lookup_kwargs = {
392
401
k : v for k , v in self .kwargs .items () if k != "custom_object_type"
@@ -413,7 +422,7 @@ def get_extra_context(self, request, instance):
413
422
414
423
415
424
@register_model_view (CustomObject , "edit" )
416
- class CustomObjectEditView (generic .ObjectEditView ):
425
+ class CustomObjectEditView (CustomObjectModelMixin , generic .ObjectEditView ):
417
426
template_name = "netbox_custom_objects/customobject_edit.html"
418
427
form = None
419
428
queryset = None
@@ -436,7 +445,8 @@ def get_object(self, **kwargs):
436
445
object_type = get_object_or_404 (
437
446
CustomObjectType , slug = custom_object_type
438
447
)
439
- model = object_type .get_model ()
448
+ model = self .get_model (object_type )
449
+
440
450
if not self .kwargs .get ("pk" , None ):
441
451
# We're creating a new object
442
452
return model ()
@@ -573,7 +583,7 @@ def custom_save(self, commit=True):
573
583
574
584
575
585
@register_model_view (CustomObject , "delete" )
576
- class CustomObjectDeleteView (generic .ObjectDeleteView ):
586
+ class CustomObjectDeleteView (CustomObjectModelMixin , generic .ObjectDeleteView ):
577
587
queryset = None
578
588
object = None
579
589
default_return_url = "plugins:netbox_custom_objects:customobject_list"
@@ -593,7 +603,7 @@ def get_object(self, **kwargs):
593
603
object_type = get_object_or_404 (
594
604
CustomObjectType , slug = custom_object_type
595
605
)
596
- model = object_type .get_model ()
606
+ model = self .get_model (object_type )
597
607
return get_object_or_404 (model .objects .all (), ** self .kwargs )
598
608
599
609
def get_return_url (self , request , obj = None ):
@@ -614,7 +624,7 @@ def get_return_url(self, request, obj=None):
614
624
615
625
616
626
@register_model_view (CustomObject , "bulk_edit" , path = "edit" , detail = False )
617
- class CustomObjectBulkEditView (CustomObjectTableMixin , generic .BulkEditView ):
627
+ class CustomObjectBulkEditView (CustomObjectTableMixin , CustomObjectModelMixin , generic .BulkEditView ):
618
628
queryset = None
619
629
custom_object_type = None
620
630
table = None
@@ -633,7 +643,7 @@ def get_queryset(self, request):
633
643
self .custom_object_type = CustomObjectType .objects .get (
634
644
slug = custom_object_type
635
645
)
636
- model = self .custom_object_type . get_model ()
646
+ model = self .get_model (self . custom_object_type )
637
647
return model .objects .all ()
638
648
639
649
def get_form (self , queryset ):
@@ -673,7 +683,7 @@ def get_form(self, queryset):
673
683
674
684
675
685
@register_model_view (CustomObject , "bulk_delete" , path = "delete" , detail = False )
676
- class CustomObjectBulkDeleteView (CustomObjectTableMixin , generic .BulkDeleteView ):
686
+ class CustomObjectBulkDeleteView (CustomObjectTableMixin , CustomObjectModelMixin , generic .BulkDeleteView ):
677
687
queryset = None
678
688
custom_object_type = None
679
689
table = None
@@ -691,12 +701,12 @@ def get_queryset(self, request):
691
701
self .custom_object_type = CustomObjectType .objects .get (
692
702
slug = custom_object_type
693
703
)
694
- model = self .custom_object_type . get_model ()
704
+ model = self .get_model (self . custom_object_type )
695
705
return model .objects .all ()
696
706
697
707
698
708
@register_model_view (CustomObject , "bulk_import" , path = "import" , detail = False )
699
- class CustomObjectBulkImportView (generic .BulkImportView ):
709
+ class CustomObjectBulkImportView (CustomObjectModelMixin , generic .BulkImportView ):
700
710
queryset = None
701
711
model_form = None
702
712
@@ -720,7 +730,7 @@ def get_queryset(self, request):
720
730
self .custom_object_type = CustomObjectType .objects .get (
721
731
name__iexact = custom_object_type
722
732
)
723
- model = self .custom_object_type . get_model ()
733
+ model = self .get_model (self . custom_object_type )
724
734
return model .objects .all ()
725
735
726
736
def get_model_form (self , queryset ):
@@ -756,7 +766,7 @@ def get_model_form(self, queryset):
756
766
return form
757
767
758
768
759
- class CustomObjectJournalView (ConditionalLoginRequiredMixin , View ):
769
+ class CustomObjectJournalView (ConditionalLoginRequiredMixin , CustomObjectModelMixin , View ):
760
770
"""
761
771
Custom journal view for CustomObject instances.
762
772
Shows all journal entries for a custom object.
@@ -772,7 +782,7 @@ def get(self, request, custom_object_type, **kwargs):
772
782
object_type = get_object_or_404 (
773
783
CustomObjectType , slug = custom_object_type
774
784
)
775
- model = object_type .get_model ()
785
+ model = self .get_model (object_type )
776
786
777
787
# Get the specific object
778
788
lookup_kwargs = {k : v for k , v in kwargs .items () if k != "custom_object_type" }
@@ -828,7 +838,7 @@ def get(self, request, custom_object_type, **kwargs):
828
838
)
829
839
830
840
831
- class CustomObjectChangeLogView (ConditionalLoginRequiredMixin , View ):
841
+ class CustomObjectChangeLogView (ConditionalLoginRequiredMixin , CustomObjectModelMixin , View ):
832
842
"""
833
843
Custom changelog view for CustomObject instances.
834
844
Shows all changes made to a custom object.
@@ -844,7 +854,7 @@ def get(self, request, custom_object_type, **kwargs):
844
854
object_type = get_object_or_404 (
845
855
CustomObjectType , slug = custom_object_type
846
856
)
847
- model = object_type .get_model ()
857
+ model = self .get_model (object_type )
848
858
849
859
# Get the specific object
850
860
lookup_kwargs = {k : v for k , v in kwargs .items () if k != "custom_object_type" }
0 commit comments