Track the catalog zone SOA serial per view instead of a single global value. The new CatalogZone model has a 1:1 FK to netbox_dns.models.View and its own soa_serial field; each view's catalog zone now increments independently. The existing global serial is preserved during migration by copying it into every view's new CatalogZone row. Change CatalogZoneMemberIdentifier.name from globally unique to unique per catalog zone (UniqueConstraint(name, catalog_zone)) and add a catalog_zone FK to enforce it. Remove the dns-settings management command, which was a generic interface over the now-removed IntegerKeyValueSetting model. Increment both catalog zone serials when a zone moves between views: the old view's catalog zone loses a member and the new view's gains one, so both must be bumped. Also refresh the member identifier's catalog_zone FK on a view move so it does not go stale. The pre_save handler now captures _old_view_id and the post_save handler detects view changes, increments the old view's serial inside on_commit (before the new view's), and re-creates the identifier. AI generated, code manually verified.
78 lines
1.8 KiB
Python
78 lines
1.8 KiB
Python
from django.db import models
|
|
|
|
# import netbox.models
|
|
import netbox_dns.models
|
|
|
|
|
|
class CatalogZone(models.Model):
|
|
_netbox_private = True
|
|
|
|
view = models.OneToOneField(
|
|
to=netbox_dns.models.View,
|
|
on_delete=models.CASCADE,
|
|
related_name="catalog_zone",
|
|
)
|
|
soa_serial = models.IntegerField(default=1)
|
|
|
|
class Meta:
|
|
ordering = ("view__name",)
|
|
|
|
def __str__(self):
|
|
return f"{self.view.name}: {self.soa_serial}"
|
|
|
|
|
|
class CatalogZoneMemberIdentifier(models.Model):
|
|
_netbox_private = True
|
|
|
|
name = models.CharField(
|
|
max_length=26,
|
|
)
|
|
|
|
zone = models.OneToOneField(
|
|
to=netbox_dns.models.Zone,
|
|
on_delete=models.CASCADE,
|
|
related_name="catz_identifier",
|
|
)
|
|
|
|
catalog_zone = models.ForeignKey(
|
|
to=CatalogZone,
|
|
on_delete=models.CASCADE,
|
|
related_name="member_identifiers",
|
|
)
|
|
|
|
class Meta:
|
|
ordering = ("name",)
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=["name", "catalog_zone"],
|
|
name="unique_name_per_catalog_zone",
|
|
),
|
|
]
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
|
|
class SeenTransferClients(models.Model):
|
|
_netbox_private = True
|
|
|
|
source_ip = models.GenericIPAddressField()
|
|
last_seen = models.DateTimeField()
|
|
view = models.ForeignKey(
|
|
to=netbox_dns.models.View,
|
|
on_delete=models.PROTECT,
|
|
related_name="seen_transfer_clients",
|
|
)
|
|
|
|
def __str__(self):
|
|
return f"{self.source_ip} ({self.view.name})"
|
|
|
|
class Meta:
|
|
ordering = ("source_ip",)
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=["source_ip", "view"],
|
|
name="unique_ip_view_seen_client",
|
|
),
|
|
]
|