standardize phonenumber format (#23443)
This commit is contained in:
parent
58bc5c436e
commit
d70894fadd
|
@ -0,0 +1,26 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
from corbo.utils import format_phonenumber
|
||||
|
||||
|
||||
def format_sms_identifiers(apps, schema_editor):
|
||||
Subscription = apps.get_model('corbo', 'Subscription')
|
||||
uri = 'sms:'
|
||||
for subscription in Subscription.objects.filter(identifier__startswith=uri):
|
||||
_, phonenumber = subscription.identifier.split(':', 1)
|
||||
subscription.identifier = uri + format_phonenumber(phonenumber)
|
||||
subscription.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('corbo', '0010_broadcast_delivery_count'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(format_sms_identifiers, format_sms_identifiers),
|
||||
]
|
|
@ -177,7 +177,6 @@ class Broadcast(models.Model):
|
|||
self.deliver_time = timezone.now()
|
||||
self.save()
|
||||
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('sent')
|
||||
ordering = ('-deliver_time',)
|
||||
|
@ -198,3 +197,12 @@ class Subscription(models.Model):
|
|||
|
||||
class Meta:
|
||||
unique_together = ('category', 'identifier', 'uuid')
|
||||
|
||||
def clean(self):
|
||||
if 'sms:' in self.identifier:
|
||||
uri, phonenumber = self.identifier.split(':', 1)
|
||||
self.identifier = '%s:%s' % (uri, utils.format_phonenumber(phonenumber))
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.clean()
|
||||
return super(Subscription, self).save(*args, **kwargs)
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import re
|
||||
import logging
|
||||
import requests
|
||||
import urlparse
|
||||
|
@ -103,3 +104,16 @@ def send_sms(content, destinations):
|
|||
else:
|
||||
logger.error('SMS send requested but no SMS gateway defined.')
|
||||
return sent
|
||||
|
||||
|
||||
def format_phonenumber(phonenumber):
|
||||
'''formats a given string into a phone number by removing:
|
||||
1. parenthesis and their content
|
||||
2. every characters but numbers and plus signs
|
||||
3. all plus signs after the first character.
|
||||
'''
|
||||
phonenumber = re.sub('\(.*?\)', '', phonenumber)
|
||||
phonenumber = re.sub('[^\d+]', '', phonenumber)
|
||||
if phonenumber:
|
||||
phonenumber = phonenumber[0] + re.sub('[\+]', '', phonenumber[1:])
|
||||
return phonenumber
|
||||
|
|
|
@ -131,3 +131,9 @@ def tenant(transactional_db, request, tenant_base):
|
|||
return Tenant(domain_url=name,
|
||||
schema_name=name.replace('-', '_').replace('.', '_'))
|
||||
return make_tenant('corbo.example.net')
|
||||
|
||||
|
||||
@pytest.fixture(params=['06 10 20 30 40', '+33 6 10 20 30 40',
|
||||
'0033610203040', '+33 (0) 6 10 20 30 40', '0033+610203040+'])
|
||||
def phonenumber(request):
|
||||
return request.param
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import pytest
|
||||
|
||||
import urllib
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
|
@ -70,7 +72,7 @@ def test_get_subscriptions(app, categories, announces, user):
|
|||
|
||||
for identifier, name in channel_choices:
|
||||
for category in categories:
|
||||
uri = '%s:%s' % (identifier, uuid)
|
||||
uri = '%s:%s' % (identifier, '00339988776655')
|
||||
Subscription.objects.create(identifier=uri, uuid=uuid, category=category)
|
||||
resp = app.get(reverse('subscriptions'), params={'uuid': uuid})
|
||||
assert len(resp.json['data']) == 2
|
||||
|
@ -141,12 +143,14 @@ def test_simple_email_subscription(app, categories, user):
|
|||
assert transport['id'] == 'mailto'
|
||||
assert transport['text'] == 'mailto'
|
||||
|
||||
def test_simple_sms_subscription(app, categories, user):
|
||||
def test_simple_sms_subscription(app, categories, user, phonenumber):
|
||||
payload = {'category_id': 'alerts', 'transports': ['sms']}
|
||||
uuid = uuid4()
|
||||
url = '/api/subscribe/?uuid=%s&mobile=0607080900' % uuid
|
||||
url_params = urllib.urlencode({'uuid': uuid, 'mobile': phonenumber})
|
||||
url = '/api/subscribe/?' + url_params
|
||||
app.authorization = ('Basic', ('john.doe', 'password'))
|
||||
resp = app.post_json(url, params=payload, status=200)
|
||||
assert Subscription.objects.get(uuid=uuid).identifier in ['sms:0610203040', 'sms:+33610203040', 'sms:0033610203040']
|
||||
resp = app.get('/api/subscriptions/?uuid=%s' % uuid)
|
||||
data = resp.json['data']
|
||||
print resp.json
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
import uuid
|
||||
|
||||
import pytest
|
||||
|
||||
from django.db import connection
|
||||
from django.db.migrations.executor import MigrationExecutor
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
def test_subscription_sms_identifier_format_migration():
|
||||
executor = MigrationExecutor(connection)
|
||||
app = 'corbo'
|
||||
migrate_from = [(app, '0009_auto_20170120_1533')]
|
||||
migrate_to = [(app, '0011_auto_20180426_1334')]
|
||||
executor.migrate(migrate_from)
|
||||
executor.loader.build_graph()
|
||||
old_apps = executor.loader.project_state(migrate_from).apps
|
||||
Category = old_apps.get_model('corbo', 'Category')
|
||||
Subscription = old_apps.get_model('corbo', 'Subscription')
|
||||
category = Category.objects.create(name='Science', slug='science')
|
||||
for identifier in ('sms:06 10 20 30 40', 'sms:+33 6 10 20 30 40', 'sms:0033610203040',
|
||||
'mailto:john@doe.com', 'sms:+33 (0) 6 10 20 30 40', 'sms:06.10.20:30.40.'):
|
||||
Subscription.objects.create(uuid=uuid.uuid4().hex, category=category, identifier=identifier)
|
||||
executor.migrate(migrate_to)
|
||||
executor.loader.build_graph()
|
||||
apps = executor.loader.project_state(migrate_from).apps
|
||||
Subscription = apps.get_model('corbo', 'Subscription')
|
||||
assert Subscription.objects.count() == 6
|
||||
valid_sms_identifier = ['sms:0610203040', 'sms:+33610203040', 'sms:0033610203040']
|
||||
for subscription in Subscription.objects.filter(identifier__startswith='sms:'):
|
||||
assert subscription.identifier in valid_sms_identifier
|
Reference in New Issue