iro/offer/smstrade.py
branchdevel
changeset 253 e8d56537c9cc
parent 244 48fba7b4afe9
child 255 507f6f4186d8
equal deleted inserted replaced
252:d0de2ca7201a 253:e8d56537c9cc
    36     71 : "Feature nicht ueber diese Route moeglich.",
    36     71 : "Feature nicht ueber diese Route moeglich.",
    37     80 : "Uebergabe an SMS-C fehlgeschlagen.",
    37     80 : "Uebergabe an SMS-C fehlgeschlagen.",
    38     90 : "Versand nicht moeglich.",
    38     90 : "Versand nicht moeglich.",
    39     100 : "SMS wurde versendet.",
    39     100 : "SMS wurde versendet.",
    40     }
    40     }
       
    41 """statuscodes of external smstrade API"""
    41 
    42 
    42 
    43 
    43 class SmstradeException(ExternalException):
    44 class SmstradeException(ExternalException):
       
    45     """An excetion that connects the status code with the excetion string (see :attr:`statusCodes`)"""
    44     def __init__(self,status):
    46     def __init__(self,status):
    45         ExternalException.__init__(self)
    47         ExternalException.__init__(self)
    46         self.status = status
    48         self.status = status
    47         self.str_=str(status)
    49         self.str_=str(status)
    48     
    50     
    49     def __str__(self):
    51     def __str__(self):
    50         return "%s\n%s"%(ExternalException.__str__(self),self.str_)
    52         return "%s\n%s"%(ExternalException.__str__(self),self.str_)
    51 
    53 
    52 
    54 
    53 class StatusCode:
    55 class StatusCode:
    54      def __init__(self,code, exID=None, costs=Decimal("0.0"), count=0):
    56     """Class that represents the output of one smstrade request."""
       
    57     def __init__(self,code, exID=None, costs=Decimal("0.0"), count=0):
    55         self.code = code
    58         self.code = code
    56         
    59         
    57         self.exID = exID
    60         self.exID = exID
    58         self.costs = Decimal(costs)
    61         self.costs = Decimal(costs)
    59         self.count = int(count)
    62         self.count = int(count)
    60      
    63      
    61      def __str__(self):
    64     def __str__(self):
    62         if self.code in statusCodes.keys():
    65         if self.code in statusCodes.keys():
    63             return "%i: %s"%(self.code, statusCodes[self.code])
    66             return "%i: %s"%(self.code, statusCodes[self.code])
    64         
    67         
    65         return "%i: unknown statuscode."%self.code
    68         return "%i: unknown statuscode."%self.code
    66 
    69 
    67      def __int__(self):
    70     def __int__(self):
    68         return self.code
    71         return self.code
    69 
    72 
    70 class Smstrade(Provider):
    73 class Smstrade(Provider):
    71     """
    74     """A Provider to send SMS to recipients using smstrade.
    72     s. auch http://kundencenter.smstrade.de/sites/smstrade.de.kundencenter/__pdf/SMS-Gateway_HTTP_API_v2.pdf
    75     Smstrade only supports to send SMS  and four diffrent routes: ``["basic","economy","gold","direct"]``.
       
    76 
       
    77     It needs a smstrade Gateway Key https://login.smstrade.de/index.php?gateway in configuration file.
       
    78 
       
    79     smstrade API documentation:  http://kundencenter.smstrade.de/sites/smstrade.de.kundencenter/__pdf/SMS-Gateway_HTTP_API_v2.pdf
       
    80 
       
    81     The smstrade API supports a debug mode, that can be set  with :attr:`~iro.offer.provider.Provider.testmode`.
    73     """
    82     """
    74     params= {"debug":("boolean",False),
    83     params= {"debug":("boolean",False),
    75             "concat_sms":('boolean',False),
    84             "concat_sms":('boolean',False),
    76             "message_id":('boolean',False),
    85             "message_id":('boolean',False),
    77             "count":('boolean',False),
    86             "count":('boolean',False),
    78             "cost":('boolean',False),
    87             "cost":('boolean',False),
    79            }
    88            }
       
    89     '''dict for standrd values of the smstrade api, it is used to get the right values to the API.'''
       
    90 
    80     def __init__(self, name):       
    91     def __init__(self, name):       
    81         self.url = "https://gateway.smstrade.de"
    92         self.url = "https://gateway.smstrade.de"
    82         Provider.__init__(self, name, {"sms":["basic","economy","gold","direct"]})
    93         Provider.__init__(self, name, {"sms":["basic","economy","gold","direct"]})
    83         self.options.update({
    94         self.options.update({
    84             "key":Option(lambda x,y:x,long="smstrade Gateway Key https://login.smstrade.de/index.php?gateway", must=True)
    95             "key":Option(lambda x,y:x,long="smstrade Gateway Key https://login.smstrade.de/index.php?gateway", must=True)
    85             })
    96             })
    86         self.order.append("key")
    97         self.order.append("key")
    87 
    98 
    88     def send(self, route, recipient, sms):
    99     def send(self, route, recipient, sms):
    89         """send SMS with $sms to $recipients"""
   100         """send on SMS to recipients via route
       
   101         
       
   102         :param string route: A valid route ``["basic", "economy", "gold", "direct"]
       
   103         :param string recipient: Mobilenumber of recipient
       
   104         :param `iro.model.message.sms` sms: the sms to send
       
   105         :return:
       
   106             - All went ok -- :class:`iro.model.status.Status` object
       
   107             - otherwise -- an exception
       
   108         """
    90         #logger.debug('smstrade.sendSMS(%s,%s)'%(sms,  recipient))
   109         #logger.debug('smstrade.sendSMS(%s,%s)'%(sms,  recipient))
    91 
   110 
    92         route = unicode(route)
   111         route = unicode(route)
    93 
   112 
    94         if recipient.land != '49' and route == "basic":
   113         if recipient.land != '49' and route == "basic":
   103             raise RejectRecipient(recipient, status=s)
   122             raise RejectRecipient(recipient, status=s)
   104         else:
   123         else:
   105             raise SmstradeException(s)
   124             raise SmstradeException(s)
   106 
   125 
   107     def __send(self, route, to, sms):
   126     def __send(self, route, to, sms):
   108         """ This function is the main part of the request to the sms service.    
   127         """ This is the main function to request to the sms service.    
   109         The function has to return a unicode formated string that will represent the answer of the sms service
   128 
   110         to the request."""
   129         :param string route: A valid route ``["basic", "economy", "gold", "direct"]
       
   130         :param string recipient: Mobilenumber of recipient
       
   131         :param `iro.model.message.sms` sms: the sms to send
       
   132         :return: a :class:`.StatusCode` object
       
   133         """
       
   134         
   111         #logger.debug('smstrade._send(%s,%s,%s)'%( route, to, sms))
   135         #logger.debug('smstrade._send(%s,%s,%s)'%( route, to, sms))
   112         parameters= {"key": self.key,
   136         parameters= {"key": self.key,
   113                 "route": route,
   137                 "route": route,
   114                 "to": to,
   138                 "to": to,
   115                 "message": sms.content,
   139                 "message": sms.content,
   149         if len(data) == 1:
   173         if len(data) == 1:
   150             return StatusCode(int(data[0]))
   174             return StatusCode(int(data[0]))
   151         return StatusCode(int(data[0]),exID=data[1],costs=data[2],count=data[3])
   175         return StatusCode(int(data[0]),exID=data[1],costs=data[2],count=data[3])
   152 
   176 
   153     def getSendFunc(self, typ, route):
   177     def getSendFunc(self, typ, route):
       
   178         """returns  a partial :meth:`send` methed with bounded route, if typ and route is valid."""
       
   179 
   154         Provider.getSendFunc(self, typ, route)
   180         Provider.getSendFunc(self, typ, route)
   155         return partial(self.send,route)
   181         return partial(self.send,route)
   156 
   182 
   157 providers["smstrade"]=Smstrade
   183 providers["smstrade"]=Smstrade