mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Remove unused attributes, methods
This commit is contained in:
parent
f7b8d6ede5
commit
fb8904af54
@ -25,34 +25,23 @@ class CustomFieldModelForm(forms.ModelForm):
|
|||||||
|
|
||||||
self.obj_type = ContentType.objects.get_for_model(self._meta.model)
|
self.obj_type = ContentType.objects.get_for_model(self._meta.model)
|
||||||
self.custom_fields = []
|
self.custom_fields = []
|
||||||
self.custom_field_values = {}
|
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
if self.instance._cf is None:
|
|
||||||
self.instance._cf = {}
|
|
||||||
|
|
||||||
self._append_customfield_fields()
|
self._append_customfield_fields()
|
||||||
|
|
||||||
def _append_customfield_fields(self):
|
def _append_customfield_fields(self):
|
||||||
"""
|
"""
|
||||||
Append form fields for all CustomFields assigned to this model.
|
Append form fields for all CustomFields assigned to this model.
|
||||||
"""
|
"""
|
||||||
# Retrieve initial CustomField values for the instance
|
|
||||||
if self.instance.pk:
|
|
||||||
self.custom_field_values = self.instance.custom_field_data
|
|
||||||
|
|
||||||
# Append form fields; assign initial values if modifying and existing object
|
# Append form fields; assign initial values if modifying and existing object
|
||||||
for cf in CustomField.objects.filter(obj_type=self.obj_type):
|
for cf in CustomField.objects.filter(obj_type=self.obj_type):
|
||||||
field_name = 'cf_{}'.format(cf.name)
|
field_name = 'cf_{}'.format(cf.name)
|
||||||
if self.instance.pk:
|
if self.instance.pk:
|
||||||
self.fields[field_name] = cf.to_form_field(set_initial=False)
|
self.fields[field_name] = cf.to_form_field(set_initial=False)
|
||||||
value = self.custom_field_values.get(cf.name)
|
self.fields[field_name].initial = self.instance.custom_field_data.get(cf.name)
|
||||||
self.fields[field_name].initial = value
|
|
||||||
self.instance._cf[cf.name] = value
|
|
||||||
else:
|
else:
|
||||||
self.fields[field_name] = cf.to_form_field()
|
self.fields[field_name] = cf.to_form_field()
|
||||||
self.instance._cf[cf.name] = self.fields[field_name].initial
|
|
||||||
|
|
||||||
# Annotate the field in the list of CustomField form fields
|
# Annotate the field in the list of CustomField form fields
|
||||||
self.custom_fields.append(field_name)
|
self.custom_fields.append(field_name)
|
||||||
|
@ -24,10 +24,6 @@ class CustomFieldModel(models.Model):
|
|||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
def __init__(self, *args, custom_fields=None, **kwargs):
|
|
||||||
self._cf = custom_fields
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cf(self):
|
def cf(self):
|
||||||
"""
|
"""
|
||||||
@ -132,42 +128,6 @@ class CustomField(models.Model):
|
|||||||
'default': f"The specified default value ({self.default}) is not listed as an available choice."
|
'default': f"The specified default value ({self.default}) is not listed as an available choice."
|
||||||
})
|
})
|
||||||
|
|
||||||
def serialize_value(self, value):
|
|
||||||
"""
|
|
||||||
Serialize the given value to a string suitable for storage as a CustomFieldValue
|
|
||||||
"""
|
|
||||||
if value is None:
|
|
||||||
return ''
|
|
||||||
if self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
|
|
||||||
return str(int(bool(value)))
|
|
||||||
if self.type == CustomFieldTypeChoices.TYPE_DATE:
|
|
||||||
# Could be date/datetime object or string
|
|
||||||
try:
|
|
||||||
return value.strftime('%Y-%m-%d')
|
|
||||||
except AttributeError:
|
|
||||||
return value
|
|
||||||
if self.type == CustomFieldTypeChoices.TYPE_SELECT:
|
|
||||||
# Could be ModelChoiceField or TypedChoiceField
|
|
||||||
return str(value.id) if hasattr(value, 'id') else str(value)
|
|
||||||
return value
|
|
||||||
|
|
||||||
def deserialize_value(self, serialized_value):
|
|
||||||
"""
|
|
||||||
Convert a string into the object it represents depending on the type of field
|
|
||||||
"""
|
|
||||||
if serialized_value == '':
|
|
||||||
return None
|
|
||||||
if self.type == CustomFieldTypeChoices.TYPE_INTEGER:
|
|
||||||
return int(serialized_value)
|
|
||||||
if self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
|
|
||||||
return bool(int(serialized_value))
|
|
||||||
if self.type == CustomFieldTypeChoices.TYPE_DATE:
|
|
||||||
# Read date as YYYY-MM-DD
|
|
||||||
return date(*[int(n) for n in serialized_value.split('-')])
|
|
||||||
if self.type == CustomFieldTypeChoices.TYPE_SELECT:
|
|
||||||
return self.choices.get(pk=int(serialized_value))
|
|
||||||
return serialized_value
|
|
||||||
|
|
||||||
def to_form_field(self, set_initial=True, enforce_required=True, for_csv_import=False):
|
def to_form_field(self, set_initial=True, enforce_required=True, for_csv_import=False):
|
||||||
"""
|
"""
|
||||||
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.
|
||||||
@ -229,7 +189,7 @@ class CustomField(models.Model):
|
|||||||
field = forms.CharField(max_length=255, required=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 = str(self)
|
||||||
if self.description:
|
if self.description:
|
||||||
field.help_text = self.description
|
field.help_text = self.description
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user