0001-sms-improve-SMSGatewayMixin.clean_numbers.patch
passerelle/apps/choosit/migrations/0002_choositsmsgateway_default_trunk_prefix.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import models, migrations |
|
5 | ||
6 | ||
7 |
class Migration(migrations.Migration): |
|
8 | ||
9 |
dependencies = [ |
|
10 |
('choosit', '0001_initial'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.AddField( |
|
15 |
model_name='choositsmsgateway', |
|
16 |
name='default_trunk_prefix', |
|
17 |
field=models.CharField(default='0', max_length=2), |
|
18 |
preserve_default=True, |
|
19 |
), |
|
20 |
] |
passerelle/apps/choosit/models.py | ||
---|---|---|
18 | 18 |
class ChoositSMSGateway(BaseResource, SMSGatewayMixin): |
19 | 19 |
key = models.CharField(max_length=64) |
20 | 20 |
default_country_code = models.CharField(max_length=3, default=u'33') |
21 |
default_trunk_prefix = models.CharField(max_length=2, default=u'0') |
|
21 | 22 |
# FIXME: add regexp field, to check destination and from format |
22 | 23 | |
23 | 24 |
class Meta: |
... | ... | |
50 | 51 |
# from http://sms.choosit.com/documentation_technique.html |
51 | 52 |
# unfortunately it lacks a batch API... |
52 | 53 |
destinations = self.clean_numbers(destinations, |
53 |
self.default_country_code, prefix='') |
|
54 |
self.default_country_code, |
|
55 |
self.default_trunk_prefix) |
|
54 | 56 |
for dest in destinations: |
55 | 57 |
params = { |
56 | 58 |
'key': self.key, |
passerelle/apps/mobyt/migrations/0002_mobytsmsgateway_default_trunk_prefix.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import models, migrations |
|
5 | ||
6 | ||
7 |
class Migration(migrations.Migration): |
|
8 | ||
9 |
dependencies = [ |
|
10 |
('mobyt', '0001_initial'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.AddField( |
|
15 |
model_name='mobytsmsgateway', |
|
16 |
name='default_trunk_prefix', |
|
17 |
field=models.CharField(default='0', max_length=2), |
|
18 |
preserve_default=True, |
|
19 |
), |
|
20 |
] |
passerelle/apps/mobyt/models.py | ||
---|---|---|
31 | 31 |
quality = models.CharField(max_length=4, choices=MESSAGES_QUALITIES, |
32 | 32 |
default='l', verbose_name=_('message quality')) |
33 | 33 |
default_country_code = models.CharField(max_length=3, default=u'33') |
34 |
default_trunk_prefix = models.CharField(max_length=2, default=u'0') |
|
34 | 35 |
# FIXME: add regexp field, to check destination and from format |
35 | 36 | |
36 | 37 |
class Meta: |
... | ... | |
61 | 62 |
def send(self, text, sender, destinations): |
62 | 63 |
"""Send a SMS using the Mobyt provider""" |
63 | 64 |
# unfortunately it lacks a batch API... |
64 |
destinations = self.clean_numbers(destinations, self.default_country_code) |
|
65 |
destinations = self.clean_numbers(destinations, |
|
66 |
self.default_country_code, |
|
67 |
self.default_trunk_prefix) |
|
65 | 68 |
rcpt = ','.join(destinations) |
66 | 69 |
params = urllib.urlencode({ |
67 | 70 |
'user': self.username, |
passerelle/apps/ovh/migrations/0002_ovhsmsgateway_default_trunk_prefix.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import models, migrations |
|
5 | ||
6 | ||
7 |
class Migration(migrations.Migration): |
|
8 | ||
9 |
dependencies = [ |
|
10 |
('ovh', '0001_initial'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.AddField( |
|
15 |
model_name='ovhsmsgateway', |
|
16 |
name='default_trunk_prefix', |
|
17 |
field=models.CharField(default='0', max_length=2), |
|
18 |
preserve_default=True, |
|
19 |
), |
|
20 |
] |
passerelle/apps/ovh/models.py | ||
---|---|---|
38 | 38 |
default=1, verbose_name=_('message class')) |
39 | 39 |
credit_threshold_alert = models.PositiveIntegerField(default=100) |
40 | 40 |
default_country_code = models.CharField(max_length=3, default=u'33') |
41 |
default_trunk_prefix = models.CharField(max_length=2, default=u'0') |
|
41 | 42 |
credit_left = models.PositiveIntegerField(default=0) |
42 | 43 |
# FIXME: add regexp field, to check destination and from format |
43 | 44 | |
... | ... | |
68 | 69 | |
69 | 70 |
def send(self, text, sender, destinations): |
70 | 71 |
"""Send a SMS using the OVH provider""" |
71 |
# unfortunately it lacks a batch API... |
|
72 |
destinations = self.clean_numbers(destinations, self.default_country_code) |
|
72 |
destinations = self.clean_numbers(destinations, |
|
73 |
self.default_country_code, |
|
74 |
self.default_trunk_prefix) |
|
73 | 75 | |
74 | 76 |
text = unicode(text).encode('utf-8') |
75 | 77 |
to = ','.join(destinations) |
passerelle/apps/oxyd/migrations/0002_oxydsmsgateway_default_trunk_prefix.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import models, migrations |
|
5 | ||
6 | ||
7 |
class Migration(migrations.Migration): |
|
8 | ||
9 |
dependencies = [ |
|
10 |
('oxyd', '0001_initial'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.AddField( |
|
15 |
model_name='oxydsmsgateway', |
|
16 |
name='default_trunk_prefix', |
|
17 |
field=models.CharField(default='0', max_length=2), |
|
18 |
preserve_default=True, |
|
19 |
), |
|
20 |
] |
passerelle/apps/oxyd/models.py | ||
---|---|---|
21 | 21 |
username = models.CharField(max_length=64) |
22 | 22 |
password = models.CharField(max_length=64) |
23 | 23 |
default_country_code = models.CharField(max_length=3, default=u'33') |
24 |
default_trunk_prefix = models.CharField(max_length=2, default=u'0') |
|
24 | 25 |
# FIXME: add regexp field, to check destination and from format |
25 | 26 | |
26 | 27 |
class Meta: |
... | ... | |
50 | 51 | |
51 | 52 |
def send(self, text, sender, destinations): |
52 | 53 |
"""Send a SMS using the Oxyd provider""" |
53 |
# unfortunately it lacks a batch API... |
|
54 | 54 |
destinations = self.clean_numbers(destinations, |
55 |
self.default_country_code, prefix='') |
|
55 |
self.default_country_code, |
|
56 |
self.default_trunk_prefix) |
|
56 | 57 |
for dest in destinations: |
57 | 58 |
params = urllib.urlencode({ |
58 | 59 |
'id': self.username, |
passerelle/sms/__init__.py | ||
---|---|---|
6 | 6 |
category = _('SMS Providers') |
7 | 7 | |
8 | 8 |
@classmethod |
9 |
def clean_numbers(cls, destinations, default_country_code, prefix='+'): |
|
9 |
def clean_numbers(cls, destinations, default_country_code='33', |
|
10 |
default_trunk_prefix='0'): # Yeah France first ! |
|
10 | 11 |
numbers = [] |
11 | 12 |
for dest in destinations: |
12 | 13 |
# most gateways needs the number prefixed by the country code, this is |
13 | 14 |
# really unfortunate. |
15 |
dest = dest.strip() |
|
14 | 16 |
number = ''.join(re.findall('[0-9]', dest)) |
15 | 17 |
if dest.startswith('+'): |
16 |
pass # it already is fully qualified
|
|
18 |
number = '00' + number
|
|
17 | 19 |
elif number.startswith('00'): |
18 | 20 |
# assumes 00 is international access code, remove it |
19 |
number = number[2:] |
|
20 |
elif number.startswith('0'): |
|
21 |
# local prefix, remove 0 and add default country code |
|
22 |
number = default_country_code + number[1:] |
|
23 |
numbers.append(prefix + number) |
|
21 |
pass |
|
22 |
elif number.startswith(default_trunk_prefix): |
|
23 |
number = '00' + default_country_code + number[len(default_trunk_prefix):] |
|
24 |
else: |
|
25 |
raise NotImplementedError('phone number %r is unsupported (no ' |
|
26 |
'international prefix, no local ' |
|
27 |
'trunk prefix)' % number) |
|
28 |
numbers.append(number) |
|
24 | 29 |
return numbers |
25 | 30 |
tests/test_sms.py | ||
---|---|---|
1 | 1 |
from passerelle.sms import SMSGatewayMixin |
2 | 2 | |
3 | 3 |
def test_clean_numbers(): |
4 |
assert SMSGatewayMixin.clean_numbers(['+ 33 12'], '33') == ['+3312']
|
|
5 |
assert SMSGatewayMixin.clean_numbers(['0 0 33 12'], '33') == ['+3312']
|
|
6 |
assert SMSGatewayMixin.clean_numbers(['0 12'], '33') == ['+3312']
|
|
4 |
assert SMSGatewayMixin.clean_numbers(['+ 33 12']) == ['003312']
|
|
5 |
assert SMSGatewayMixin.clean_numbers(['0 0 33 12']) == ['003312']
|
|
6 |
assert SMSGatewayMixin.clean_numbers(['0 12']) == ['003312']
|
|
7 | 7 | |
8 | 8 |
def test_clean_numbers_no_prefix(): |
9 |
assert SMSGatewayMixin.clean_numbers(['+ 33 12'], '33', prefix='') == ['3312'] |
|
10 |
assert SMSGatewayMixin.clean_numbers(['0 0 33 12'], '33', prefix='') == ['3312'] |
|
11 |
assert SMSGatewayMixin.clean_numbers(['0 12'], '33', prefix='') == ['3312'] |
|
9 |
assert SMSGatewayMixin.clean_numbers(['+ 33 12']) == ['003312'] |
|
10 |
assert SMSGatewayMixin.clean_numbers(['0 0 33 12']) == ['003312'] |
|
11 |
assert SMSGatewayMixin.clean_numbers(['0 12']) == ['003312'] |
|
12 |
- |