From 501bcab0f0a9a0cbb12f97dec1bef4eeb4e96c3f Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 16 Sep 2024 09:20:27 -0400 Subject: [PATCH] Fixes #120: max_branches should disregard archived branches --- netbox_branching/models/branches.py | 6 +++--- netbox_branching/tests/test_branches.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/netbox_branching/models/branches.py b/netbox_branching/models/branches.py index 23591d0..a8a90fb 100644 --- a/netbox_branching/models/branches.py +++ b/netbox_branching/models/branches.py @@ -129,12 +129,12 @@ def clean(self): # Enforce the maximum number of total branches if not self.pk and (max_branches := get_plugin_config('netbox_branching', 'max_branches')): - total_branch_count = Branch.objects.count() + total_branch_count = Branch.objects.exclude(status=BranchStatusChoices.ARCHIVED).count() if total_branch_count >= max_branches: raise ValidationError( _( - "The configured maximum number of branches ({max}) cannot be exceeded. One or more existing " - "branches must be deleted before a new branch may be created." + "The configured maximum number of non-archived branches ({max}) cannot be exceeded. One or " + "more existing branches must be deleted before a new branch may be created." ).format(max=max_branches) ) diff --git a/netbox_branching/tests/test_branches.py b/netbox_branching/tests/test_branches.py index 6cbd01d..3bb263b 100644 --- a/netbox_branching/tests/test_branches.py +++ b/netbox_branching/tests/test_branches.py @@ -112,10 +112,16 @@ def test_max_branches(self): Verify that the max_branches config parameter is enforced. """ Branch.objects.bulk_create(( - Branch(name='Branch 1'), - Branch(name='Branch 2'), + Branch(name='Branch 1', status=BranchStatusChoices.ARCHIVED), + Branch(name='Branch 2', status=BranchStatusChoices.READY), )) + # Creating a second non-archived Branch should succeed branch = Branch(name='Branch 3') + branch.full_clean() + branch.save(provision=False) + + # Creating a third non-archived Branch should fail + branch = Branch(name='Branch 4') with self.assertRaises(ValidationError): branch.full_clean()