Browse Source

Combine the add_webhook and edit_webhook views

pull/563/head
Pēteris Caune 3 years ago
parent
commit
a27652d762
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
10 changed files with 38 additions and 53 deletions
  1. +3
    -0
      hc/api/models.py
  2. +2
    -7
      hc/front/forms.py
  3. +1
    -0
      hc/front/tests/test_add_email.py
  4. +1
    -0
      hc/front/tests/test_edit_email.py
  5. +2
    -2
      hc/front/tests/test_edit_webhook.py
  6. +1
    -2
      hc/front/urls.py
  7. +16
    -33
      hc/front/views.py
  8. +2
    -5
      templates/front/channels.html
  9. +7
    -1
      templates/integrations/email_form.html
  10. +3
    -3
      templates/integrations/webhook_form.html

+ 3
- 0
hc/api/models.py View File

@ -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)


+ 2
- 7
hc/front/forms.py View File

@ -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):


+ 1
- 0
hc/front/tests/test_add_email.py View File

@ -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": "[email protected]", "down": "true", "up": "true"}


+ 1
- 0
hc/front/tests/test_edit_email.py View File

@ -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, "[email protected]")
self.assertContains(r, "Email Settings")
def test_it_saves_changes(self):
form = {"value": "[email protected]", "down": "true", "up": "false"}


+ 2
- 2
hc/front/tests/test_edit_webhook.py View File

@ -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="[email protected]", 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 = "[email protected]"
self.channel.save()


+ 1
- 2
hc/front/urls.py View File

@ -39,7 +39,6 @@ channel_urls = [
path("<uuid:code>/checks/", views.channel_checks, name="hc-channel-checks"),
path("<uuid:code>/name/", views.update_channel_name, name="hc-channel-name"),
path("<uuid:code>/edit/", views.edit_channel, name="hc-edit-channel"),
path("<uuid:code>/edit_webhook/", views.edit_webhook, name="hc-edit-webhook"),
path("<uuid:code>/test/", views.send_test_notification, name="hc-channel-test"),
path("<uuid:code>/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"),


+ 16
- 33
hc/front/views.py View File

@ -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)


+ 2
- 5
templates/front/channels.html View File

@ -144,10 +144,7 @@
{% endif %}
</td>
<td class="actions">
{% if ch.kind == "webhook" and rw %}
<a class="btn btn-sm btn-default" href="{% url 'hc-edit-webhook' ch.code %}">Edit</a>
{% endif %}
{% if ch.kind == "email" and rw %}
{% if ch.is_editable and rw %}
<a class="btn btn-sm btn-default" href="{% url 'hc-edit-channel' ch.code %}">Edit</a>
{% endif %}
<form action="{% url 'hc-channel-test' ch.code %}" method="post">
@ -606,7 +603,7 @@
</div>
<div class="modal-footer">
{% if ch.kind == "webhook" %}
<a class="btn btn-default pull-left" href="{% url 'hc-edit-webhook' ch.code %}">Edit Webhook Parameters&hellip;</a>
<a class="btn btn-default pull-left" href="{% url 'hc-edit-channel' ch.code %}">Edit Webhook Parameters&hellip;</a>
{% endif %}
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save</button>


templates/integrations/add_email.html → templates/integrations/email_form.html View File


+ 3
- 3
templates/integrations/webhook_form.html View File

@ -2,10 +2,10 @@
{% load compress humanize static hc_extras %}
{% block title %}
{% if channel %}
Webhook Settings - {% site_name %}
{% else %}
{% if is_new %}
Add Webhook Integration - {% site_name %}
{% else %}
Webhook Settings - {% site_name %}
{% endif %}
{% endblock %}


Loading…
Cancel
Save