Skip to content

Commit cbad819

Browse files
Fix #139 - use Nautobot 1.2 plugin banner API for current branch instead of middleware (#160)
1 parent 4684533 commit cbad819

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

dolt/banner.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Injection of branch information banner via the Plugins API."""
2+
3+
from django.utils.html import format_html
4+
5+
from nautobot.extras.choices import BannerClassChoices
6+
from nautobot.extras.plugins import PluginBanner
7+
8+
from dolt.constants import DOLT_BRANCH_KEYWORD
9+
from dolt.utils import active_branch
10+
11+
12+
def banner(context, *args, **kwargs):
13+
"""Show a banner indicating the current active branch, if the user is logged in."""
14+
if not context.request.user.is_authenticated:
15+
return None
16+
branch_name = active_branch()
17+
return PluginBanner(
18+
content=format_html(
19+
"""
20+
<div class="text-center">
21+
Active Branch: <strong>{}</strong>
22+
<div class = "pull-right">
23+
<div class="btn btn-xs btn-primary" id="branch-share-button">
24+
Share
25+
</div>
26+
</div>
27+
</div>
28+
<script>
29+
const btn = document.getElementById("branch-share-button");
30+
btn.addEventListener('click', ()=>{{
31+
const currLink = window.location.href;
32+
const copiedLink = currLink + "?{}={}";
33+
navigator.clipboard.writeText(copiedLink);
34+
btn.textContent = "Copied!"
35+
}});
36+
</script>""",
37+
branch_name,
38+
DOLT_BRANCH_KEYWORD,
39+
branch_name,
40+
),
41+
banner_class=BannerClassChoices.CLASS_INFO,
42+
)

dolt/middleware.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""middleware.py contains the middleware add-ons needed for the dolt plugin to work."""
22

3-
import random
4-
53
from django.contrib import messages
64
from django.core.exceptions import ObjectDoesNotExist
75
from django.db.models.signals import m2m_changed, post_save, pre_delete
@@ -16,7 +14,7 @@
1614
DOLT_DEFAULT_BRANCH,
1715
)
1816
from dolt.models import Branch, Commit
19-
from dolt.utils import DoltError, active_branch
17+
from dolt.utils import DoltError
2018

2119

2220
def dolt_health_check_intercept_middleware(get_response):
@@ -62,14 +60,6 @@ def process_view(self, request, view_func, view_args, view_kwargs): # pylint: d
6260
msg = f"could not checkout branch {branch}: {str(e)}"
6361
messages.error(request, mark_safe(msg))
6462

65-
if request.user.is_authenticated:
66-
# Inject the "active branch" banner. Use a random number for the button id to ensure button listeners do not
67-
# clash. This is safe since it is JS generated on our end and should not be modifiable by any XSS attack.
68-
msg = DoltBranchMiddleware.get_active_branch_banner(
69-
random.randint(0, 10000) # nosec random is not being used for security purposes.
70-
)
71-
messages.info(request, mark_safe(msg))
72-
7363
try:
7464
return view_func(request, *view_args, **view_kwargs)
7565
except DoltError as e:
@@ -91,29 +81,6 @@ def get_branch(request):
9181
request.session[DOLT_BRANCH_KEYWORD] = DOLT_DEFAULT_BRANCH
9282
return Branch.objects.get(pk=DOLT_DEFAULT_BRANCH)
9383

94-
@staticmethod
95-
def get_active_branch_banner(b_id):
96-
"""get_active_branch_banner returns a banner that renders the active branch and its share button."""
97-
return f"""
98-
<div class="text-center">
99-
Active Branch: {active_branch()}
100-
<div class = "pull-right">
101-
<div class="btn btn-xs btn-primary" id="share-button-{b_id}">
102-
Share
103-
</div>
104-
</div>
105-
</div>
106-
<script>
107-
const btn{b_id} = document.getElementById("share-button-{b_id}");
108-
btn{b_id}.addEventListener('click', ()=>{{
109-
const currLink = window.location.href;
110-
const copiedLink = currLink + "?{DOLT_BRANCH_KEYWORD}={active_branch()}";
111-
navigator.clipboard.writeText(copiedLink);
112-
btn{b_id}.textContent = "Copied!"
113-
}});
114-
</script>
115-
"""
116-
11784

11885
class DoltAutoCommitMiddleware:
11986
"""

0 commit comments

Comments
 (0)