mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 17:08:41 -06:00
Remove get_custom_fields_for_model()
This commit is contained in:
parent
585ea71d1a
commit
c3f86456d6
@ -20,22 +20,6 @@ from .models import ConfigContext, CustomField, CustomFieldValue, ImageAttachmen
|
|||||||
# Custom fields
|
# Custom fields
|
||||||
#
|
#
|
||||||
|
|
||||||
def get_custom_fields_for_model(content_type, filterable_only=False, bulk_edit=False):
|
|
||||||
"""
|
|
||||||
Retrieve all CustomFields applicable to the given ContentType
|
|
||||||
"""
|
|
||||||
field_dict = OrderedDict()
|
|
||||||
custom_fields = CustomField.objects.filter(obj_type=content_type)
|
|
||||||
if filterable_only:
|
|
||||||
custom_fields = custom_fields.exclude(filter_logic=CustomFieldFilterLogicChoices.FILTER_DISABLED)
|
|
||||||
|
|
||||||
for cf in custom_fields:
|
|
||||||
field_name = 'cf_{}'.format(str(cf.name))
|
|
||||||
field_dict[field_name] = cf.to_form_field(set_initial=not bulk_edit)
|
|
||||||
|
|
||||||
return field_dict
|
|
||||||
|
|
||||||
|
|
||||||
class CustomFieldModelForm(forms.ModelForm):
|
class CustomFieldModelForm(forms.ModelForm):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@ -60,9 +44,10 @@ class CustomFieldModelForm(forms.ModelForm):
|
|||||||
Append form fields for all applicable CustomFields.
|
Append form fields for all applicable CustomFields.
|
||||||
"""
|
"""
|
||||||
self.custom_fields = []
|
self.custom_fields = []
|
||||||
for name, field in get_custom_fields_for_model(self.obj_type).items():
|
custom_fields = CustomField.objects.filter(obj_type=self.obj_type)
|
||||||
self.fields[name] = field
|
for cf in custom_fields:
|
||||||
self.custom_fields.append(name)
|
self.fields[cf.name] = cf.to_form_field()
|
||||||
|
self.custom_fields.append(cf.name)
|
||||||
|
|
||||||
def _save_custom_fields(self):
|
def _save_custom_fields(self):
|
||||||
|
|
||||||
@ -106,15 +91,14 @@ class CustomFieldBulkEditForm(BulkEditForm):
|
|||||||
self.obj_type = ContentType.objects.get_for_model(self.model)
|
self.obj_type = ContentType.objects.get_for_model(self.model)
|
||||||
|
|
||||||
# Add all applicable CustomFields to the form
|
# Add all applicable CustomFields to the form
|
||||||
custom_fields = get_custom_fields_for_model(self.obj_type, bulk_edit=True).items()
|
custom_fields = CustomField.objects.filter(obj_type=self.obj_type)
|
||||||
for name, field in custom_fields:
|
for cf in custom_fields:
|
||||||
# Annotate non-required custom fields as nullable
|
# Annotate non-required custom fields as nullable
|
||||||
if not field.required:
|
if not cf.required:
|
||||||
self.nullable_fields.append(name)
|
self.nullable_fields.append(cf.name)
|
||||||
field.required = False
|
self.fields[cf.name] = cf.to_form_field(set_initial=False, enforce_required=False)
|
||||||
self.fields[name] = field
|
|
||||||
# Annotate this as a custom field
|
# Annotate this as a custom field
|
||||||
self.custom_fields.append(name)
|
self.custom_fields.append(cf.name)
|
||||||
|
|
||||||
|
|
||||||
class CustomFieldFilterForm(forms.Form):
|
class CustomFieldFilterForm(forms.Form):
|
||||||
@ -126,10 +110,11 @@ class CustomFieldFilterForm(forms.Form):
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
# Add all applicable CustomFields to the form
|
# Add all applicable CustomFields to the form
|
||||||
custom_fields = get_custom_fields_for_model(self.obj_type, filterable_only=True).items()
|
custom_fields = CustomField.objects.filter(obj_type=self.obj_type).exclude(
|
||||||
for name, field in custom_fields:
|
filter_logic=CustomFieldFilterLogicChoices.FILTER_DISABLED
|
||||||
field.required = False
|
)
|
||||||
self.fields[name] = field
|
for cf in custom_fields:
|
||||||
|
self.fields[cf.name] = cf.to_form_field(set_initial=True, enforce_required=False)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -282,17 +282,19 @@ class CustomField(models.Model):
|
|||||||
return self.choices.get(pk=int(serialized_value))
|
return self.choices.get(pk=int(serialized_value))
|
||||||
return serialized_value
|
return serialized_value
|
||||||
|
|
||||||
def to_form_field(self, set_initial=True):
|
def to_form_field(self, set_initial=True, enforce_required=True):
|
||||||
"""
|
"""
|
||||||
Return a form field suitable for setting a CustomField's value for an object.
|
Return a form field suitable for setting a CustomField's value for an object.
|
||||||
|
|
||||||
set_initial: Set initial date for the field. This should be false when generating a field for bulk editing.
|
set_initial: Set initial date for the field. This should be False when generating a field for bulk editing.
|
||||||
|
enforce_required: Honor the value of CustomField.required. Set to False for filtering/bulk editing.
|
||||||
"""
|
"""
|
||||||
initial = self.default if set_initial else None
|
initial = self.default if set_initial else None
|
||||||
|
required = self.required if enforce_required else False
|
||||||
|
|
||||||
# Integer
|
# Integer
|
||||||
if self.type == CustomFieldTypeChoices.TYPE_INTEGER:
|
if self.type == CustomFieldTypeChoices.TYPE_INTEGER:
|
||||||
field = forms.IntegerField(required=self.required, initial=initial)
|
field = forms.IntegerField(required=required, initial=initial)
|
||||||
|
|
||||||
# Boolean
|
# Boolean
|
||||||
elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
|
elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
|
||||||
@ -308,19 +310,19 @@ class CustomField(models.Model):
|
|||||||
else:
|
else:
|
||||||
initial = None
|
initial = None
|
||||||
field = forms.NullBooleanField(
|
field = forms.NullBooleanField(
|
||||||
required=self.required, initial=initial, widget=StaticSelect2(choices=choices)
|
required=required, initial=initial, widget=StaticSelect2(choices=choices)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Date
|
# Date
|
||||||
elif self.type == CustomFieldTypeChoices.TYPE_DATE:
|
elif self.type == CustomFieldTypeChoices.TYPE_DATE:
|
||||||
field = forms.DateField(required=self.required, initial=initial, widget=DatePicker())
|
field = forms.DateField(required=required, initial=initial, widget=DatePicker())
|
||||||
|
|
||||||
# Select
|
# Select
|
||||||
elif self.type == CustomFieldTypeChoices.TYPE_SELECT:
|
elif self.type == CustomFieldTypeChoices.TYPE_SELECT:
|
||||||
choices = [(cfc.pk, cfc.value) for cfc in self.choices.all()]
|
choices = [(cfc.pk, cfc.value) for cfc in self.choices.all()]
|
||||||
# TODO: Accommodate bulk edit/filtering
|
# TODO: Accommodate bulk edit/filtering
|
||||||
# if not self.required or bulk_edit or filterable_only:
|
# if not self.required or bulk_edit or filterable_only:
|
||||||
if not self.required:
|
if not required:
|
||||||
choices = add_blank_choice(choices)
|
choices = add_blank_choice(choices)
|
||||||
# Set the initial value to the PK of the default choice, if any
|
# Set the initial value to the PK of the default choice, if any
|
||||||
if set_initial:
|
if set_initial:
|
||||||
@ -328,16 +330,16 @@ class CustomField(models.Model):
|
|||||||
if default_choice:
|
if default_choice:
|
||||||
initial = default_choice.pk
|
initial = default_choice.pk
|
||||||
field = forms.TypedChoiceField(
|
field = forms.TypedChoiceField(
|
||||||
choices=choices, coerce=int, required=self.required, initial=initial, widget=StaticSelect2()
|
choices=choices, coerce=int, required=required, initial=initial, widget=StaticSelect2()
|
||||||
)
|
)
|
||||||
|
|
||||||
# URL
|
# URL
|
||||||
elif self.type == CustomFieldTypeChoices.TYPE_URL:
|
elif self.type == CustomFieldTypeChoices.TYPE_URL:
|
||||||
field = LaxURLField(required=self.required, initial=initial)
|
field = LaxURLField(required=required, initial=initial)
|
||||||
|
|
||||||
# Text
|
# Text
|
||||||
else:
|
else:
|
||||||
field = forms.CharField(max_length=255, required=self.required, initial=initial)
|
field = forms.CharField(max_length=255, required=required, initial=initial)
|
||||||
|
|
||||||
field.model = self
|
field.model = self
|
||||||
field.label = self.label if self.label else self.name.replace('_', ' ').capitalize()
|
field.label = self.label if self.label else self.name.replace('_', ' ').capitalize()
|
||||||
|
Loading…
Reference in New Issue
Block a user