Skip to content

Fixes #120: max_branches should disregard archived branches #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions netbox_branching/models/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
10 changes: 8 additions & 2 deletions netbox_branching/tests/test_branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()