mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-22 05:12:22 -06:00
70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
# Generated by Django 5.0.9 on 2025-02-20 16:49
|
|
|
|
import sys
|
|
import time
|
|
|
|
import django.db.models.deletion
|
|
from django.db import migrations, models
|
|
|
|
|
|
def draw_progress(count, total, length=20):
|
|
progress = count / total
|
|
percent = int(progress * 100)
|
|
bar = int(progress * length)
|
|
sys.stdout.write('\r')
|
|
sys.stdout.write(f"[{'=' * bar:{length}s}] {percent}%")
|
|
sys.stdout.flush()
|
|
|
|
|
|
def set_prefix(apps, schema_editor):
|
|
start = time.time()
|
|
IPAddress = apps.get_model('ipam', 'IPAddress')
|
|
Prefix = apps.get_model('ipam', 'Prefix')
|
|
|
|
addresses = IPAddress.objects.all()
|
|
i = 0
|
|
total = addresses.count()
|
|
if total > 0:
|
|
print('\r\n')
|
|
draw_progress(i, total, 50)
|
|
for ip in addresses:
|
|
i += 1
|
|
prefixes = Prefix.objects.filter(
|
|
vrf=ip.vrf,
|
|
prefix__net_contains_or_equals=str(ip.address.ip),
|
|
prefix__net_mask_length__lte=ip.address.prefixlen,
|
|
)
|
|
ip.prefix = prefixes.last()
|
|
ip.save()
|
|
draw_progress(i, total, 50)
|
|
|
|
end = time.time()
|
|
print(f"\r\nElapsed Time: {end - start:.2f}s")
|
|
|
|
|
|
def unset_prefix(apps, schema_editor):
|
|
IPAddress = apps.get_model('ipam', 'IPAddress')
|
|
IPAddress.objects.update(prefix=None)
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('ipam', '0078_iprange_mark_utilized'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AddField(
|
|
model_name='ipaddress',
|
|
name='prefix',
|
|
field=models.ForeignKey(
|
|
blank=True,
|
|
null=True,
|
|
on_delete=django.db.models.deletion.SET_NULL,
|
|
related_name='ip_addresses',
|
|
to='ipam.prefix',
|
|
),
|
|
),
|
|
migrations.RunPython(set_prefix, unset_prefix)
|
|
]
|