iro/offer/smstrade.py
branchdevel
changeset 215 5bacdb7e94d1
parent 214 bacf50bc46bf
child 216 ab8e2f26718e
--- a/iro/offer/smstrade.py	Sat Mar 03 03:54:53 2012 +0100
+++ b/iro/offer/smstrade.py	Mon Mar 05 19:47:08 2012 +0100
@@ -13,16 +13,17 @@
 
 
 import urllib
-import copy
 from functools import partial
+from decimal import Decimal
+#import copy
 
 from ..config import Option
 from ..model.status import Status
 from .provider import Provider, providers
 from ..error import RejectRecipient, ExternalException
 
-import logging
-logger=logging.getLogger("smstrade")
+#import logging
+#logger=logging.getLogger("smstrade")
 
 statusCodes = {10 : "Empfaengernummer nicht korrekt.",
     20 : "Absenderkennung nicht korrekt.",
@@ -39,38 +40,31 @@
     }
 
 
-class StatusException(ExternalException):
+class SmstradeException(ExternalException):
     def __init__(self,status):
         ExternalException.__init__(self)
         self.status = status
-        self.str_ = "%i: unknown statuscode."%status
-        try:
-            self.str_="%i: %s"%(status, statusCodes[int(status)])
-        except KeyError:
-            pass
+        self.str_=str(status)
+    
     def __str__(self):
         return "%s\n%s"%(ExternalException.__str__(self),self.str_)
 
 
 class StatusCode:
-     def __init__(self,code, mID=None, cost=None, count=None):
-        if code in statusCodes.keys():
-            self.code = code
-        else:
-            raise StatusException(code)
-        self.mID=mID
-        self.cost = cost
-        self.count = count
+     def __init__(self,code, exID=None, costs=Decimal("0.0"), count=0):
+        self.code = code
+        
+        self.exID = exID
+        self.costs = Decimal(costs)
+        self.count = int(count)
      
      def __str__(self):
-        try:
+        if self.code in statusCodes.keys():
             return "%i: %s"%(self.code, statusCodes[self.code])
-        except IndexError:
-            raise StatusException(self.code)
+        
+        return "%i: unknown statuscode."%self.code
 
      def __int__(self):
-        if not self.code in statusCodes.keys():
-            raise StatusException(self.code)
         return self.code
 
 class Smstrade(Provider):
@@ -92,7 +86,8 @@
 
     def send(self, route, sms, recipient):
         """send SMS with $sms to $recipients"""
-        logger.debug('smstrade.sendSMS(%s,%s)'%(sms,  recipient))
+        #logger.debug('smstrade.sendSMS(%s,%s)'%(sms,  recipient))
+
         route = unicode(route)
 
         if recipient.land != '49' and route == "basic":
@@ -100,20 +95,19 @@
         
         to ='00'+recipient.land+recipient.number
         
-        smsSendStatus = self.__send(route, to, sms)	
-        logger.info('smstrade._send(...)=%i(%s)'%(int(smsSendStatus),str(smsSendStatus)))
-        if int(smsSendStatus) in (100,):
-            return Status(self,route)
-        elif int(smsSendStatus) in (70,71,):
-            raise RejectRecipient(recipient, status=smsSendStatus)
+        s = self.__send(route, to, sms)	
+        if int(s) in (100,):
+            return Status(self,route, exID=s.exID, costs=s.costs, count=s.count)
+        elif int(s) in (70,71,):
+            raise RejectRecipient(recipient, status=s)
         else:
-            raise StatusException(smsSendStatus)
+            raise SmstradeException(s)
 
     def __send(self, route, to, sms):
         """ This function is the main part of the request to the sms service.    
         The function has to return a unicode formated string that will represent the answer of the sms service
         to the request."""
-        logger.debug('smstrade._send(%s,%s,%s)'%( route, to, sms))
+        #logger.debug('smstrade._send(%s,%s,%s)'%( route, to, sms))
         parameters= {"key": self.key,
                 "route": route,
                 "to": to,
@@ -140,20 +134,20 @@
             if p in self.params.keys():
                 if self.params[p][0] == "boolean":
                     if parameters[p] != self.params[p][1]:
-                        ps[p]=int(not self.params[p][1])
+                        ps[p]=int(bool(parameters[p]))
             else:
                 ps[p] = parameters[p]
 
         params = urllib.urlencode(ps)
-        dp=copy.deepcopy(ps)
-        dp["key"]="<KEY>"
-        logger.debug('smstrade._send-parameters:%s\n\t->%s'%(str(dp), urllib.urlencode(dp)) )
+        #dp=copy.deepcopy(ps)
+        #dp["key"]="<KEY>"
+        #logger.debug('smstrade._send-parameters:%s\n\t->%s'%(str(dp), urllib.urlencode(dp)) )
         
         response = urllib.urlopen(self.url, params)
         data = response.readlines()
         if len(data) == 1:
             return StatusCode(int(data[0]))
-        return StatusCode(int(data[0]),mID=data[1],cost=data[2],count=data[3])
+        return StatusCode(int(data[0]),exID=data[1],costs=data[2],count=data[3])
 
     def getSendFunc(self, typ, route):
         Provider.getSendFunc(self, typ, route)