Archived
Adds logging to stderr. Finally fixes encoding issues.
This commit is contained in:
1 parent
73f00b24a9
commit
80dabbb237
1 file changed
+17
-14
+17
-14
@@ -15,6 +15,15 @@ app = Flask(__name__)
|
|||||||
app.config.from_object('config')
|
app.config.from_object('config')
|
||||||
app.json_encoder = CustomJSONEncoder
|
app.json_encoder = CustomJSONEncoder
|
||||||
|
|
||||||
|
if not app.debug:
|
||||||
|
import logging
|
||||||
|
from logging.handlers import StreamHandler
|
||||||
|
handler = StreamHandler(stream=sys.stderr)
|
||||||
|
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
handler.setLevel(logging.INFO)
|
||||||
|
app.logger.addHandler(handler)
|
||||||
|
|
||||||
def connect_db():
|
def connect_db():
|
||||||
"""Connects to our database."""
|
"""Connects to our database."""
|
||||||
db = MySQLdb.connect(
|
db = MySQLdb.connect(
|
||||||
@@ -133,14 +142,15 @@ def get_full_name(user):
|
|||||||
def fix_encoding(message):
|
def fix_encoding(message):
|
||||||
try:
|
try:
|
||||||
assert type(message) == unicode
|
assert type(message) == unicode
|
||||||
message_latin = message.encode('latin-1', 'ignore')
|
new_message = message.encode('latin-1', 'ignore')
|
||||||
except:
|
except:
|
||||||
message_latin = message
|
new_message = message
|
||||||
app.logger.warning("Unicode woes: %s", sys.exc_info())
|
app.logger.warning("Unicode woes: message_type=%s message=%s exc_info=%s",
|
||||||
|
type(message), message, sys.exc_info())
|
||||||
# Oh, never mind, let's try to send it anyway.
|
# Oh, never mind, let's try to send it anyway.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return message
|
return new_message
|
||||||
|
|
||||||
def send_sms(log_only=False, gsm=None, message=None, response_to=None, billing=False, billing_price=None, operator=None, activation_code=None):
|
def send_sms(log_only=False, gsm=None, message=None, response_to=None, billing=False, billing_price=None, operator=None, activation_code=None):
|
||||||
assert gsm
|
assert gsm
|
||||||
@@ -152,14 +162,7 @@ def send_sms(log_only=False, gsm=None, message=None, response_to=None, billing=F
|
|||||||
operator = 'ukjent'
|
operator = 'ukjent'
|
||||||
|
|
||||||
# The strings in this file are in utf-8, encode to latin-1 before sending
|
# The strings in this file are in utf-8, encode to latin-1 before sending
|
||||||
try:
|
message_latin = fix_encoding(message)
|
||||||
assert type(message) == unicode
|
|
||||||
message_latin = message.encode('latin-1', 'ignore')
|
|
||||||
except:
|
|
||||||
message_latin = message
|
|
||||||
app.logger.warning("Unicode woes: %s", sys.exc_info())
|
|
||||||
# Oh, never mind, let's try to send it anyway.
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Truncate message if too long
|
# Truncate message if too long
|
||||||
if len(message_latin) > 160:
|
if len(message_latin) > 160:
|
||||||
@@ -208,8 +211,8 @@ def send_sms(log_only=False, gsm=None, message=None, response_to=None, billing=F
|
|||||||
|
|
||||||
response = result.text.split(' ')
|
response = result.text.split(' ')
|
||||||
if response[:3] == ['Meldingen', 'er', 'sendt']:
|
if response[:3] == ['Meldingen', 'er', 'sendt']:
|
||||||
if billing and response[3:4] and response[3:4][0].isdigit():
|
if billing and len(response) == 4 and response[3].isdigit():
|
||||||
msgid = int(response[3:4][0])
|
msgid = int(response[3])
|
||||||
if billing and msgid == 0:
|
if billing and msgid == 0:
|
||||||
app.logger.warning("Did not get a msgid from Eurobate when billing as response to smsid:%s", response_to)
|
app.logger.warning("Did not get a msgid from Eurobate when billing as response to smsid:%s", response_to)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in new issue
Block a user