# -*- coding: utf-8 -*-

import requests, logging
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import force_unicode

from .base import BaseSmsBackend

SMSPUBLI_API_URL = 'https://secure.gateway360.com/api/push/'
SMSPUBLI_API_VERSION = 'HTTPV3'
SMSPUBLI_DC = 'SMS'
SMSPUBLI_DR = 0
SMSPUBLI_ROUTE = 2

SMSPUBLI_USERNAME = getattr(settings, 'SMSPUBLI_USERNAME', '')
SMSPUBLI_PASSWORD = getattr(settings, 'SMSPUBLI_PASSWORD', '')
SMSPUBLI_ALLOW_LONG_SMS = getattr(settings, 'SMSPUBLI_ALLOW_LONG_SMS', False)

class SMSBackend(BaseSmsBackend):
    """ 
    SMS Backend smspubli.com provider.
    """

    def _send(self, message):
        params = {
            'V': SMSPUBLI_API_VERSION, 
            'UN': SMSPUBLI_USERNAME, 
            'PWD': SMSPUBLI_PASSWORD,
            'R': SMSPUBLI_ROUTE, 
            'SA': message.from_phone,
            'DA': ','.join(message.to),
            'M': message.body.encode('latin-1'),
            'DC': SMSPUBLI_DC,
            'DR': SMSPUBLI_DR, 
            'UR': message.from_phone
        }
        if SMSPUBLI_ALLOW_LONG_SMS:
            params['LM'] = '1'

        response = requests.post(SMSPUBLI_API_URL, params)
        if response.status_code != 200 and not self.fail_silently:
            raise
        else:
            return False

        response_msg, response_code = response.content.split(':')
        if response_msg == 'OK':
            try:
                if "," in response_code:
                    codes = map(int, response_code.split(",")
                else:
                    codes = [int(response_code)]

                for code in codes:
                    if code == -5:
                        #: TODO send error signal (no $$)
                        pass
                    elif code == -3:
                        #: TODO send error signal (incorrect num)

                return True

            except (ValueError, TypeError):
                if not self.fail_silently:
                    raise
                return False
        
        return False

    def send_messages(self, messages):
        counter = 0
        for message in messages:
            res = self._send(message)
            if res:
                counter += 1

        return counter

@@ -576,18 +576,18 @@
 %0Aclass S
-MS
+ms
 Backend(
@@ -1265,12 +1265,25 @@
  200
- and
+:%0A            if 
  not
@@ -1307,32 +1307,36 @@
 ly:%0A            
+    
 raise%0A        el
@@ -1325,38 +1325,46 @@
   raise%0A        
+  
+  
 else:%0A
+    
             retu
@@ -1591,16 +1591,17 @@
 lit(%22,%22)
+)
 %0A       
@@ -1859,16 +1859,16 @@
  == -3:%0A
-
         
@@ -1924,16 +1924,45 @@
 ect num)
+%0A                        pass
 %0A%0A      

Fix smspubli backend and tested on production sandbox.