Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions netbox_branching/signal_receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
from django.dispatch import receiver
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
from django.contrib.contenttypes.models import ContentType

from core.choices import ObjectChangeActionChoices
from core.models import ObjectChange, ObjectType
from extras.events import process_event_rules
from extras.models import EventRule
from netbox.signals import post_clean
from netbox_branching import signals
from utilities.exceptions import AbortRequest
from utilities.serialization import serialize_object
Expand All @@ -25,9 +28,43 @@
'handle_branch_event',
'record_change_diff',
'validate_branch_deletion',
'validate_branching_operations',
)


@receiver(post_clean)
def validate_branching_operations(sender, instance, **kwargs):
"""
Validate that branching operations are valid (e.g., not modifying deleted objects).
"""
branch = active_branch.get()

# Only validate if we're in a branch and this model supports branching
if branch is None:
return

# Check if this model supports branching
content_type = ContentType.objects.get_for_model(instance)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not query on ObjectType directly here?

if 'branching' not in content_type.object_type.features:
return

# For updates and deletes, check if the object exists in main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably missing it, but how are we accounting for deletes?

if hasattr(instance, 'pk') and instance.pk is not None:
model = instance.__class__
with deactivate_branch():
try:
# Try to get the object from main database
model.objects.get(pk=instance.pk)
except model.DoesNotExist:
raise ValidationError(
_("Cannot modify {model_name} '{object_name}' because it has been deleted in the main branch.")
.format(
model_name=model._meta.verbose_name,
object_name=str(instance)
)
)


@receiver(post_save, sender=ObjectChange)
def record_change_diff(instance, **kwargs):
"""
Expand Down