mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 20:12:00 -06:00
Initial work on reimplementing custom fields
This commit is contained in:
parent
ec66e1a5c0
commit
879166d939
23
netbox/circuits/migrations/0020_custom_field_data.py
Normal file
23
netbox/circuits/migrations/0020_custom_field_data.py
Normal file
@ -0,0 +1,23 @@
|
||||
# Generated by Django 3.1 on 2020-08-21 18:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('circuits', '0019_nullbooleanfield_to_booleanfield'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='circuit',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='provider',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
]
|
38
netbox/dcim/migrations/0115_custom_field_data.py
Normal file
38
netbox/dcim/migrations/0115_custom_field_data.py
Normal file
@ -0,0 +1,38 @@
|
||||
# Generated by Django 3.1 on 2020-08-21 18:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('dcim', '0114_update_jsonfield'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='device',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='devicetype',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='powerfeed',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='rack',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='site',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
]
|
50
netbox/extras/migrations/0050_migrate_customfieldvalues.py
Normal file
50
netbox/extras/migrations/0050_migrate_customfieldvalues.py
Normal file
@ -0,0 +1,50 @@
|
||||
from django.db import migrations
|
||||
|
||||
from extras.choices import CustomFieldTypeChoices
|
||||
|
||||
|
||||
def deserialize_value(field_type, value):
|
||||
"""
|
||||
Convert serialized values to JSON equivalents.
|
||||
"""
|
||||
if field_type in (CustomFieldTypeChoices.TYPE_INTEGER, CustomFieldTypeChoices.TYPE_SELECT):
|
||||
return int(value)
|
||||
if field_type == CustomFieldTypeChoices.TYPE_BOOLEAN:
|
||||
return bool(int(value))
|
||||
return value
|
||||
|
||||
|
||||
def migrate_customfieldvalues(apps, schema_editor):
|
||||
CustomFieldValue = apps.get_model('extras', 'CustomFieldValue')
|
||||
|
||||
for cfv in CustomFieldValue.objects.prefetch_related('field').exclude(serialized_value=''):
|
||||
model = apps.get_model(cfv.obj_type.app_label, cfv.obj_type.model)
|
||||
|
||||
# Read and update custom field value for each instance
|
||||
# TODO: This can be done more efficiently once .update() is supported for JSON fields
|
||||
cf_data = model.objects.filter(pk=cfv.obj_id).values('custom_field_data').first()
|
||||
try:
|
||||
cf_data['custom_field_data'][cfv.field.name] = deserialize_value(cfv.field.type, cfv.serialized_value)
|
||||
except ValueError as e:
|
||||
print(f'{cfv.field.name} ({cfv.field.type}): {cfv.serialized_value} ({cfv.pk})')
|
||||
raise e
|
||||
model.objects.filter(pk=cfv.obj_id).update(**cf_data)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('circuits', '0020_custom_field_data'),
|
||||
('dcim', '0115_custom_field_data'),
|
||||
('extras', '0049_remove_graph'),
|
||||
('ipam', '0038_custom_field_data'),
|
||||
('secrets', '0010_custom_field_data'),
|
||||
('tenancy', '0010_custom_field_data'),
|
||||
('virtualization', '0018_custom_field_data'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
code=migrate_customfieldvalues
|
||||
),
|
||||
]
|
@ -17,6 +17,10 @@ from extras.utils import FeatureQuery
|
||||
#
|
||||
|
||||
class CustomFieldModel(models.Model):
|
||||
custom_field_data = models.JSONField(
|
||||
blank=True,
|
||||
default=dict
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
43
netbox/ipam/migrations/0038_custom_field_data.py
Normal file
43
netbox/ipam/migrations/0038_custom_field_data.py
Normal file
@ -0,0 +1,43 @@
|
||||
# Generated by Django 3.1 on 2020-08-21 18:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ipam', '0037_ipaddress_assignment'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='aggregate',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='ipaddress',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='prefix',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='service',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='vlan',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='vrf',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
]
|
18
netbox/secrets/migrations/0010_custom_field_data.py
Normal file
18
netbox/secrets/migrations/0010_custom_field_data.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.1 on 2020-08-21 18:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('secrets', '0009_secretrole_drop_users_groups'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='secret',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
]
|
18
netbox/tenancy/migrations/0010_custom_field_data.py
Normal file
18
netbox/tenancy/migrations/0010_custom_field_data.py
Normal file
@ -0,0 +1,18 @@
|
||||
# Generated by Django 3.1 on 2020-08-21 18:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('tenancy', '0009_standardize_description'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='tenant',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
]
|
23
netbox/virtualization/migrations/0018_custom_field_data.py
Normal file
23
netbox/virtualization/migrations/0018_custom_field_data.py
Normal file
@ -0,0 +1,23 @@
|
||||
# Generated by Django 3.1 on 2020-08-21 18:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('virtualization', '0017_update_jsonfield'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='cluster',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='virtualmachine',
|
||||
name='custom_field_data',
|
||||
field=models.JSONField(blank=True, default=dict),
|
||||
),
|
||||
]
|
Loading…
Reference in New Issue
Block a user