Project code in URL for the "Add Shell" page. cc: #336

This commit is contained in:
Pēteris Caune 2020-02-21 15:44:55 +02:00
parent 88f2a01182
commit 81f9a604e1
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
4 changed files with 11 additions and 8 deletions

View File

@ -5,7 +5,9 @@ from hc.test import BaseTestCase
@override_settings(SHELL_ENABLED=True)
class AddShellTestCase(BaseTestCase):
url = "/integrations/add_shell/"
def setUp(self):
super(AddShellTestCase, self).setUp()
self.url = "/projects/%s/add_shell/" % self.project.code
@override_settings(SHELL_ENABLED=False)
def test_it_is_disabled_by_default(self):
@ -23,7 +25,7 @@ class AddShellTestCase(BaseTestCase):
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, form)
self.assertRedirects(r, "/integrations/")
self.assertRedirects(r, self.channels_url)
c = Channel.objects.get()
self.assertEqual(c.project, self.project)

View File

@ -24,7 +24,6 @@ check_urls = [
channel_urls = [
path("", views.channels, name="hc-channels"),
path("add_shell/", views.add_shell, name="hc-add-shell"),
path("add_pd/", views.add_pd, name="hc-add-pd"),
path("add_pdc/", views.add_pdc, name="hc-add-pdc"),
path("add_pdc/<str:state>/", views.add_pdc, name="hc-add-pdc-state"),
@ -61,6 +60,7 @@ project_urls = [
path("add_pagerteam/", views.add_pagerteam, name="hc-add-pagerteam"),
path("add_pagertree/", views.add_pagertree, name="hc-add-pagertree"),
path("add_prometheus/", views.add_prometheus, name="hc-add-prometheus"),
path("add_shell/", views.add_shell, name="hc-add-shell"),
path("add_sms/", views.add_sms, name="hc-add-sms"),
path("add_victorops/", views.add_victorops, name="hc-add-victorops"),
path("add_webhook/", views.add_webhook, name="hc-add-webhook"),

View File

@ -876,25 +876,26 @@ def add_webhook(request, code):
@login_required
def add_shell(request):
def add_shell(request, code):
if not settings.SHELL_ENABLED:
raise Http404("shell integration is not available")
project = _get_project_for_user(request, code)
if request.method == "POST":
form = AddShellForm(request.POST)
if form.is_valid():
channel = Channel(project=request.project, kind="shell")
channel = Channel(project=project, kind="shell")
channel.value = form.get_value()
channel.save()
channel.assign_all_checks()
return redirect("hc-channels")
return redirect("hc-p-channels", project.code)
else:
form = AddShellForm()
ctx = {
"page": "channels",
"project": request.project,
"project": project,
"form": form,
"now": timezone.now().replace(microsecond=0).isoformat(),
}

View File

@ -319,7 +319,7 @@
<h2>Shell Command</h2>
<p>Execute a local shell command when a check goes up or down.</p>
<a href="{% url 'hc-add-shell' %}" class="btn btn-primary">Add Integration</a>
<a href="{% url 'hc-add-shell' project.code %}" class="btn btn-primary">Add Integration</a>
</li>
{% endif %}