89 lines
3.4 KiB
Python
89 lines
3.4 KiB
Python
import urllib
|
|
import logging
|
|
import json
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.db import models
|
|
|
|
from passerelle.base.models import BaseResource
|
|
from passerelle.sms import SMSGatewayMixin
|
|
|
|
logger = logging.getLogger('passerelle.apps.ovh')
|
|
|
|
|
|
class OVHError(Exception):
|
|
pass
|
|
|
|
|
|
class OVHSMSGateway(BaseResource, SMSGatewayMixin):
|
|
URL = 'https://www.ovh.com/cgi-bin/sms/http2sms.cgi'
|
|
MESSAGES_CLASSES = (
|
|
(0, _('Message are directly shown to users on phone screen '
|
|
'at reception. The message is never stored, neither in the '
|
|
'phone memory nor in the SIM card. It is deleted as '
|
|
'soon as the user validate the display.')),
|
|
(1, _('Messages are stored in the phone memory, or in the '
|
|
'SIM card if the memory is full. ')),
|
|
(2, _('Messages are stored in the SIM card.')),
|
|
(3, _('Messages are stored in external storage like a PDA or '
|
|
'a PC.')),
|
|
)
|
|
account = models.CharField(max_length=64)
|
|
username = models.CharField(max_length=64)
|
|
password = models.CharField(max_length=64)
|
|
msg_class = models.IntegerField(choices=MESSAGES_CLASSES,
|
|
default=1, verbose_name=_('message class'))
|
|
credit_threshold_alert = models.PositiveIntegerField(default=100)
|
|
default_country_code = models.CharField(max_length=3, default=u'33')
|
|
credit_left = models.PositiveIntegerField(default=0)
|
|
# FIXME: add regexp field, to check destination and from format
|
|
|
|
class Meta:
|
|
verbose_name = 'OVH'
|
|
db_table = 'sms_ovh'
|
|
|
|
@classmethod
|
|
def get_icon_class(cls):
|
|
return 'phone'
|
|
|
|
def send_msg(self, text, sender, destinations):
|
|
"""Send a SMS using the OVH provider"""
|
|
# unfortunately it lacks a batch API...
|
|
destinations = self.clean_numbers(destinations, self.default_country_code)
|
|
|
|
text = unicode(text).encode('utf-8')
|
|
to = ','.join(destinations)
|
|
params = {
|
|
'account': self.account.encode('utf-8'),
|
|
'login': self.username.encode('utf-8'),
|
|
'password': self.password.encode('utf-8'),
|
|
'from': sender.encode('utf-8'),
|
|
'to': to,
|
|
'message': text,
|
|
'contentType': 'text/json',
|
|
'class': self.msg_class,
|
|
}
|
|
ret = {}
|
|
try:
|
|
stream = urllib.urlopen('%s?%s' % (self.URL, urllib.urlencode(params)))
|
|
except Exception, e:
|
|
logger.error('unable to urlopen %s: %s', self.URL, e)
|
|
raise OVHError('unable to urlopen %s: %s' % (self.URL, e))
|
|
else:
|
|
result = json.loads(stream.read())
|
|
if 100 <= result['status'] < 200:
|
|
credit_left = float(result['creditLeft'])
|
|
# update credit left
|
|
OVHSMSGateway.objects.filter(id=self.id) \
|
|
.update(credit_left=credit_left)
|
|
if credit_left < self.credit_threshold_alert:
|
|
logger.error(
|
|
'credit level too low for %s: %s (threshold %s)',
|
|
self.slug, credit_left, self.credit_threshold_alert)
|
|
ret['credit_left'] = credit_left
|
|
ret['ovh_result'] = result
|
|
return ret
|
|
else:
|
|
logger.error('OVH error: %r', result)
|
|
raise OVHError('OVH error: %r' % result)
|