summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Dauvergne <bdauvergne@entrouvert.com>2016-06-16 08:00:46 (GMT)
committerBenjamin Dauvergne <bdauvergne@entrouvert.com>2016-06-16 12:18:00 (GMT)
commit05dfdc5aafb9495209309911400252b365a2cd96 (patch)
treeb62a0aac89ebdf58191b3be084e8fe0acdeb94da
parent7586ed372cc15780547cacb40b732b528c871fb0 (diff)
downloadeopayment-05dfdc5aafb9495209309911400252b365a2cd96.zip
eopayment-05dfdc5aafb9495209309911400252b365a2cd96.tar.gz
eopayment-05dfdc5aafb9495209309911400252b365a2cd96.tar.bz2
common: force string to unicode in Form
-rw-r--r--eopayment/common.py25
1 files changed, 17 insertions, 8 deletions
diff --git a/eopayment/common.py b/eopayment/common.py
index b12490c..9b58da4 100644
--- a/eopayment/common.py
+++ b/eopayment/common.py
@@ -27,6 +27,15 @@ ERROR = 99
ORDERID_TRANSACTION_SEPARATOR = '!'
+def force_text(s, encoding='utf-8'):
+ if isinstance(s, unicode):
+ return s
+ try:
+ return unicode(s, encoding)
+ except UnicodeDecodeError:
+ return unicode(s)
+
+
class ResponseError(Exception):
pass
@@ -129,20 +138,20 @@ class Form(object):
s += ')'
return s
+ def escape(self, s):
+ return cgi.escape(force_text(s, self.encoding).encode(self.encoding))
+
def __str__(self):
s = '<form method="%s" action="%s">' % (self.method, self.url)
for field in self.fields:
- s += '\n <input type="%s" name="%s" value="%s"/>' % (
- cgi.escape(field['type'].encode(self.encoding)),
- cgi.escape(field['name'].encode(self.encoding)),
- cgi.escape(field['value'].encode(self.encoding)))
+ s += '\n <input type="%s" name="%s" value="%s"/>' % (self.escape(field['type']),
+ self.escape(field['name']),
+ self.escape(field['value']))
s += '\n <input type="submit"'
if self.submit_name:
- s += ' name="%s"' % cgi.escape(
- self.submit_name.encode(self.encoding))
+ s += ' name="%s"' % self.escape(self.submit_name)
if self.submit_value:
- s += ' value="%s"' % cgi.escape(
- self.submit_value.encode(self.encoding))
+ s += ' value="%s"' % self.escape(self.submit_value)
s += ' />'
s += '\n</form>'
return s