Skip to content
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

148 Allow NetBox Scripts to run when a Branch is selected #184

Merged
merged 14 commits into from
Jan 30, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adapt for dynamic registration of request processors in NetBox
jeremystretch committed Dec 12, 2024
commit 2184b9e1bac169773c18596b9812e62fbaa0fe96
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -38,13 +38,7 @@ PLUGINS = [
]
```

5. Add `SCRIPT_CONTEXT_MANAGERS` to `configuration.py`.

```python
SCRIPT_CONTEXT_MANAGERS = ['netbox_branching.script_context_manager.NetBoxScriptContextManager',]
```

6. Create `local_settings.py` (in the same directory as `settings.py`) to override the `DATABASES` & `DATABASE_ROUTERS` settings. This enables dynamic schema support.
5. Create `local_settings.py` (in the same directory as `settings.py`) to override the `DATABASES` & `DATABASE_ROUTERS` settings. This enables dynamic schema support.

```python
from netbox_branching.utilities import DynamicSchemaDict
@@ -61,7 +55,7 @@ DATABASE_ROUTERS = [
]
```

7. Run NetBox migrations:
6. Run NetBox migrations:

```
$ ./manage.py migrate
9 changes: 0 additions & 9 deletions netbox_branching/script_context_manager.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
{% if active_branch %}
<div class="alert alert-warning" role="alert">
<i class="mdi mdi-alert"></i>
{% trans "This script will be run in branch:" %}
{% trans "This script will be run in branch" %}
<a href="{{ active_branch.get_absolute_url }}">{{ active_branch.name }}</a>
</div>
{% endif %}
16 changes: 12 additions & 4 deletions netbox_branching/utilities.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import datetime
import logging
from collections import defaultdict
from contextlib import contextmanager
from contextlib import contextmanager, nullcontext
from dataclasses import dataclass

from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import ForeignKey, ManyToManyField
from django.http import HttpResponseBadRequest
from django.urls import reverse

from netbox.plugins import get_plugin_config
from netbox.registry import registry
from netbox.utils import register_request_processor
from .choices import BranchStatusChoices
from .constants import EXEMPT_MODELS, INCLUDE_MODELS
from .constants import COOKIE_NAME, BRANCH_HEADER, QUERY_PARAM
from .constants import BRANCH_HEADER, COOKIE_NAME, EXEMPT_MODELS, INCLUDE_MODELS, QUERY_PARAM
from .contextvars import active_branch

__all__ = (
'ChangeSummary',
'DynamicSchemaDict',
'ListHandler',
'ActiveBranchContextManager',
'activate_branch',
'deactivate_branch',
'get_active_branch',
'get_branchable_object_types',
'get_tables_to_replicate',
'is_api_request',
@@ -250,3 +251,10 @@ def get_active_branch(request):
# Branch set by cookie
elif schema_id := request.COOKIES.get(COOKIE_NAME):
return Branch.objects.filter(schema_id=schema_id, status=BranchStatusChoices.READY).first()


@register_request_processor
def ActiveBranchContextManager(request):
if branch := get_active_branch(request):
return activate_branch(branch)
return nullcontext()