18136 Allow Script running within a Branch

This commit is contained in:
Arthur Hanson 2024-12-02 12:53:10 -08:00
parent 24b76792a9
commit 51e770fa9c
3 changed files with 30 additions and 2 deletions

View File

@ -219,3 +219,11 @@ The time zone NetBox will use when dealing with dates and times. It is recommend
Default: True Default: True
Enables language translation for the user interface. (This parameter maps to Django's [USE_I18N](https://docs.djangoproject.com/en/stable/ref/settings/#std-setting-USE_I18N) setting.) Enables language translation for the user interface. (This parameter maps to Django's [USE_I18N](https://docs.djangoproject.com/en/stable/ref/settings/#std-setting-USE_I18N) setting.)
---
## BRANCHING_BACKEND
Default: None
The dotted path to the desired branching class. If using [netboxlabs-netbox-branching](https://github.com/netboxlabs/netbox-branching) set this to `netbox_branching.backends.BranchingBackend`

View File

@ -2,7 +2,10 @@ import logging
import traceback import traceback
from contextlib import nullcontext from contextlib import nullcontext
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import transaction from django.db import transaction
from django.utils.module_loading import import_string
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from core.signals import clear_events from core.signals import clear_events
@ -36,6 +39,7 @@ class ScriptJob(JobRunner):
""" """
logger = logging.getLogger(f"netbox.scripts.{script.full_name}") logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
logger.info(f"Running script (commit={commit})") logger.info(f"Running script (commit={commit})")
print("running script")
try: try:
try: try:
@ -49,6 +53,7 @@ class ScriptJob(JobRunner):
logger.warning("Script failed") logger.warning("Script failed")
except Exception as e: except Exception as e:
print(f"exception: {e}")
if type(e) is AbortScript: if type(e) is AbortScript:
msg = _("Script aborted with error: ") + str(e) msg = _("Script aborted with error: ") + str(e)
if is_report(type(script)): if is_report(type(script)):
@ -100,5 +105,19 @@ class ScriptJob(JobRunner):
# Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process # Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process
# change logging, event rules, etc. # change logging, event rules, etc.
with event_tracking(request) if commit else nullcontext(): branch = None
self.run_script(script, request, data, commit) if settings.BRANCHING_BACKEND:
try:
branching_cls = import_string(settings.BRANCHING_BACKEND)
except AttributeError:
logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
message = _("Failed to import configured BRANCHING_BACKEND: ") + settings.BRANCHING_BACKEND
logger.error(message)
raise ImproperlyConfigured(message)
branching_backend = branching_cls()
branch = branching_backend.get_active_branch(request)
with branching_backend.activate_branch(branch) if branch else nullcontext():
with event_tracking(request) if commit else nullcontext():
self.run_script(script, request, data, commit)

View File

@ -179,6 +179,7 @@ STORAGE_BACKEND = getattr(configuration, 'STORAGE_BACKEND', None)
STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {}) STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {})
TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC') TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
TRANSLATION_ENABLED = getattr(configuration, 'TRANSLATION_ENABLED', True) TRANSLATION_ENABLED = getattr(configuration, 'TRANSLATION_ENABLED', True)
BRANCHING_BACKEND = getattr(configuration, 'BRANCHING_BACKEND', None)
# Load any dynamic configuration parameters which have been hard-coded in the configuration file # Load any dynamic configuration parameters which have been hard-coded in the configuration file
for param in CONFIG_PARAMS: for param in CONFIG_PARAMS: