diff --git a/hc/front/forms.py b/hc/front/forms.py index 14b82b6a..8978eb57 100644 --- a/hc/front/forms.py +++ b/hc/front/forms.py @@ -193,15 +193,13 @@ class AddShellForm(forms.Form): return json.dumps(dict(self.cleaned_data), sort_keys=True) -class AddSmsForm(forms.Form): +class PhoneNumberForm(forms.Form): error_css_class = "has-error" label = forms.CharField(max_length=100, required=False) - value = forms.CharField() - down = forms.BooleanField(required=False, initial=True) - up = forms.BooleanField(required=False, initial=True) + phone = forms.CharField() - def clean_value(self): - v = self.cleaned_data["value"] + def clean_phone(self): + v = self.cleaned_data["phone"] stripped = v.encode("ascii", "ignore").decode("ascii") stripped = stripped.replace(" ", "").replace("-", "") @@ -210,6 +208,23 @@ class AddSmsForm(forms.Form): return stripped + def get_json(self): + return json.dumps({"value": self.cleaned_data["phone"]}) + + +class PhoneUpDownForm(PhoneNumberForm): + up = forms.BooleanField(required=False, initial=True) + down = forms.BooleanField(required=False, initial=True) + + def get_json(self): + return json.dumps( + { + "value": self.cleaned_data["phone"], + "up": self.cleaned_data["up"], + "down": self.cleaned_data["down"], + } + ) + class ChannelNameForm(forms.Form): name = forms.CharField(max_length=100, required=False) diff --git a/hc/front/tests/test_add_call.py b/hc/front/tests/test_add_call.py index 089716e1..4c428ba5 100644 --- a/hc/front/tests/test_add_call.py +++ b/hc/front/tests/test_add_call.py @@ -24,7 +24,7 @@ class AddCallTestCase(BaseTestCase): self.assertContains(r, "upgrade to a") def test_it_creates_channel(self): - form = {"label": "My Phone", "value": "+1234567890"} + form = {"label": "My Phone", "phone": "+1234567890"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) @@ -37,14 +37,14 @@ class AddCallTestCase(BaseTestCase): self.assertEqual(c.project, self.project) def test_it_rejects_bad_number(self): - form = {"value": "not a phone number"} + form = {"phone": "not a phone number"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) self.assertContains(r, "Invalid phone number format.") def test_it_trims_whitespace(self): - form = {"value": " +1234567890 "} + form = {"phone": " +1234567890 "} self.client.login(username="alice@example.org", password="password") self.client.post(self.url, form) diff --git a/hc/front/tests/test_add_signal.py b/hc/front/tests/test_add_signal.py index de6e8a0a..07642ec9 100644 --- a/hc/front/tests/test_add_signal.py +++ b/hc/front/tests/test_add_signal.py @@ -17,7 +17,7 @@ class AddSignalTestCase(BaseTestCase): def test_it_creates_channel(self): form = { "label": "My Phone", - "value": "+1234567890", + "phone": "+1234567890", "down": "true", "up": "true", } @@ -35,7 +35,7 @@ class AddSignalTestCase(BaseTestCase): self.assertEqual(c.project, self.project) def test_it_obeys_up_down_flags(self): - form = {"label": "My Phone", "value": "+1234567890"} + form = {"label": "My Phone", "phone": "+1234567890"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) @@ -46,7 +46,7 @@ class AddSignalTestCase(BaseTestCase): self.assertFalse(c.signal_notify_up) @override_settings(SIGNAL_CLI_USERNAME=None) - def test_it_handles_unset_username(self): + def test_it_handles_unset_sender_username(self): self.client.login(username="alice@example.org", password="password") r = self.client.get(self.url) self.assertEqual(r.status_code, 404) diff --git a/hc/front/tests/test_add_sms.py b/hc/front/tests/test_add_sms.py index 4ecfc75d..766d977d 100644 --- a/hc/front/tests/test_add_sms.py +++ b/hc/front/tests/test_add_sms.py @@ -24,7 +24,7 @@ class AddSmsTestCase(BaseTestCase): self.assertContains(r, "upgrade to a") def test_it_creates_channel(self): - form = {"label": "My Phone", "value": "+1234567890"} + form = {"label": "My Phone", "phone": "+1234567890"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) @@ -38,13 +38,13 @@ class AddSmsTestCase(BaseTestCase): def test_it_rejects_bad_number(self): for v in ["not a phone number address", False, 15, "+123456789A"]: - form = {"value": v} + form = {"phone": v} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) self.assertContains(r, "Invalid phone number format.") def test_it_trims_whitespace(self): - form = {"value": " +1234567890 "} + form = {"phone": " +1234567890 "} self.client.login(username="alice@example.org", password="password") self.client.post(self.url, form) @@ -67,7 +67,7 @@ class AddSmsTestCase(BaseTestCase): self.assertEqual(r.status_code, 403) def test_it_strips_invisible_formatting_characters(self): - form = {"label": "My Phone", "value": "\u202c+1234567890\u202c"} + form = {"label": "My Phone", "phone": "\u202c+1234567890\u202c"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) @@ -77,7 +77,7 @@ class AddSmsTestCase(BaseTestCase): self.assertEqual(c.phone_number, "+1234567890") def test_it_strips_hyphens(self): - form = {"label": "My Phone", "value": "+123-4567890"} + form = {"label": "My Phone", "phone": "+123-4567890"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) @@ -87,7 +87,7 @@ class AddSmsTestCase(BaseTestCase): self.assertEqual(c.phone_number, "+1234567890") def test_it_strips_spaces(self): - form = {"label": "My Phone", "value": "+123 45 678 90"} + form = {"label": "My Phone", "phone": "+123 45 678 90"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) diff --git a/hc/front/tests/test_add_whatsapp.py b/hc/front/tests/test_add_whatsapp.py index 2ff1ceec..8fd06c0b 100644 --- a/hc/front/tests/test_add_whatsapp.py +++ b/hc/front/tests/test_add_whatsapp.py @@ -33,7 +33,7 @@ class AddWhatsAppTestCase(BaseTestCase): def test_it_creates_channel(self): form = { "label": "My Phone", - "value": "+1234567890", + "phone": "+1234567890", "down": "true", "up": "true", } @@ -51,7 +51,7 @@ class AddWhatsAppTestCase(BaseTestCase): self.assertEqual(c.project, self.project) def test_it_obeys_up_down_flags(self): - form = {"label": "My Phone", "value": "+1234567890"} + form = {"label": "My Phone", "phone": "+1234567890"} self.client.login(username="alice@example.org", password="password") r = self.client.post(self.url, form) diff --git a/hc/front/views.py b/hc/front/views.py index 9be6f049..efabd53b 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -1555,17 +1555,17 @@ def add_telegram(request): def add_sms(request, code): project = _get_rw_project_for_user(request, code) if request.method == "POST": - form = forms.AddSmsForm(request.POST) + form = forms.PhoneNumberForm(request.POST) if form.is_valid(): channel = Channel(project=project, kind="sms") channel.name = form.cleaned_data["label"] - channel.value = json.dumps({"value": form.cleaned_data["value"]}) + channel.value = form.get_json() channel.save() channel.assign_all_checks() return redirect("hc-channels", project.code) else: - form = forms.AddSmsForm() + form = forms.PhoneNumberForm() ctx = { "page": "channels", @@ -1581,17 +1581,17 @@ def add_sms(request, code): def add_call(request, code): project = _get_rw_project_for_user(request, code) if request.method == "POST": - form = forms.AddSmsForm(request.POST) + form = forms.PhoneNumberForm(request.POST) if form.is_valid(): channel = Channel(project=project, kind="call") channel.name = form.cleaned_data["label"] - channel.value = json.dumps({"value": form.cleaned_data["value"]}) + channel.value = form.get_json() channel.save() channel.assign_all_checks() return redirect("hc-channels", project.code) else: - form = forms.AddSmsForm() + form = forms.PhoneNumberForm() ctx = { "page": "channels", @@ -1607,23 +1607,17 @@ def add_call(request, code): def add_whatsapp(request, code): project = _get_rw_project_for_user(request, code) if request.method == "POST": - form = forms.AddSmsForm(request.POST) + form = forms.PhoneUpDownForm(request.POST) if form.is_valid(): channel = Channel(project=project, kind="whatsapp") channel.name = form.cleaned_data["label"] - channel.value = json.dumps( - { - "value": form.cleaned_data["value"], - "up": form.cleaned_data["up"], - "down": form.cleaned_data["down"], - } - ) + channel.value = form.get_json() channel.save() channel.assign_all_checks() return redirect("hc-channels", project.code) else: - form = forms.AddSmsForm() + form = forms.PhoneUpDownForm() ctx = { "page": "channels", @@ -1639,23 +1633,17 @@ def add_whatsapp(request, code): def add_signal(request, code): project = _get_rw_project_for_user(request, code) if request.method == "POST": - form = forms.AddSmsForm(request.POST) + form = forms.PhoneUpDownForm(request.POST) if form.is_valid(): channel = Channel(project=project, kind="signal") channel.name = form.cleaned_data["label"] - channel.value = json.dumps( - { - "value": form.cleaned_data["value"], - "up": form.cleaned_data["up"], - "down": form.cleaned_data["down"], - } - ) + channel.value = form.get_json() channel.save() channel.assign_all_checks() return redirect("hc-channels", project.code) else: - form = forms.AddSmsForm() + form = forms.PhoneUpDownForm() ctx = { "page": "channels", diff --git a/templates/integrations/add_call.html b/templates/integrations/add_call.html index 7d583162..cdd32416 100644 --- a/templates/integrations/add_call.html +++ b/templates/integrations/add_call.html @@ -50,20 +50,20 @@ {% endif %} -
+
+ value="{{ form.phone.value|default:"" }}"> - {% if form.value.errors %} + {% if form.phone.errors %}
- {{ form.value.errors|join:"" }} + {{ form.phone.errors|join:"" }}
{% else %} diff --git a/templates/integrations/add_signal.html b/templates/integrations/add_signal.html index 461759d8..a13936df 100644 --- a/templates/integrations/add_signal.html +++ b/templates/integrations/add_signal.html @@ -39,20 +39,20 @@
-
+
+ value="{{ form.phone.value|default:"" }}"> - {% if form.value.errors %} + {% if form.phone.errors %}
- {{ form.value.errors|join:"" }} + {{ form.phone.errors|join:"" }}
{% else %} diff --git a/templates/integrations/add_sms.html b/templates/integrations/add_sms.html index 55e9f186..66843811 100644 --- a/templates/integrations/add_sms.html +++ b/templates/integrations/add_sms.html @@ -50,20 +50,20 @@ {% endif %}
-
+
+ value="{{ form.phone.value|default:"" }}"> - {% if form.value.errors %} + {% if form.phone.errors %}
- {{ form.value.errors|join:"" }} + {{ form.phone.errors|join:"" }}
{% else %} diff --git a/templates/integrations/add_whatsapp.html b/templates/integrations/add_whatsapp.html index 2d6696ce..fba677f9 100644 --- a/templates/integrations/add_whatsapp.html +++ b/templates/integrations/add_whatsapp.html @@ -50,20 +50,20 @@
-
+
+ value="{{ form.phone.value|default:"" }}"> - {% if form.value.errors %} + {% if form.phone.errors %}
- {{ form.value.errors|join:"" }} + {{ form.phone.errors|join:"" }}
{% else %}