Browse Source

Clean up Pushover validation.

pull/340/head
Pēteris Caune 5 years ago
parent
commit
26757c6785
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
2 changed files with 29 additions and 17 deletions
  1. +22
    -0
      hc/front/forms.py
  2. +7
    -17
      hc/front/views.py

+ 22
- 0
hc/front/forms.py View File

@ -90,6 +90,28 @@ class AddOpsGenieForm(forms.Form):
key = forms.CharField(max_length=40)
PRIO_CHOICES = [
("-2", "Lowest Priority"),
("-1", "Low Priority"),
("0", "Normal Priority"),
("1", "High Priority"),
("2", "Emergency Priority"),
]
class AddPushoverForm(forms.Form):
error_css_class = "has-error"
pushover_user_key = forms.CharField()
prio = forms.ChoiceField(initial="0", choices=PRIO_CHOICES)
prio_up = forms.ChoiceField(initial="0", choices=PRIO_CHOICES)
def get_value(self):
key = self.cleaned_data["pushover_user_key"]
prio = self.cleaned_data["prio"]
prio_up = self.cleaned_data["prio_up"]
return "%s|%s|%s" % (key, prio, prio_up)
class AddEmailForm(forms.Form):
error_css_class = "has-error"
value = forms.EmailField(max_length=100)


+ 7
- 17
hc/front/views.py View File

@ -41,6 +41,7 @@ from hc.front.forms import (
AddMatrixForm,
AddOpsGenieForm,
AddPdForm,
AddPushoverForm,
AddShellForm,
AddSmsForm,
AddUrlForm,
@ -1254,7 +1255,6 @@ def add_pushover(request, code):
project = _get_project_for_user(request, code)
if request.method == "POST":
# Initiate the subscription
state = token_urlsafe()
failure_url = settings.SITE_ROOT + reverse("hc-p-channels", args=[project.code])
@ -1288,27 +1288,17 @@ def add_pushover(request, code):
if request.GET.get("state") != state:
return HttpResponseForbidden()
key = request.GET.get("pushover_user_key")
if key is None:
return HttpResponseBadRequest()
# Validate priority
prio = request.GET.get("prio")
if prio not in ("-2", "-1", "0", "1", "2"):
return HttpResponseBadRequest()
prio_up = request.GET.get("prio_up")
if prio_up not in ("-2", "-1", "0", "1", "2"):
return HttpResponseBadRequest()
if request.GET.get("pushover_unsubscribed") == "1":
# Unsubscription: delete all Pushover channels for this project
Channel.objects.filter(project=project, kind="po").delete()
return redirect("hc-channels")
return redirect("hc-p-channels", project.code)
form = AddPushoverForm(request.GET)
if not form.is_valid():
return HttpResponseBadRequest()
# Subscription
channel = Channel(project=project, kind="po")
channel.value = "%s|%s|%s" % (key, prio, prio_up)
channel.value = form.get_value()
channel.save()
channel.assign_all_checks()


Loading…
Cancel
Save