forms: use threadind package for sending SMS in background thread, log exceptions

This commit is contained in:
Benjamin Dauvergne 2013-09-12 10:35:32 +02:00
parent 359f099e53
commit 739baa6388
1 changed files with 14 additions and 3 deletions

View File

@ -3,6 +3,8 @@ import hmac
import thread
import datetime
import hashlib
import logging
import threading
from django.forms import (ModelForm, Form, Textarea, EmailField, CharField, ModelChoiceField)
@ -31,6 +33,10 @@ from .validators import phone_normalize, validate_fr_be_phone
from .middleware import get_extra
from .utils import mime_types_to_extensions
logger = logging.getLogger(__name__)
class RecipientForm(object):
'''
Base form mixin for forms containing a RecipienField, i.e. all
@ -424,9 +430,14 @@ class ProfileForm(ModelForm):
code = '%06d' % (int(code, 16) % 1000000)
key = '%s-code' % self.prefix if self.prefix else 'code'
if self.data.get(key, '').strip() != code:
sms_carrier = notification.SMSNotifier.get_carrier()
thread.start_new_thread(sms_carrier.send_sms,
((mobile_phone,), 'code is ' + code))
def send_sms(mobile_phone, code):
try:
sms_carrier = notification.SMSNotifier.get_carrier()
sms_carrier.send_sms((mobile_phone,), 'code is ' + code)
except Exception:
logger.exception('failure in SMS background thread')
thread = threading.Thread(target=send_sms, args=(mobile_phone, code))
thread.start()
self.fields['code'] = CharField(label='Code')
i = self.layout.fields.index('mobile_phone')
self.layout.fields.insert(i+1, 'code')