64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
import urllib
|
|
import urllib2
|
|
import logging
|
|
|
|
from django.db import models
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from passerelle.base.models import BaseResource
|
|
from passerelle.sms import SMSGatewayMixin
|
|
|
|
logger = logging.getLogger('passerelle.apps.oxyd')
|
|
|
|
|
|
class OxydError(Exception):
|
|
pass
|
|
|
|
|
|
class OxydSMSGateway(BaseResource, SMSGatewayMixin):
|
|
username = models.CharField(max_length=64)
|
|
password = models.CharField(max_length=64)
|
|
default_country_code = models.CharField(max_length=3, default=u'33')
|
|
# FIXME: add regexp field, to check destination and from format
|
|
|
|
class Meta:
|
|
verbose_name = 'Oxyd'
|
|
db_table = 'sms_oxyd'
|
|
|
|
@classmethod
|
|
def get_icon_class(cls):
|
|
return 'phone'
|
|
|
|
def send_msg(self, text, sender, destinations):
|
|
"""Send a SMS using the Oxyd provider"""
|
|
# unfortunately it lacks a batch API...
|
|
destinations = self.clean_numbers(destinations,
|
|
self.default_country_code, prefix='')
|
|
for dest in destinations:
|
|
params = urllib.urlencode({
|
|
'id': self.username,
|
|
'pass': self.password,
|
|
'num': number,
|
|
'sms': text.encode('utf-8'),
|
|
'flash': '0'
|
|
})
|
|
try:
|
|
r = urllib2.urlopen('http://sms.oxyd.fr/send.php', params)
|
|
except Exception, e:
|
|
# XXX: add proper handling of errors
|
|
logger.error('urlopen oxyd.fr failed : %s', e)
|
|
raise OxydError('urlopen oxyd.fr failed : %s' % e)
|
|
else:
|
|
line = r.read()
|
|
code = line.split()[0]
|
|
if code != '200':
|
|
logger.error('OXYD error: %r', line)
|
|
raise OxydError('OXYD error: %r' % line)
|
|
r.close()
|
|
|
|
def get_sms_left(self, type='standard'):
|
|
raise NotImplementedError
|
|
|
|
def get_money_left(self):
|
|
raise NotImplementedError
|