transports: check mobile numbers use the french format

This commit is contained in:
Benjamin Dauvergne 2013-06-18 18:06:36 +02:00
parent 2d5db8a270
commit a1e218dd3b
1 changed files with 10 additions and 2 deletions

View File

@ -87,6 +87,7 @@ class SMSTransport(object):
'portail_citoyen_announces/body_{category}.txt',
'portail_citoyen_announces/body.txt',
]
mobile_re = re.compile('^0[67][0-9]{8}$')
def __init__(self, url, from_mobile, login=None, password=None, identifier='sms'):
self.url = url
@ -104,7 +105,14 @@ class SMSTransport(object):
def get_sms(self, category):
qs = self.get_subscriptions(category)
return qs.values_list('identifier', flat=True).distinct()
for subscription in qs:
sms = ''
if subscription.identifier:
sms = subscription.identifier
elif subscription.user:
sms = subscription.user.mobile
if self.mobile_re.match(sms):
yield sms
def send(self, announce):
category = announce.category
@ -113,7 +121,7 @@ class SMSTransport(object):
category=category.identifier, identifier=self.identifier)
ctx = Context({ 'announce': announce, 'site': site, 'category': category })
body = body_template.render(ctx)
sms = self.get_sms(category)
sms = list(self.get_sms(category))
logger.info(u'sending announce %(announce)s through %(mode)s to %(count)s emails',
dict(announce=announce, mode=self.identifier, count=len(sms)))
try: