Skip to content

Commit 023afc5

Browse files
committed
fix the unicode issue
1 parent 23b0216 commit 023afc5

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

encrypted_fields/fields.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ def to_python(self, value):
154154

155155
try:
156156
value = self.crypter().decrypt(value)
157+
value = value.decode('unicode_escape')
157158
except keyczar.errors.KeyczarError:
158159
pass
160+
except UnicodeEncodeError:
161+
pass
159162

160163
return super(EncryptedFieldMixin, self).to_python(value)
161164

@@ -164,7 +167,14 @@ def get_prep_value(self, value):
164167

165168
if value is None or value == '' or self.decrypt_only:
166169
return value
167-
return self.prefix + self.crypter().encrypt(smart_text(value))
170+
171+
if isinstance(value, types.StringTypes):
172+
value = value.encode('unicode_escape')
173+
value = value.encode('ascii')
174+
else:
175+
value = str(value)
176+
177+
return self.prefix + self.crypter().encrypt(value)
168178

169179
def get_db_prep_value(self, value, connection, prepared=False):
170180
if not prepared:

encrypted_fields/tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_char_field_encrypted(self):
124124
self.assertEqual(fresh_model.char, plaintext)
125125

126126
def test_unicode_encrypted(self):
127-
plaintext = 'Oh hi, test reader! 🐱'
127+
plaintext = u'Oh hi, test reader! 🐱'
128128

129129
model = TestModel()
130130
model.char = plaintext

0 commit comments

Comments
 (0)