Browse Source

Split AddSmsForm into PhoneNumberForm and PhoneUpDownForm

The PhoneNumberForm is used in "Add SMS" and "Add Phone Call" pages.
The PhoneUpDownForm is a subclass of PhoneNumberForm and
adds "up" and "down" boolean fields. It is used in "Add Signal"
and "Add WhatsApp" pages.
pull/468/head
Pēteris Caune 4 years ago
parent
commit
55a22e5043
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
10 changed files with 67 additions and 64 deletions
  1. +21
    -6
      hc/front/forms.py
  2. +3
    -3
      hc/front/tests/test_add_call.py
  3. +3
    -3
      hc/front/tests/test_add_signal.py
  4. +6
    -6
      hc/front/tests/test_add_sms.py
  5. +2
    -2
      hc/front/tests/test_add_whatsapp.py
  6. +12
    -24
      hc/front/views.py
  7. +5
    -5
      templates/integrations/add_call.html
  8. +5
    -5
      templates/integrations/add_signal.html
  9. +5
    -5
      templates/integrations/add_sms.html
  10. +5
    -5
      templates/integrations/add_whatsapp.html

+ 21
- 6
hc/front/forms.py View File

@ -193,15 +193,13 @@ class AddShellForm(forms.Form):
return json.dumps(dict(self.cleaned_data), sort_keys=True) return json.dumps(dict(self.cleaned_data), sort_keys=True)
class AddSmsForm(forms.Form):
class PhoneNumberForm(forms.Form):
error_css_class = "has-error" error_css_class = "has-error"
label = forms.CharField(max_length=100, required=False) 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 = v.encode("ascii", "ignore").decode("ascii")
stripped = stripped.replace(" ", "").replace("-", "") stripped = stripped.replace(" ", "").replace("-", "")
@ -210,6 +208,23 @@ class AddSmsForm(forms.Form):
return stripped 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): class ChannelNameForm(forms.Form):
name = forms.CharField(max_length=100, required=False) name = forms.CharField(max_length=100, required=False)


+ 3
- 3
hc/front/tests/test_add_call.py View File

@ -24,7 +24,7 @@ class AddCallTestCase(BaseTestCase):
self.assertContains(r, "upgrade to a") self.assertContains(r, "upgrade to a")
def test_it_creates_channel(self): def test_it_creates_channel(self):
form = {"label": "My Phone", "value": "+1234567890"}
form = {"label": "My Phone", "phone": "+1234567890"}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)
@ -37,14 +37,14 @@ class AddCallTestCase(BaseTestCase):
self.assertEqual(c.project, self.project) self.assertEqual(c.project, self.project)
def test_it_rejects_bad_number(self): def test_it_rejects_bad_number(self):
form = {"value": "not a phone number"}
form = {"phone": "not a phone number"}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)
self.assertContains(r, "Invalid phone number format.") self.assertContains(r, "Invalid phone number format.")
def test_it_trims_whitespace(self): def test_it_trims_whitespace(self):
form = {"value": " +1234567890 "}
form = {"phone": " +1234567890 "}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
self.client.post(self.url, form) self.client.post(self.url, form)


+ 3
- 3
hc/front/tests/test_add_signal.py View File

@ -17,7 +17,7 @@ class AddSignalTestCase(BaseTestCase):
def test_it_creates_channel(self): def test_it_creates_channel(self):
form = { form = {
"label": "My Phone", "label": "My Phone",
"value": "+1234567890",
"phone": "+1234567890",
"down": "true", "down": "true",
"up": "true", "up": "true",
} }
@ -35,7 +35,7 @@ class AddSignalTestCase(BaseTestCase):
self.assertEqual(c.project, self.project) self.assertEqual(c.project, self.project)
def test_it_obeys_up_down_flags(self): def test_it_obeys_up_down_flags(self):
form = {"label": "My Phone", "value": "+1234567890"}
form = {"label": "My Phone", "phone": "+1234567890"}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)
@ -46,7 +46,7 @@ class AddSignalTestCase(BaseTestCase):
self.assertFalse(c.signal_notify_up) self.assertFalse(c.signal_notify_up)
@override_settings(SIGNAL_CLI_USERNAME=None) @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="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.get(self.url) r = self.client.get(self.url)
self.assertEqual(r.status_code, 404) self.assertEqual(r.status_code, 404)


+ 6
- 6
hc/front/tests/test_add_sms.py View File

