Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 Updates #31

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 *
24 changes: 8 additions & 16 deletions encrypted_fields/fields.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@

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 import six
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 @@ -163,21 +159,20 @@ def from_db_value(self, value, expression, connection, context):
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, six.string_types):
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 +182,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 +219,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',
],
)