From 023fb476e9f24d40dd135f4b5674a0f23a265f37 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Tue, 3 Dec 2024 14:49:57 -0800 Subject: [PATCH] 87 deactivate the current branch when creating a new one --- netbox_branching/views.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/netbox_branching/views.py b/netbox_branching/views.py index 38fb44c..5616fbc 100644 --- a/netbox_branching/views.py +++ b/netbox_branching/views.py @@ -7,12 +7,14 @@ from core.choices import ObjectChangeActionChoices from core.filtersets import ObjectChangeFilterSet from core.models import ObjectChange +from netbox_branching.contextvars import active_branch from netbox.views import generic from utilities.views import ViewTab, register_model_view from . import filtersets, forms, tables from .choices import BranchStatusChoices from .jobs import MergeBranchJob, RevertBranchJob, SyncBranchJob from .models import Branch, ChangeDiff +from .utilities import activate_branch # @@ -74,6 +76,15 @@ def alter_object(self, obj, request, url_args, url_kwargs): obj.owner = request.user return obj + def post(self, request, **kwargs): + # if creating and there is an active branch - switch to main + obj = self.get_object(**kwargs) + if not obj.pk and active_branch.get(): + with activate_branch(None): + return super().post(request, **kwargs) + + return super().post(request, **kwargs) + @register_model_view(Branch, 'delete') class BranchDeleteView(generic.ObjectDeleteView):