Commits

Matthew Schinckel committed baf052c

Gary groundwork for getting cost predictions before sending an SMS

  • Participants
  • Parent commits c20c9e7

Comments (0)

Files changed (2)

File sms/models/gateway.py

     
     success_format = models.CharField(max_length=256, null=True, blank=True,
         help_text=_(u'A regular expression that parses the response'))
-        
+    
+#     check_number_url = models.CharField(max_length=256, null=True, blank=True,
+#         help_text=_(u'The URL that can be used to check availability of sending to a number'))
+#     check_number_field = models.CharField(max_length=65, null=True, blank=True,
+#         help_text=_(u'The keyword that contains the number to check'))
+#     check_number_response_format = models.CharField(max_length=256, null=True, blank=True,
+#         help_text=_(u'A regular expression that parses the response. Keys: status, charge'))
+#     check_number_status_mapping = jsonfield.JSONField(null=True, blank=True)
+    
     class Meta:
         app_label = 'sms'
     
         message.save()
         
         return message
+    
+    def check_availability_to_send(self, number):
+        if not self.check_number_url:
+            return None
+            
+        raw_data = {}
+        raw_data.update(**self.settings)
+        raw_data[self.check_number_field] = number
+        data = urllib.urlencode(raw_data)
+        res = urllib.urlopen(self.check_number_url, data)
+        res_data = res.read()
+        
+        if self.check_number_response_format:
+            parsed_response = re.match(self.check_number_response_format, res_data).groupdict()
+            status = self.check_number_status_mapping.get(parsed_response.get('status', None), None)
+            charge = self.check_number_status_mapping.get(parsed_response.get('charge', None), None)
+            
+            

File sms/models/message.py

     def custom_reply_matcher(self, datadict):
         # Designed to be overridden.
         return None
-                
+    
+    def get_last_rate_for(self, recipient_number):
+        m = Message.objects.filter(recipient_number=recipient_number).exclude(
+            gateway_charge=None).order_by('-send_date')[0]
+        return m.gateway_charge/m.length
+
 MESSAGE_STATUSES = (
     ('Unsent', 'Unsent'),
     ('Sent', 'Sent'),