Skip to content
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
language: python
python:
- "2.7"
- "3.5"
env:
- DJANGO_VERSION=1.4
- DJANGO_VERSION=1.5
- DJANGO_VERSION=1.6
- DJANGO_VERSION=1.7
- DJANGO_VERSION=1.8
- DJANGO_VERSION=1.9
- DJANGO_VERSION=1.10
- DJANGO_VERSION=1.11
install:
- pip install -q Django==$DJANGO_VERSION
- pip install -q -r requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion encrypted_fields/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '1.1.2'
__version__ = '1.2.0a1'

from .fields import *
25 changes: 8 additions & 17 deletions encrypted_fields/fields.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@

import os
import types
import binascii

import django
from django.db import models
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import force_text
from django.utils.functional import cached_property

try:
from django.utils.encoding import smart_text
except ImportError:
from django.utils.encoding import smart_str as smart_text

from keyczar import keyczar


Expand Down Expand Up @@ -159,25 +154,24 @@ def get_internal_type(self):
def load_crypter(self):
self._crypter = self._crypter_klass(self.keydir)

def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, expression, connection, **kwargs):
return self.to_python(value)

def to_python(self, value):
if value is None or not isinstance(value, types.StringTypes):
if value is None or not isinstance(value, (str, bytes)):
return value

if self.prefix and value.startswith(self.prefix):
value = value[len(self.prefix):]

try:
value = self.crypter().decrypt(value)
value = value.decode('unicode_escape')
value = force_text(self.crypter().decrypt(value))
except keyczar.errors.KeyczarError:
pass
except UnicodeEncodeError:
pass
except binascii.Error:
pass
pass

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

Expand All @@ -187,11 +181,7 @@ def get_prep_value(self, value):
if value is None or value == '' or self.decrypt_only:
return value

if isinstance(value, types.StringTypes):
value = value.encode('unicode_escape')
value = value.encode('ascii')
else:
value = str(value)
value = force_text(value)

return self.prefix + self.crypter().encrypt(value)

Expand Down Expand Up @@ -228,6 +218,7 @@ class EncryptedDateTimeField(EncryptedFieldMixin, models.DateTimeField):


class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):

@cached_property
def validators(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions encrypted_fields/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_decrypt_only_field(self):
model.save()

plaintext = self.get_db_value('decrypt_only', model.id)
self.assertEquals(plaintext, known_plaintext)
self.assertEqual(plaintext, known_plaintext)

def test_decrypt_only_plaintext(self):
known_plaintext = 'I am so plain and ordinary'
Expand All @@ -129,7 +129,7 @@ def test_decrypt_only_plaintext(self):
model.save()

plaintext = self.get_db_value('decrypt_only', model.id)
self.assertEquals(plaintext, known_plaintext)
self.assertEqual(plaintext, known_plaintext)

def test_char_field_encrypted(self):
plaintext = 'Oh hi, test reader!'
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Django>=1.4
python-keyczar==0.715
Django>=1.8
python3-keyczar==0.71rc0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
version=version,
install_requires=[
'Django>=1.4',
'python-keyczar>=0.71c',
'python3-keyczar>=0.71c',
],
)