@ -24,7 +24,7 @@ class AddSmsTestCase(BaseTestCase):
self.assertContains(r, "upgrade to a") self.assertContains(r, "upgrade to a")
def test_it_creates_channel(self): def test_it_creates_channel(self):
form = {"label": "My Phone", "value": "+1234567890"}
form = {"label": "My Phone", "phone": "+1234567890"}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)
@ -38,13 +38,13 @@ class AddSmsTestCase(BaseTestCase):
def test_it_rejects_bad_number(self): def test_it_rejects_bad_number(self):
for v in ["not a phone number address", False, 15, "+123456789A"]: for v in ["not a phone number address", False, 15, "+123456789A"]:
form = {"value": v}
form = {"phone": v}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)
self.assertContains(r, "Invalid phone number format.") self.assertContains(r, "Invalid phone number format.")
def test_it_trims_whitespace(self): def test_it_trims_whitespace(self):
form = {"value": " +1234567890 "}
form = {"phone": " +1234567890 "}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
self.client.post(self.url, form) self.client.post(self.url, form)
@ -67,7 +67,7 @@ class AddSmsTestCase(BaseTestCase):
self.assertEqual(r.status_code, 403) self.assertEqual(r.status_code, 403)
def test_it_strips_invisible_formatting_characters(self): 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="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)
@ -77,7 +77,7 @@ class AddSmsTestCase(BaseTestCase):
self.assertEqual(c.phone_number, "+1234567890") self.assertEqual(c.phone_number, "+1234567890")
def test_it_strips_hyphens(self): def test_it_strips_hyphens(self):
form = {"label": "My Phone", "value": "+123-4567890"}
form = {"label": "My Phone", "phone": "+123-4567890"}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)
@ -87,7 +87,7 @@ class AddSmsTestCase(BaseTestCase):
self.assertEqual(c.phone_number, "+1234567890") self.assertEqual(c.phone_number, "+1234567890")
def test_it_strips_spaces(self): 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="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)


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

@ -33,7 +33,7 @@ class AddWhatsAppTestCase(BaseTestCase):
def test_it_creates_channel(self): def test_it_creates_channel(self):
form = { form = {
"label": "My Phone", "label": "My Phone",
"value": "+1234567890",
"phone": "+1234567890",
"down": "true", "down": "true",
"up": "true", "up": "true",
} }
@ -51,7 +51,7 @@ class AddWhatsAppTestCase(BaseTestCase):
self.assertEqual(c.project, self.project) self.assertEqual(c.project, self.project)
def test_it_obeys_up_down_flags(self): def test_it_obeys_up_down_flags(self):
form = {"label": "My Phone", "value": "+1234567890"}
form = {"label": "My Phone", "phone": "+1234567890"}
self.client.login(username="[email protected]", password="password") self.client.login(username="[email protected]", password="password")
r = self.client.post(self.url, form) r = self.client.post(self.url, form)


+ 12
- 24
hc/front/views.py View File

