From a27652d762846755d19b99bd61f2df62a5f1bc0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 25 Aug 2021 20:29:46 +0300 Subject: [PATCH] Combine the add_webhook and edit_webhook views --- hc/api/models.py | 3 ++ hc/front/forms.py | 9 +--- hc/front/tests/test_add_email.py | 1 + hc/front/tests/test_edit_email.py | 1 + hc/front/tests/test_edit_webhook.py | 4 +- hc/front/urls.py | 3 +- hc/front/views.py | 49 ++++++------------- templates/front/channels.html | 7 +-- .../{add_email.html => email_form.html} | 8 ++- templates/integrations/webhook_form.html | 6 +-- 10 files changed, 38 insertions(+), 53 deletions(-) rename templates/integrations/{add_email.html => email_form.html} (95%) diff --git a/hc/api/models.py b/hc/api/models.py index fb4e3535..750cf6da 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -415,6 +415,9 @@ class Channel(models.Model): def to_dict(self): return {"id": str(self.code), "name": self.name, "kind": self.kind} + def is_editable(self): + return self.kind in ("email", "webhook") + def assign_all_checks(self): checks = Check.objects.filter(project=self.project) self.checks.add(*checks) diff --git a/hc/front/forms.py b/hc/front/forms.py index d434ead2..5f6f7da8 100644 --- a/hc/front/forms.py +++ b/hc/front/forms.py @@ -155,13 +155,8 @@ class EmailForm(forms.Form): if not down and not up: self.add_error("down", "Please select at least one.") - def to_json(self): - d = { - "value": self.cleaned_data["value"], - "up": self.cleaned_data["up"], - "down": self.cleaned_data["down"], - } - return json.dumps(d) + def get_value(self): + return json.dumps(dict(self.cleaned_data), sort_keys=True) class AddUrlForm(forms.Form): diff --git a/hc/front/tests/test_add_email.py b/hc/front/tests/test_add_email.py index 1dc48a8c..05e35f02 100644 --- a/hc/front/tests/test_add_email.py +++ b/hc/front/tests/test_add_email.py @@ -17,6 +17,7 @@ class AddEmailTestCase(BaseTestCase): r = self.client.get(self.url) self.assertContains(r, "Get an email message") self.assertContains(r, "Requires confirmation") + self.assertContains(r, "Set Up Email Notifications") def test_it_creates_channel(self): form = {"value": "dan@example.org", "down": "true", "up": "true"} diff --git a/hc/front/tests/test_edit_email.py b/hc/front/tests/test_edit_email.py index 14df713e..d17401b1 100644 --- a/hc/front/tests/test_edit_email.py +++ b/hc/front/tests/test_edit_email.py @@ -25,6 +25,7 @@ class EditEmailTestCase(BaseTestCase): r = self.client.get(self.url) self.assertContains(r, "Get an email message when check goes up or down.") self.assertContains(r, "alerts@example.org") + self.assertContains(r, "Email Settings") def test_it_saves_changes(self): form = {"value": "new@example.org", "down": "true", "up": "false"} diff --git a/hc/front/tests/test_edit_webhook.py b/hc/front/tests/test_edit_webhook.py index 515d625c..1c71e7a3 100644 --- a/hc/front/tests/test_edit_webhook.py +++ b/hc/front/tests/test_edit_webhook.py @@ -24,7 +24,7 @@ class EditWebhookTestCase(BaseTestCase): self.channel.value = json.dumps(definition) self.channel.save() - self.url = "/integrations/%s/edit_webhook/" % self.channel.code + self.url = "/integrations/%s/edit/" % self.channel.code def test_it_shows_form(self): self.client.login(username="alice@example.org", password="password") @@ -75,7 +75,7 @@ class EditWebhookTestCase(BaseTestCase): self.assertEqual(up_spec["headers"], {"Content-Type": "text/plain"}) def test_it_requires_kind_webhook(self): - self.channel.kind = "email" + self.channel.kind = "sms" self.channel.value = "foo@example.org" self.channel.save() diff --git a/hc/front/urls.py b/hc/front/urls.py index b1938d94..33465c61 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -39,7 +39,6 @@ channel_urls = [ path("/checks/", views.channel_checks, name="hc-channel-checks"), path("/name/", views.update_channel_name, name="hc-channel-name"), path("/edit/", views.edit_channel, name="hc-edit-channel"), - path("/edit_webhook/", views.edit_webhook, name="hc-edit-webhook"), path("/test/", views.send_test_notification, name="hc-channel-test"), path("/remove/", views.remove_channel, name="hc-remove-channel"), path( @@ -75,7 +74,7 @@ project_urls = [ path("add_spike/", views.add_spike, name="hc-add-spike"), path("add_trello/", views.add_trello, name="hc-add-trello"), path("add_victorops/", views.add_victorops, name="hc-add-victorops"), - path("add_webhook/", views.add_webhook, name="hc-add-webhook"), + path("add_webhook/", views.webhook_form, name="hc-add-webhook"), path("add_whatsapp/", views.add_whatsapp, name="hc-add-whatsapp"), path("add_zulip/", views.add_zulip, name="hc-add-zulip"), path("badges/", views.badges, name="hc-badges"), diff --git a/hc/front/views.py b/hc/front/views.py index 1d522543..f1fe1720 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -951,7 +951,7 @@ def remove_channel(request, code): @login_required -def email_form(request, code=None, channel=None): +def email_form(request, channel=None, code=None): """ Add email integration or edit an existing email integration. """ is_new = channel is None @@ -974,7 +974,7 @@ def email_form(request, code=None, channel=None): else: channel.email_verified = False - channel.value = form.to_json() + channel.value = form.get_value() channel.save() if is_new: @@ -984,6 +984,8 @@ def email_form(request, code=None, channel=None): channel.send_verify_link() return redirect("hc-channels", channel.project.code) + elif is_new: + form = forms.EmailForm() else: form = forms.EmailForm( { @@ -998,8 +1000,9 @@ def email_form(request, code=None, channel=None): "project": channel.project, "use_verification": settings.EMAIL_USE_VERIFICATION, "form": form, + "is_new": is_new, } - return render(request, "integrations/add_email.html", ctx) + return render(request, "integrations/email_form.html", ctx) @login_required @@ -1007,51 +1010,32 @@ def edit_channel(request, code): channel = _get_rw_channel_for_user(request, code) if channel.kind == "email": return email_form(request, channel=channel) + if channel.kind == "webhook": + return webhook_form(request, channel=channel) return HttpResponseBadRequest() @require_setting("WEBHOOKS_ENABLED") @login_required -def add_webhook(request, code): - project = _get_rw_project_for_user(request, code) +def webhook_form(request, channel=None, code=None): + is_new = channel is None + if is_new: + project = _get_rw_project_for_user(request, code) + channel = Channel(project=project, kind="webhook") if request.method == "POST": form = forms.WebhookForm(request.POST) if form.is_valid(): - channel = Channel(project=project, kind="webhook") channel.name = form.cleaned_data["name"] channel.value = form.get_value() channel.save() channel.assign_all_checks() - return redirect("hc-channels", project.code) + return redirect("hc-channels", channel.project.code) - else: + elif is_new: form = forms.WebhookForm() - - ctx = { - "page": "channels", - "project": project, - "form": form, - } - return render(request, "integrations/webhook_form.html", ctx) - - -@login_required -def edit_webhook(request, code): - channel = _get_rw_channel_for_user(request, code) - if channel.kind != "webhook": - return HttpResponseBadRequest() - - if request.method == "POST": - form = forms.WebhookForm(request.POST) - if form.is_valid(): - channel.name = form.cleaned_data["name"] - channel.value = form.get_value() - channel.save() - - return redirect("hc-channels", channel.project.code) else: def flatten(d): @@ -1061,14 +1045,13 @@ def edit_webhook(request, code): doc["headers_down"] = flatten(doc["headers_down"]) doc["headers_up"] = flatten(doc["headers_up"]) doc["name"] = channel.name - form = forms.WebhookForm(doc) ctx = { "page": "channels", "project": channel.project, - "channel": channel, "form": form, + "is_new": is_new, } return render(request, "integrations/webhook_form.html", ctx) diff --git a/templates/front/channels.html b/templates/front/channels.html index df6e1bca..d84ecd0a 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -144,10 +144,7 @@ {% endif %} - {% if ch.kind == "webhook" and rw %} - Edit - {% endif %} - {% if ch.kind == "email" and rw %} + {% if ch.is_editable and rw %} Edit {% endif %}
@@ -606,7 +603,7 @@