2020-02-07 11:54:34 -05:00
from django . db import migrations
import utilities . fields
import utilities . ordering
def _update_model_names ( model ) :
# Update each unique field value in bulk
for name in model . objects . values_list ( ' name ' , flat = True ) . order_by ( ' name ' ) . distinct ( ) :
2020-02-13 21:41:00 -05:00
model . objects . filter ( name = name ) . update ( _name = utilities . ordering . naturalize ( name , max_length = 100 ) )
2020-02-07 11:54:34 -05:00
def naturalize_sites ( apps , schema_editor ) :
_update_model_names ( apps . get_model ( ' dcim ' , ' Site ' ) )
def naturalize_racks ( apps , schema_editor ) :
_update_model_names ( apps . get_model ( ' dcim ' , ' Rack ' ) )
def naturalize_devices ( apps , schema_editor ) :
_update_model_names ( apps . get_model ( ' dcim ' , ' Device ' ) )
class Migration ( migrations . Migration ) :
dependencies = [
( ' dcim ' , ' 0094_device_component_template_ordering ' ) ,
]
operations = [
migrations . AlterModelOptions (
name = ' device ' ,
options = { ' ordering ' : ( ' _name ' , ' pk ' ) , ' permissions ' : ( ( ' napalm_read ' , ' Read-only access to devices via NAPALM ' ) , ( ' napalm_write ' , ' Read/write access to devices via NAPALM ' ) ) } ,
) ,
migrations . AlterModelOptions (
name = ' rack ' ,
options = { ' ordering ' : ( ' site ' , ' group ' , ' _name ' , ' pk ' ) } ,
) ,
migrations . AlterModelOptions (
name = ' site ' ,
options = { ' ordering ' : ( ' _name ' , ) } ,
) ,
migrations . AddField (
model_name = ' device ' ,
name = ' _name ' ,
2020-02-07 12:24:38 -05:00
field = utilities . fields . NaturalOrderingField ( ' target_field ' , blank = True , max_length = 100 , naturalize_function = utilities . ordering . naturalize , null = True ) ,
2020-02-07 11:54:34 -05:00
) ,
migrations . AddField (
model_name = ' rack ' ,
name = ' _name ' ,
field = utilities . fields . NaturalOrderingField ( ' target_field ' , blank = True , max_length = 100 , naturalize_function = utilities . ordering . naturalize ) ,
) ,
migrations . AddField (
model_name = ' site ' ,
name = ' _name ' ,
field = utilities . fields . NaturalOrderingField ( ' target_field ' , blank = True , max_length = 100 , naturalize_function = utilities . ordering . naturalize ) ,
) ,
migrations . RunPython (
2020-02-07 12:24:38 -05:00
code = naturalize_sites ,
reverse_code = migrations . RunPython . noop
2020-02-07 11:54:34 -05:00
) ,
migrations . RunPython (
2020-02-07 12:24:38 -05:00
code = naturalize_racks ,
reverse_code = migrations . RunPython . noop
2020-02-07 11:54:34 -05:00
) ,
migrations . RunPython (
2020-02-07 12:24:38 -05:00
code = naturalize_devices ,
reverse_code = migrations . RunPython . noop
2020-02-07 11:54:34 -05:00
) ,
]