@ -1555,17 +1555,17 @@ def add_telegram(request):
def add_sms(request, code): def add_sms(request, code):
project = _get_rw_project_for_user(request, code) project = _get_rw_project_for_user(request, code)
if request.method == "POST": if request.method == "POST":
form = forms.AddSmsForm(request.POST)
form = forms.PhoneNumberForm(request.POST)
if form.is_valid(): if form.is_valid():
channel = Channel(project=project, kind="sms") channel = Channel(project=project, kind="sms")
channel.name = form.cleaned_data["label"] channel.name = form.cleaned_data["label"]
channel.value = json.dumps({"value": form.cleaned_data["value"]})
channel.value = form.get_json()
channel.save() channel.save()
channel.assign_all_checks() channel.assign_all_checks()
return redirect("hc-channels", project.code) return redirect("hc-channels", project.code)
else: else:
form = forms.AddSmsForm()
form = forms.PhoneNumberForm()
ctx = { ctx = {
"page": "channels", "page": "channels",
@ -1581,17 +1581,17 @@ def add_sms(request, code):
def add_call(request, code): def add_call(request, code):
project = _get_rw_project_for_user(request, code) project = _get_rw_project_for_user(request, code)
if request.method == "POST": if request.method == "POST":
form = forms.AddSmsForm(request.POST)
form = forms.PhoneNumberForm(request.POST)
if form.is_valid(): if form.is_valid():
channel = Channel(project=project, kind="call") channel = Channel(project=project, kind="call")
channel.name = form.cleaned_data["label"] channel.name = form.cleaned_data["label"]
channel.value = json.dumps({"value": form.cleaned_data["value"]})
channel.value = form.get_json()
channel.save() channel.save()
channel.assign_all_checks() channel.assign_all_checks()
return redirect("hc-channels", project.code) return redirect("hc-channels", project.code)
else: else:
form = forms.AddSmsForm()
form = forms.PhoneNumberForm()
ctx = { ctx = {
"page": "channels", "page": "channels",
@ -1607,23 +1607,17 @@ def add_call(request, code):
def add_whatsapp(request, code): def add_whatsapp(request, code):
project = _get_rw_project_for_user(request, code) project = _get_rw_project_for_user(request, code)
if request.method == "POST": if request.method == "POST":
form = forms.AddSmsForm(request.POST)
form = forms.PhoneUpDownForm(request.POST)
if form.is_valid(): if form.is_valid():
channel = Channel(project=project, kind="whatsapp") channel = Channel(project=project, kind="whatsapp")
channel.name = form.cleaned_data["label"] 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.save()
channel.assign_all_checks() channel.assign_all_checks()
return redirect("hc-channels", project.code) return redirect("hc-channels", project.code)
else: else:
form = forms.AddSmsForm()
form = forms.PhoneUpDownForm()
ctx = { ctx = {
"page": "channels", "page": "channels",
@ -1639,23 +1633,17 @@ def add_whatsapp(request, code):
def add_signal(request, code): def add_signal(request, code):
project = _get_rw_project_for_user(request, code) project = _get_rw_project_for_user(request, code)
if request.method == "POST": if request.method == "POST":
form = forms.AddSmsForm(request.POST)
form = forms.PhoneUpDownForm(request.POST)
if form.is_valid(): if form.is_valid():
channel = Channel(project=project, kind="signal") channel = Channel(project=project, kind="signal")
channel.name = form.cleaned_data["label"] 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.save()
channel.assign_all_checks() channel.assign_all_checks()
return redirect("hc-channels", project.code) return redirect("hc-channels", project.code)
else: else:
form = forms.AddSmsForm()
form = forms.PhoneUpDownForm()
ctx = { ctx = {
"page": "channels", "page": "channels",


+ 5
- 5
templates/integrations/add_call.html View File

@ -50,20 +50,20 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
<div class="form-group {{ form.value.css_classes }}">
<div class="form-group {{ form.phone.css_classes }}">
<label for="id_number" class="col-sm-2 control-label">Phone Number</label> <label for="id_number" class="col-sm-2 control-label">Phone Number</label>
<div class="col-sm-3"> <div class="col-sm-3">
<input <input
id="id_number" id="id_number"
type="tel" type="tel"
class="form-control" class="form-control"
name="value"
name="phone"
placeholder="+1234567890" placeholder="+1234567890"
value="{{ form.value.value|default:"" }}">
value="{{ form.phone.value|default:"" }}">
{% if form.value.errors %}
{% if form.phone.errors %}
<div class="help-block"> <div class="help-block">
{{ form.value.errors|join:"" }}
{{ form.phone.errors|join:"" }}
</div> </div>
{% else %} {% else %}
<span class="help-block"> <span class="help-block">


+ 5
- 5
templates/integrations/add_signal.html View File

@ -39,20 +39,20 @@
</div> </div>
</div> </div>
<div class="form-group {{ form.value.css_classes }}">
<div class="form-group {{ form.phone.css_classes }}">
<label for="id_number" class="col-sm-2 control-label">Phone Number</label> <label for="id_number" class="col-sm-2 control-label">Phone Number</label>
<div class="col-sm-3"> <div class="col-sm-3">
<input <input
id="id_number" id="id_number"
type="tel" type="tel"
class="form-control" class="form-control"
name="value"
name="phone"
placeholder="+1234567890" placeholder="+1234567890"
value="{{ form.value.value|default:"" }}">
value="{{ form.phone.value|default:"" }}">
{% if form.value.errors %}
{% if form.phone.errors %}
<div class="help-block"> <div class="help-block">
{{ form.value.errors|join:"" }}
{{ form.phone.errors|join:"" }}
</div> </div>
{% else %} {% else %}
<span class="help-block"> <span class="help-block">


+ 5
- 5
templates/integrations/add_sms.html View File

@ -50,20 +50,20 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
<div class="form-group {{ form.value.css_classes }}">
<div class="form-group {{ form.phone.css_classes }}">
<label for="id_number" class="col-sm-2 control-label">Phone Number</label> <label for="id_number" class="col-sm-2 control-label">Phone Number</label>
<div class="col-sm-3"> <div class="col-sm-3">
<input <input
id="id_number" id="id_number"
type="tel" type="tel"
class="form-control" class="form-control"
name="value"
name="phone"
placeholder="+1234567890" placeholder="+1234567890"
value="{{ form.value.value|default:"" }}">
value="{{ form.phone.value|default:"" }}">
{% if form.value.errors %}
{% if form.phone.errors %}
<div class="help-block"> <div class="help-block">
{{ form.value.errors|join:"" }}
{{ form.phone.errors|join:"" }}
</div> </div>
{% else %} {% else %}
<span class="help-block"> <span class="help-block">


+ 5
- 5
templates/integrations/add_whatsapp.html View File

@ -50,20 +50,20 @@
</div> </div>
</div> </div>
<div class="form-group {{ form.value.css_classes }}">
<div class="form-group {{ form.phone.css_classes }}">
<label for="id_number" class="col-sm-2 control-label">Phone Number</label> <label for="id_number" class="col-sm-2 control-label">Phone Number</label>
<div class="col-sm-3"> <div class="col-sm-3">
<input <input
id="id_number" id="id_number"
type="tel" type="tel"
class="form-control" class="form-control"
name="value"
name="phone"
placeholder="+1234567890" placeholder="+1234567890"
value="{{ form.value.value|default:"" }}">
value="{{ form.phone.value|default:"" }}">
{% if form.value.errors %}
{% if form.phone.errors %}
<div class="help-block"> <div class="help-block">
{{ form.value.errors|join:"" }}
{{ form.phone.errors|join:"" }}
</div> </div>
{% else %} {% else %}
<span class="help-block"> <span class="help-block">


Loading…
Cancel
Save