2
0

0048_prefix_populate_depth_children.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from django.db import migrations
  2. from ipam.utils import rebuild_prefixes
  3. def push_to_stack(stack, prefix):
  4. # Increment child count on parent nodes
  5. for n in stack:
  6. n['children'] += 1
  7. stack.append({
  8. 'pk': prefix['pk'],
  9. 'prefix': prefix['prefix'],
  10. 'children': 0,
  11. })
  12. def populate_prefix_hierarchy(apps, schema_editor):
  13. """
  14. Populate _depth and _children attrs for all Prefixes.
  15. """
  16. Prefix = apps.get_model('ipam', 'Prefix')
  17. VRF = apps.get_model('ipam', 'VRF')
  18. total_count = Prefix.objects.count()
  19. print(f'\nUpdating {total_count} prefixes...')
  20. # Rebuild the global table
  21. rebuild_prefixes(None)
  22. # Iterate through all VRFs, rebuilding each
  23. for vrf in VRF.objects.all():
  24. rebuild_prefixes(vrf)
  25. class Migration(migrations.Migration):
  26. dependencies = [
  27. ('ipam', '0047_prefix_depth_children'),
  28. ]
  29. operations = [
  30. migrations.RunPython(
  31. code=populate_prefix_hierarchy,
  32. reverse_code=migrations.RunPython.noop
  33. ),
  34. ]