Skip to content

Commit

Permalink
ievv_sms...pswin: A bit smarter handling of latin-1 encoding.
Browse files Browse the repository at this point in the history
Use a CHARACTER_REPLACE_MAP + encode with errors="ignore".
  • Loading branch information
Espen A. Kristiansen committed Oct 17, 2018
1 parent 95386f4 commit 2bb8766
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ievv_opensource/ievv_sms/backends/pswin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Backend(sms_registry.AbstractSmsBackend):
The backend_id for this backend is ``pswin``.
"""
CHARACTER_REPLACE_MAP = {
'–': '-'
}

@classmethod
def get_backend_id(cls):
return 'pswin'
Expand All @@ -41,6 +45,7 @@ def __init__(self, phone_number, message,

@property
def pswin_base_url(self):
# https://wiki.pswin.com/Gateway%20HTTP%20API.ashx#Submitting_SMS_24
return 'https://simple.pswin.com'

@property
Expand All @@ -59,6 +64,16 @@ def pswin_sender(self):
def default_country_code(self):
return self._pswin_default_country_code or settings.PSWIN_DEFAULT_COUNTRY_CODE

def replace_message_characters(self, message):
for from_char, to_char in self.CHARACTER_REPLACE_MAP.items():
message = message.replace(from_char, to_char)
return message

def clean_message(self, message):
message = self.replace_message_characters(message)
message = message.encode('latin-1', errors='ignore').decode('latin-1')
return message

def clean_phone_number(self, phone_number):
phone_number = self.STRIP_WHITESPACE_PATTERN.sub('', phone_number)
if phone_number.startswith('00'):
Expand All @@ -76,8 +91,7 @@ def pswin_postdata(self):
'PW': self.pswin_password,
'RCV': self.cleaned_phone_number,
'SND': self.pswin_sender,
'TXT': self.cleaned_message.encode('ISO-8859-1'),
'Content-Type': 'text/html; ISO-8859-1'
'TXT': self.cleaned_message.encode('ISO-8859-1')
}

def send(self):
Expand Down

0 comments on commit 2bb8766

Please sign in to comment.