diff --git a/netbox_branching/__init__.py b/netbox_branching/__init__.py index 7ba11ca..32d1c2b 100644 --- a/netbox_branching/__init__.py +++ b/netbox_branching/__init__.py @@ -11,7 +11,7 @@ class AppConfig(PluginConfig): description = 'A git-like branching implementation for NetBox' version = '0.5.2' base_url = 'branching' - min_version = '4.1' + min_version = '4.1.9' middleware = [ 'netbox_branching.middleware.BranchMiddleware' ] diff --git a/netbox_branching/middleware.py b/netbox_branching/middleware.py index a79e994..c339365 100644 --- a/netbox_branching/middleware.py +++ b/netbox_branching/middleware.py @@ -1,14 +1,8 @@ -from django.contrib import messages from django.core.exceptions import ObjectDoesNotExist from django.http import HttpResponseBadRequest -from django.urls import reverse -from utilities.api import is_api_request - -from .choices import BranchStatusChoices -from .constants import COOKIE_NAME, BRANCH_HEADER, QUERY_PARAM -from .models import Branch -from .utilities import activate_branch, is_api_request +from .constants import COOKIE_NAME, QUERY_PARAM +from .utilities import activate_branch, is_api_request, get_active_branch __all__ = ( 'BranchMiddleware', @@ -24,12 +18,11 @@ def __call__(self, request): # Set/clear the active Branch on the request try: - branch = self.get_active_branch(request) + branch = get_active_branch(request) except ObjectDoesNotExist: return HttpResponseBadRequest("Invalid branch identifier") - with activate_branch(branch): - response = self.get_response(request) + response = self.get_response(request) # Set/clear the branch cookie (for non-API requests) if not is_api_request(request): @@ -39,34 +32,3 @@ def __call__(self, request): response.delete_cookie(COOKIE_NAME) return response - - @staticmethod - def get_active_branch(request): - """ - Return the active Branch (if any). - """ - # The active Branch may be specified by HTTP header for REST & GraphQL API requests. - if is_api_request(request) and BRANCH_HEADER in request.headers: - branch = Branch.objects.get(schema_id=request.headers.get(BRANCH_HEADER)) - if not branch.ready: - return HttpResponseBadRequest(f"Branch {branch} is not ready for use (status: {branch.status})") - return branch - - # Branch activated/deactivated by URL query parameter - elif QUERY_PARAM in request.GET: - if schema_id := request.GET.get(QUERY_PARAM): - branch = Branch.objects.get(schema_id=schema_id) - if branch.ready: - messages.success(request, f"Activated branch {branch}") - return branch - else: - messages.error(request, f"Branch {branch} is not ready for use (status: {branch.status})") - return None - else: - messages.success(request, f"Deactivated branch") - request.COOKIES.pop(COOKIE_NAME, None) # Delete cookie if set - return None - - # Branch set by cookie - elif schema_id := request.COOKIES.get(COOKIE_NAME): - return Branch.objects.filter(schema_id=schema_id, status=BranchStatusChoices.READY).first() diff --git a/netbox_branching/template_content.py b/netbox_branching/template_content.py index 063a9df..b4993a0 100644 --- a/netbox_branching/template_content.py +++ b/netbox_branching/template_content.py @@ -1,6 +1,6 @@ from django.contrib.contenttypes.models import ContentType -from netbox.plugins import PluginTemplateExtension +from netbox.plugins import PluginTemplateExtension from .choices import BranchStatusChoices from .contextvars import active_branch from .models import Branch, ChangeDiff @@ -8,6 +8,8 @@ __all__ = ( 'BranchNotification', 'BranchSelector', + 'ScriptNotification', + 'ShareButton', 'template_extensions', ) @@ -34,6 +36,7 @@ class BranchNotification(PluginTemplateExtension): def alerts(self): if not (instance := self.context['object']): return '' + ct = ContentType.objects.get_for_model(instance) relevant_changes = ChangeDiff.objects.filter( object_type=ct, @@ -51,4 +54,18 @@ def alerts(self): }) -template_extensions = [BranchSelector, ShareButton, BranchNotification] +class ScriptNotification(PluginTemplateExtension): + models = ['extras.script'] + + def alerts(self): + return self.render('netbox_branching/inc/script_alert.html', extra_context={ + 'active_branch': active_branch.get(), + }) + + +template_extensions = ( + BranchSelector, + BranchNotification, + ScriptNotification, + ShareButton, +) diff --git a/netbox_branching/templates/netbox_branching/inc/script_alert.html b/netbox_branching/templates/netbox_branching/inc/script_alert.html new file mode 100644 index 0000000..002e3aa --- /dev/null +++ b/netbox_branching/templates/netbox_branching/inc/script_alert.html @@ -0,0 +1,8 @@ +{% load i18n %} +{% if active_branch %} +