Skip to content

Commit f6b1e94

Browse files
committed
Merge pull request #14 from patientdev/subfieldbaseReplace
Replace 'SubfieldBase' with 'from_db_value()'
2 parents f3e3f5f + 04d12cc commit f6b1e94

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ env:
66
- DJANGO_VERSION=1.5
77
- DJANGO_VERSION=1.6
88
- DJANGO_VERSION=1.7
9+
- DJANGO_VERSION=1.8
10+
- DJANGO_VERSION=1.9
911
install:
1012
- pip install -q Django==$DJANGO_VERSION --use-mirrors
1113
- pip install -q -r requirements.txt --use-mirrors

encrypted_fields/fields.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import types
44

5+
import django
56
from django.db import models
67
from django.conf import settings
78
from django.core.exceptions import ImproperlyConfigured
@@ -45,8 +46,8 @@ class EncryptedFieldMixin(object):
4546
data in your database. This lives in a Keyczar key directory specified by:
4647
the setting - settings.ENCRYPTED_FIELDS_KEYDIR -
4748
48-
Optionally, you can name specific encryption keys for data-specific purposes
49-
in your model such as:
49+
Optionally, you can name specific encryption keys for data-specific
50+
purposes in your model such as:
5051
special_data = EncrytpedCharField( ..., keyname='special_data' )
5152
5253
The Mixin will handle the encryption/decryption seamlessly, but native
@@ -77,7 +78,9 @@ class EncryptedFieldMixin(object):
7778
A ValueError will be raised if the encrypted length of the data (including
7879
prefix if specified) is greater than the max_length of the field.
7980
"""
80-
__metaclass__ = models.SubfieldBase
81+
82+
if django.VERSION < (1, 8):
83+
__metaclass__ = models.SubfieldBase
8184

8285
def __init__(self, *args, **kwargs):
8386
"""
@@ -137,7 +140,11 @@ def __init__(self, *args, **kwargs):
137140

138141
# Ensure the encrypted data does not exceed the max_length
139142
# of the database. Data truncation is a possibility otherwise.
140-
self.enforce_max_length = getattr(settings, 'ENFORCE_MAX_LENGTH', False)
143+
self.enforce_max_length = getattr(
144+
settings,
145+
'ENFORCE_MAX_LENGTH',
146+
False
147+
)
141148
if not self.enforce_max_length:
142149
self.enforce_max_length = kwargs.pop('enforce_max_length', False)
143150

@@ -149,6 +156,9 @@ def crypter(self):
149156
def get_internal_type(self):
150157
return 'TextField'
151158

159+
def from_db_value(self, value, expression, connection, context):
160+
return self.to_python(value)
161+
152162
def to_python(self, value):
153163
if value is None or not isinstance(value, types.StringTypes):
154164
return value
@@ -186,10 +196,9 @@ def get_db_prep_value(self, value, connection, prepared=False):
186196

187197
if self.enforce_max_length:
188198
if (
189-
value
190-
and hasattr(self, 'max_length')
191-
and self.max_length
192-
and len(value) > self.max_length
199+
value and hasattr(self, 'max_length') and
200+
self.max_length and
201+
len(value) > self.max_length
193202
):
194203
raise ValueError(
195204
'Field {0} max_length={1} encrypted_len={2}'.format(
@@ -218,8 +227,8 @@ class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):
218227
def validators(self):
219228
"""
220229
See issue https://github.com/defrex/django-encrypted-fields/issues/7
221-
Need to keep all field validators, but need to change `get_internal_type` on the fly
222-
to prevent fail in django 1.7.
230+
Need to keep all field validators, but need to change
231+
`get_internal_type` on the fly to prevent fail in django 1.7.
223232
"""
224233
self.get_internal_type = lambda: 'IntegerField'
225234
return models.IntegerField.validators.__get__(self)

encrypted_fields/tests.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ def decrypt(self, ciphertext):
3939

4040
class TestModel(models.Model):
4141
char = EncryptedCharField(max_length=255, null=True, blank=True)
42-
prefix_char = EncryptedCharField(max_length=255, prefix='ENCRYPTED:::', blank=True)
43-
decrypt_only = EncryptedCharField(max_length=255, decrypt_only=True, blank=True)
42+
prefix_char = EncryptedCharField(
43+
max_length=255,
44+
prefix='ENCRYPTED:::',
45+
blank=True
46+
)
47+
decrypt_only = EncryptedCharField(
48+
max_length=255,
49+
decrypt_only=True,
50+
blank=True
51+
)
4452
short_char = EncryptedCharField(
4553
max_length=50, null=True, enforce_max_length=True, blank=True)
4654

@@ -53,11 +61,16 @@ class TestModel(models.Model):
5361
boolean = EncryptedBooleanField(default=False, blank=True)
5462

5563
char_custom_crypter = EncryptedCharField(
56-
max_length=255, null=True,crypter_klass=TestCrypter, blank=True)
64+
max_length=255,
65+
null=True,
66+
crypter_klass=TestCrypter,
67+
blank=True
68+
)
5769

5870

5971
class FieldTest(TestCase):
60-
IS_POSTGRES = settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2'
72+
IS_POSTGRES = settings.DATABASES['default']['ENGINE'] == \
73+
'django.db.backends.postgresql_psycopg2'
6174

6275
def get_db_value(self, field, model_id):
6376
cursor = connection.cursor()

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Django>=1.4
2-
python-keyczar==0.71c
2+
python-keyczar==0.715

testsettings.py

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
'encrypted_fields',
1515
)
1616

17+
MIDDLEWARE_CLASSES = []
18+
1719
ENCRYPTED_FIELDS_KEYDIR = os.path.join(os.path.dirname(__file__), 'testkey')

0 commit comments

Comments
 (0)