Skip to content

Commit f471475

Browse files
committed
Ruff fixes
1 parent bc12047 commit f471475

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

netbox_custom_objects/field_types.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class LazyForeignKey(ForeignKey):
3535

3636
def __init__(self, to_model_name, *args, **kwargs):
3737
self._to_model_name = to_model_name
38-
38+
3939
# Filter out our custom parameters before passing to Django's ForeignKey
4040
field_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_') and k != 'generating_models'}
41-
41+
4242
super().__init__(to_model_name, *args, **field_kwargs)
4343

4444
def contribute_to_class(self, cls, name, **kwargs):
@@ -84,7 +84,7 @@ def _safe_kwargs(self, **kwargs):
8484
Create a safe kwargs dict that can be passed to Django field constructors.
8585
This method automatically filters out any custom parameters.
8686
"""
87-
return {k: v for k, v in kwargs.items()
87+
return {k: v for k, v in kwargs.items()
8888
if not k.startswith('_') and k != 'generating_models'}
8989

9090
def get_annotated_form_field(self, field, enforce_visibility=True, **kwargs):
@@ -93,7 +93,7 @@ def get_annotated_form_field(self, field, enforce_visibility=True, **kwargs):
9393
form_field.label = str(field)
9494
# Set the field name so Django can properly bind it to the instance
9595
form_field.name = field.name
96-
96+
9797
if field.description:
9898
form_field.help_text = render_markdown(field.description)
9999

@@ -387,7 +387,7 @@ class ObjectFieldType(FieldType):
387387
def get_model_field(self, field, **kwargs):
388388
content_type = ContentType.objects.get(pk=field.related_object_type_id)
389389
to_model = content_type.model
390-
390+
391391
# Extract our custom parameters and keep only Django field parameters
392392
generating_models = kwargs.pop('_generating_models', getattr(self, '_generating_models', set()))
393393
field_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
@@ -401,16 +401,16 @@ def get_model_field(self, field, **kwargs):
401401
"model", ""
402402
)
403403
custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
404-
404+
405405
# Check if this is a self-referential field
406406
if custom_object_type.id == field.custom_object_type.id:
407407
# For self-referential fields, use LazyForeignKey to defer resolution
408408
model_name = f"{APP_LABEL}.{custom_object_type.get_table_model_name(custom_object_type.id)}"
409409
f = LazyForeignKey(
410410
model_name,
411-
null=True,
412-
blank=True,
413-
on_delete=models.CASCADE,
411+
null=True,
412+
blank=True,
413+
on_delete=models.CASCADE,
414414
**field_kwargs
415415
)
416416
return f
@@ -423,9 +423,9 @@ def get_model_field(self, field, **kwargs):
423423
model_name = f"{APP_LABEL}.{custom_object_type.get_table_model_name(custom_object_type.id)}"
424424
f = models.ForeignKey(
425425
model_name,
426-
null=True,
427-
blank=True,
428-
on_delete=models.CASCADE,
426+
null=True,
427+
blank=True,
428+
on_delete=models.CASCADE,
429429
**field_kwargs
430430
)
431431
return f
@@ -435,11 +435,11 @@ def get_model_field(self, field, **kwargs):
435435
# to_model = content_type.model_class()._meta.object_name
436436
to_ct = f"{content_type.app_label}.{to_model}"
437437
model = apps.get_model(to_ct)
438-
438+
439439
f = models.ForeignKey(
440440
model, null=True, blank=True, on_delete=models.CASCADE, **field_kwargs
441441
)
442-
442+
443443
return f
444444

445445
def get_form_field(self, field, for_csv_import=False, **kwargs):
@@ -458,7 +458,7 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
458458
"model", ""
459459
)
460460
custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
461-
461+
462462
# Check if we're in a recursion situation
463463
generating_models = getattr(self, '_generating_models', set())
464464
if generating_models and custom_object_type.id in generating_models:
@@ -737,7 +737,7 @@ def get_model_field(self, field, **kwargs):
737737
custom_object_type_id = content_type.model.replace("table", "").replace(
738738
"model", ""
739739
)
740-
740+
741741
# Extract our custom parameters and keep only Django field parameters
742742
generating_models = kwargs.pop('_generating_models', getattr(self, '_generating_models', set()))
743743
field_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
@@ -860,16 +860,16 @@ def after_model_generation(self, instance, model, field_name):
860860
# Update both source and target fields to point to the same model
861861
source_field = through_model._meta.get_field("source")
862862
target_field = through_model._meta.get_field("target")
863-
863+
864864
# Resolve the foreign key fields to point to the actual model
865865
source_field.remote_field.model = model
866866
source_field.related_model = model
867867
target_field.remote_field.model = model
868868
target_field.related_model = model
869-
869+
870870
# Also update the field's to attribute to point to the actual model
871871
field.to = model
872-
872+
873873
return
874874

875875
# For non-self-referential fields, we need to resolve the target model
@@ -884,7 +884,7 @@ def after_model_generation(self, instance, model, field_name):
884884
"model", ""
885885
)
886886
custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
887-
887+
888888
# For self-referential fields, we need to resolve them to the current model
889889
# This doesn't cause recursion because we're not calling get_model() again
890890
if custom_object_type.id == instance.custom_object_type.id:
@@ -941,7 +941,7 @@ def create_m2m_table(self, instance, model, field_name):
941941
custom_object_type = CustomObjectType.objects.get(
942942
pk=custom_object_type_id
943943
)
944-
944+
945945
# Check if we're in a recursion situation
946946
generating_models = getattr(self, '_generating_models', set())
947947
if generating_models and custom_object_type.id in generating_models:

netbox_custom_objects/models.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def clear_model_cache(cls, custom_object_type_id=None):
220220

221221
# Clear Django apps registry cache to ensure newly created models are recognized
222222
apps.get_models.cache_clear()
223-
223+
224224
# Clear global recursion tracking when clearing cache
225225
cls.clear_global_recursion_tracking()
226226

@@ -355,15 +355,15 @@ def _fetch_and_generate_field_attrs(
355355

356356
# Pass generating models set to field generation to prevent infinite loops
357357
field_type._generating_models = generating_models
358-
358+
359359
# Check if we're in a recursion situation before generating the field
360360
# Use depth-based recursion control: allow self-referential fields at level 0, skip at deeper levels
361361
should_skip = False
362-
362+
363363
# Calculate depth correctly: depth 0 is when we're generating the main model
364364
# depth 1+ is when we're generating related models recursively
365365
current_depth = len(generating_models) - 1 if generating_models else 0
366-
366+
367367
if field.type in [CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT]:
368368
if field.related_object_type:
369369
# Check if this field references the same CustomObjectType (self-referential)
@@ -375,13 +375,13 @@ def _fetch_and_generate_field_attrs(
375375
# Extract the custom object type ID from the model name
376376
# The model name format is "table{id}model" or similar
377377
model_name = content_type.model
378-
378+
379379
# Try to extract the ID from the model name
380380
import re
381381
id_match = re.search(r'table(\d+)model', model_name, re.IGNORECASE)
382382
if id_match:
383383
custom_object_type_id = int(id_match.group(1))
384-
384+
385385
if custom_object_type_id == self.id:
386386
# This is a self-referential field
387387
if current_depth == 0:
@@ -408,17 +408,17 @@ def _fetch_and_generate_field_attrs(
408408
else:
409409
# Not object/multiobject type
410410
pass
411-
411+
412412
if should_skip:
413413
# Skip this field to prevent further recursion
414414
field_attrs["_skipped_fields"].add(field.name)
415415
continue
416-
416+
417417
field_attrs[field.name] = field_type.get_model_field(
418418
field,
419419
_generating_models=generating_models, # Pass as prefixed parameter
420420
)
421-
421+
422422
# Add to field objects only if the field was successfully generated
423423
field_attrs["_field_objects"][field.id] = {
424424
"field": field,
@@ -436,17 +436,17 @@ def _after_model_generation(self, attrs, model):
436436
all_field_objects = {}
437437
all_field_objects.update(attrs["_field_objects"])
438438
all_field_objects.update(attrs["_trashed_field_objects"])
439-
439+
440440
# Get the set of fields that were skipped due to recursion
441441
skipped_fields = attrs.get("_skipped_fields", set())
442-
442+
443443
for field_object in all_field_objects.values():
444444
field_name = field_object["name"]
445-
445+
446446
# Skip fields that were skipped due to recursion
447447
if field_name in skipped_fields:
448448
continue
449-
449+
450450
# Only process fields that actually exist on the model
451451
# Fields might be skipped due to recursion prevention
452452
if hasattr(model._meta, 'get_field'):
@@ -456,7 +456,7 @@ def _after_model_generation(self, attrs, model):
456456
field_object["type"].after_model_generation(
457457
field_object["field"], model, field_name
458458
)
459-
except Exception as e:
459+
except Exception:
460460
# Field doesn't exist (likely skipped due to recursion), skip processing
461461
continue
462462

@@ -553,19 +553,19 @@ def get_model(
553553
# Circular reference detection using class-level tracking
554554
if not hasattr(CustomObjectType, '_global_generating_models'):
555555
CustomObjectType._global_generating_models = set()
556-
556+
557557
if _generating_models is None:
558558
_generating_models = CustomObjectType._global_generating_models
559559
# Check if we're already generating this model (circular reference)
560560
if self.id in _generating_models:
561561
# We have a circular reference, return a minimal model to break the cycle
562562
return self._get_minimal_model(_generating_models)
563-
563+
564564
# Check recursion depth to prevent infinite loops
565565
if len(_generating_models) > 1:
566566
# We're too deep in recursion, return a minimal model
567567
return self._get_minimal_model(_generating_models)
568-
568+
569569
# Add this model to the set of models being generated
570570
_generating_models.add(self.id)
571571

@@ -604,13 +604,13 @@ def get_model(
604604

605605
# Pass the generating models set to field generation
606606
field_attrs = self._fetch_and_generate_field_attrs(
607-
fields,
607+
fields,
608608
skip_object_fields=skip_object_fields,
609609
generating_models=_generating_models
610610
)
611611

612612
attrs.update(**field_attrs)
613-
613+
614614
# Track which fields were skipped due to recursion for after_model_generation
615615
if '_skipped_fields' not in attrs:
616616
attrs['_skipped_fields'] = set()
@@ -690,7 +690,7 @@ def _get_minimal_model(self, _generating_models=None):
690690
This breaks infinite recursion cycles by providing a basic model structure.
691691
"""
692692
model_name = self.get_table_model_name(self.pk)
693-
693+
694694
meta = type(
695695
"Meta",
696696
(),

netbox_custom_objects/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,29 +525,29 @@ def custom_init(self, *args, **kwargs):
525525
def custom_save(self, commit=True):
526526
# First save the instance to get the primary key
527527
instance = forms.NetBoxModelForm.save(self, commit=False)
528-
528+
529529
if commit:
530530
instance.save()
531-
531+
532532
# Handle M2M fields manually to ensure proper clearing and setting
533533
for field_name, field_obj in self.custom_object_type_fields.items():
534534
if field_obj.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
535535
# Get the current value from the form
536536
current_value = self.cleaned_data.get(field_name, [])
537-
537+
538538
# Get the field from the instance
539539
instance_field = getattr(instance, field_name)
540-
540+
541541
# Clear existing relationships and set new ones
542542
if hasattr(instance_field, 'clear') and hasattr(instance_field, 'set'):
543543
instance_field.clear()
544-
544+
545545
if current_value:
546546
instance_field.set(current_value)
547-
547+
548548
# Save M2M relationships
549549
self.save_m2m()
550-
550+
551551
return instance
552552

553553
form_class.__init__ = custom_init

0 commit comments

Comments
 (0)