Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
add a default number prefix
Browse files Browse the repository at this point in the history
 - prefills the number field on the contact form
  • Loading branch information
monty5811 committed Oct 31, 2016
1 parent eec5d3e commit d4765af
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
All notable changes to this project will be documented in this file.

## [Unreleased]
### Added
- Default prefix for phone numbers (set at `/config/site/`) that prefills the new contact form

### Changed
- Improve handling of scheduled messages
- Improve handling of scheduled messages (**note**, already queued messages will be sent after upgrading, but their status can only be view in the admin panel, you may want to cancel queued messages before upgrading just in case)
- Update dependencies

## [v1.13.1]
Expand Down
7 changes: 7 additions & 0 deletions apostello/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def fetch_default_reply(msg=''):
return replies[msg]


def get_default_number_prefix():
"""Fetch default number prefix from database."""
from site_config.models import SiteConfiguration
site_config = SiteConfiguration.get_solo()
return site_config.default_number_prefix


def retry_request(url, http_method, *args, **kwargs):
"""Make a http request and retry 3 times if it fails."""
assert http_method in ['get', 'post', 'delete', 'patch', 'put']
Expand Down
8 changes: 7 additions & 1 deletion apostello/validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
Expand Down Expand Up @@ -73,3 +72,10 @@ def less_than_sms_char_limit(value):
'You have exceeded the maximum char limit of {0}.'.
format(sms_char_lim)
)


def validate_starts_with_plus(value):
"""Ensure value starts with a `+`."""
if value.startswith('+'):
return
raise ValidationError('Phone numbers must start with a "+".')
9 changes: 7 additions & 2 deletions apostello/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from apostello.exceptions import ArchivedItemException
from apostello.mixins import ProfilePermsMixin
from apostello.models import Keyword
from apostello.utils import exists_and_archived
from apostello.utils import exists_and_archived, get_default_number_prefix


class SimpleView(ProfilePermsMixin, View):
Expand Down Expand Up @@ -60,7 +60,12 @@ def get(self, request, *args, **kwargs):
context['sms_history'] = True
except KeyError:
# otherwise, use a blank form
form = self.form_class
if self.identifier == 'recipient':
form = self.form_class(
initial={'number': get_default_number_prefix()}
)
else:
form = self.form_class()
context['submit_text'] = "Submit"

context['form'] = form
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2016-10-31 19:01
from __future__ import unicode_literals

import apostello.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('site_config', '0007_siteconfiguration_auto_add_new_groups'),
]

operations = [
migrations.AddField(
model_name='siteconfiguration',
name='default_number_prefix',
field=models.CharField(blank=True, default='', help_text="This value will be used to prepopulate the new contact form use this if you don't want to have to type +xx every time.", max_length=5, validators=[apostello.validators.validate_starts_with_plus]),
),
]
10 changes: 9 additions & 1 deletion site_config/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models
from solo.models import SingletonModel

from apostello.validators import less_than_sms_char_limit
from apostello.validators import less_than_sms_char_limit, validate_starts_with_plus


class SiteConfiguration(SingletonModel):
Expand All @@ -18,6 +18,14 @@ class SiteConfiguration(SingletonModel):
' The sending forms use this value to limit the size of messages.'
' Check the Twilio pricing docs for pricing information.'
)
default_number_prefix = models.CharField(
max_length=5,
default='',
blank=True,
help_text='This value will be used to prepopulate the new contact form'
' use this if you don\'t want to have to type +xx every time.',
validators=[validate_starts_with_plus]
)
disable_all_replies = models.BooleanField(
default=False,
help_text='Tick this box to disable all automated replies.'
Expand Down
32 changes: 32 additions & 0 deletions tests/functional_tests/apostello/test_default_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from time import sleep

import pytest

URI = '/recipient/new/'


def load_page(b, wt, url):
b.get(url)
assert url in b.current_url
sleep(wt)
return b


@pytest.mark.django_db(transaction=True)
@pytest.mark.slow
@pytest.mark.selenium
class TestDefaultPrefix:
def test_default_prefix(
self, live_server, browser_in, users, driver_wait_time
):
"""Test prefix shows on form."""
from site_config.models import SiteConfiguration
config = SiteConfiguration.get_solo()
config.default_number_prefix = '+45'
config.save()
b = load_page(browser_in, driver_wait_time, live_server + URI)
assert '+45' in b.page_source
config.default_number_prefix = ''
config.save()
b = load_page(browser_in, driver_wait_time, live_server + URI)
assert '+45' not in b.page_source
13 changes: 12 additions & 1 deletion tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from apostello.validators import (
gsm_validator, less_than_sms_char_limit, no_overlap_keyword,
not_twilio_num, twilio_reserved, validate_lower
not_twilio_num, twilio_reserved, validate_lower, validate_starts_with_plus
)
from site_config.models import SiteConfiguration

Expand Down Expand Up @@ -95,3 +95,14 @@ def test_no_name_raises(self):
't %name%' *
(s.sms_char_limit - settings.MAX_NAME_LENGTH + len('%name%'))
)

class TestStartswithPlus:
def test_no_plus(self):
with pytest.raises(ValidationError):
validate_starts_with_plus('nope')

def test_plus(self):
validate_starts_with_plus('+44')
validate_starts_with_plus('+1')
validate_starts_with_plus('+test')
validate_starts_with_plus('+yup')

0 comments on commit d4765af

Please sign in to comment.