Skip to content

Commit ba29585

Browse files
author
Shane Cavanaugh
committed
Replace 'SubfieldBase' with 'from_db_value()'
SubfieldBase is deprecated as of Django 1.8 and will be removed in Django 1.10. This pull request simply removes the EncryptedFieldMixin metaclass variable and its SubfieldBase value, and adds from_db_value(). Since we don't need to do anything with our field values beyond decrypting them, from_db_value simply calls to_python() to decrypt the data and return the proper Python object. Other, minor updates: * The Travis CI config includes Django 1.8 and 1.9 * The python-keyczar is now set to 0.715, the latest version as of this commit * Line breaks for PEP8 compliance
1 parent f3e3f5f commit ba29585

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class EncryptedFieldMixin(object):
4545
data in your database. This lives in a Keyczar key directory specified by:
4646
the setting - settings.ENCRYPTED_FIELDS_KEYDIR -
4747
48-
Optionally, you can name specific encryption keys for data-specific purposes
49-
in your model such as:
48+
Optionally, you can name specific encryption keys for data-specific
49+
purposes in your model such as:
5050
special_data = EncrytpedCharField( ..., keyname='special_data' )
5151
5252
The Mixin will handle the encryption/decryption seamlessly, but native
@@ -77,7 +77,6 @@ class EncryptedFieldMixin(object):
7777
A ValueError will be raised if the encrypted length of the data (including
7878
prefix if specified) is greater than the max_length of the field.
7979
"""
80-
__metaclass__ = models.SubfieldBase
8180

8281
def __init__(self, *args, **kwargs):
8382
"""
@@ -137,7 +136,11 @@ def __init__(self, *args, **kwargs):
137136

138137
# Ensure the encrypted data does not exceed the max_length
139138
# of the database. Data truncation is a possibility otherwise.
140-
self.enforce_max_length = getattr(settings, 'ENFORCE_MAX_LENGTH', False)
139+
self.enforce_max_length = getattr(
140+
settings,
141+
'ENFORCE_MAX_LENGTH',
142+
False
143+
)
141144
if not self.enforce_max_length:
142145
self.enforce_max_length = kwargs.pop('enforce_max_length', False)
143146

@@ -149,6 +152,9 @@ def crypter(self):
149152
def get_internal_type(self):
150153
return 'TextField'
151154

155+
def from_db_value(self, value, expression, connection, context):
156+
return self.to_python(value)
157+
152158
def to_python(self, value):
153159
if value is None or not isinstance(value, types.StringTypes):
154160
return value
@@ -186,10 +192,9 @@ def get_db_prep_value(self, value, connection, prepared=False):
186192

187193
if self.enforce_max_length:
188194
if (
189-
value
190-
and hasattr(self, 'max_length')
191-
and self.max_length
192-
and len(value) > self.max_length
195+
value and hasattr(self, 'max_length') and
196+
self.max_length and
197+
len(value) > self.max_length
193198
):
194199
raise ValueError(
195200
'Field {0} max_length={1} encrypted_len={2}'.format(
@@ -218,8 +223,8 @@ class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):
218223
def validators(self):
219224
"""
220225
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.
226+
Need to keep all field validators, but need to change
227+
`get_internal_type` on the fly to prevent fail in django 1.7.
223228
"""
224229
self.get_internal_type = lambda: 'IntegerField'
225230
return models.IntegerField.validators.__get__(self)

encrypted_fields/tests.py

Lines changed: 17 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)