Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8749 clone custom fields #10926

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion netbox/extras/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Meta:
model = CustomField
fields = [
'id', 'url', 'display', 'content_types', 'type', 'object_type', 'data_type', 'name', 'label', 'group_name',
'description', 'required', 'filter_logic', 'ui_visibility', 'default', 'weight', 'validation_minimum',
'description', 'required', 'filter_logic', 'ui_visibility', 'is_cloneable', 'default', 'weight', 'validation_minimum',
'validation_maximum', 'validation_regex', 'choices', 'created', 'last_updated',
]

Expand Down
4 changes: 4 additions & 0 deletions netbox/extras/forms/bulk_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class CustomFieldBulkEditForm(BulkEditForm):
initial='',
widget=StaticSelect()
)
is_cloneable = forms.NullBooleanField(
required=False,
widget=BulkEditNullBooleanSelect()
)

nullable_fields = ('group_name', 'description',)

Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Meta:
fields = (
'name', 'label', 'group_name', 'type', 'content_types', 'object_type', 'required', 'description', 'weight',
'filter_logic', 'default', 'choices', 'weight', 'validation_minimum', 'validation_maximum',
'validation_regex', 'ui_visibility',
'validation_regex', 'ui_visibility', 'is_cloneable',
)


Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CustomFieldForm(BootstrapMixin, forms.ModelForm):
('Custom Field', (
'content_types', 'name', 'label', 'group_name', 'type', 'object_type', 'weight', 'required', 'description',
)),
('Behavior', ('filter_logic', 'ui_visibility')),
('Behavior', ('filter_logic', 'ui_visibility', 'is_cloneable')),
('Values', ('default', 'choices')),
('Validation', ('validation_minimum', 'validation_maximum', 'validation_regex')),
)
Expand Down
18 changes: 18 additions & 0 deletions netbox/extras/migrations/0078_customfield_is_cloneable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.8 on 2022-11-15 18:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('extras', '0077_customlink_extend_text_and_url'),
]

operations = [
migrations.AddField(
model_name='customfield',
name='is_cloneable',
field=models.BooleanField(default=False),
),
]
7 changes: 6 additions & 1 deletion netbox/extras/models/customfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,17 @@ class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge
verbose_name='UI visibility',
help_text='Specifies the visibility of custom field in the UI'
)
is_cloneable = models.BooleanField(
default=False,
verbose_name='Cloneable',
help_text='If true, this field will be copied over when cloning objects.'
)

objects = CustomFieldManager()

clone_fields = (
'content_types', 'type', 'object_type', 'group_name', 'description', 'required', 'filter_logic', 'default',
'weight', 'validation_minimum', 'validation_maximum', 'validation_regex', 'choices', 'ui_visibility',
'weight', 'validation_minimum', 'validation_maximum', 'validation_regex', 'choices', 'ui_visibility', 'is_cloneable',
)

class Meta:
Expand Down
3 changes: 2 additions & 1 deletion netbox/extras/tables/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ class CustomFieldTable(NetBoxTable):
content_types = columns.ContentTypesColumn()
required = columns.BooleanColumn()
ui_visibility = columns.ChoiceFieldColumn(verbose_name="UI visibility")
is_cloneable = columns.BooleanColumn()

class Meta(NetBoxTable.Meta):
model = CustomField
fields = (
'pk', 'id', 'name', 'content_types', 'label', 'type', 'group_name', 'required', 'weight', 'default',
'description', 'filter_logic', 'ui_visibility', 'choices', 'created', 'last_updated',
'description', 'filter_logic', 'ui_visibility', 'is_cloneable', 'choices', 'created', 'last_updated',
)
default_columns = ('pk', 'name', 'content_types', 'label', 'group_name', 'type', 'required', 'description')

Expand Down
9 changes: 9 additions & 0 deletions netbox/netbox/models/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ def clone(self):
if is_taggable(self):
attrs['tags'] = [tag.pk for tag in self.tags.all()]

# check custom fields
if hasattr(self, 'custom_field_data'):
from extras.models import CustomField

for field in CustomField.objects.get_for_model(self):
if field.is_cloneable:
value = self.custom_field_data.get(field.name)
attrs[f'cf_{field.name}'] = field.deserialize(value)

return attrs


Expand Down
4 changes: 4 additions & 0 deletions netbox/templates/extras/customfield.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ <h5 class="card-header">
<th scope="row">UI Visibility</th>
<td>{{ object.get_ui_visibility_display }}</td>
</tr>
<tr>
<th scope="row">Cloneable</th>
<td>{% checkmark object.is_cloneable %}</td>
</tr>
</table>
</div>
</div>
Expand Down