2
2
import os
3
3
import types
4
4
5
+ import django
5
6
from django .db import models
6
7
from django .conf import settings
7
8
from django .core .exceptions import ImproperlyConfigured
@@ -45,8 +46,8 @@ class EncryptedFieldMixin(object):
45
46
data in your database. This lives in a Keyczar key directory specified by:
46
47
the setting - settings.ENCRYPTED_FIELDS_KEYDIR -
47
48
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:
50
51
special_data = EncrytpedCharField( ..., keyname='special_data' )
51
52
52
53
The Mixin will handle the encryption/decryption seamlessly, but native
@@ -77,7 +78,9 @@ class EncryptedFieldMixin(object):
77
78
A ValueError will be raised if the encrypted length of the data (including
78
79
prefix if specified) is greater than the max_length of the field.
79
80
"""
80
- __metaclass__ = models .SubfieldBase
81
+
82
+ if django .VERSION < (1 , 8 ):
83
+ __metaclass__ = models .SubfieldBase
81
84
82
85
def __init__ (self , * args , ** kwargs ):
83
86
"""
@@ -137,7 +140,11 @@ def __init__(self, *args, **kwargs):
137
140
138
141
# Ensure the encrypted data does not exceed the max_length
139
142
# 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
+ )
141
148
if not self .enforce_max_length :
142
149
self .enforce_max_length = kwargs .pop ('enforce_max_length' , False )
143
150
@@ -149,6 +156,9 @@ def crypter(self):
149
156
def get_internal_type (self ):
150
157
return 'TextField'
151
158
159
+ def from_db_value (self , value , expression , connection , context ):
160
+ return self .to_python (value )
161
+
152
162
def to_python (self , value ):
153
163
if value is None or not isinstance (value , types .StringTypes ):
154
164
return value
@@ -186,10 +196,9 @@ def get_db_prep_value(self, value, connection, prepared=False):
186
196
187
197
if self .enforce_max_length :
188
198
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
193
202
):
194
203
raise ValueError (
195
204
'Field {0} max_length={1} encrypted_len={2}' .format (
@@ -218,8 +227,8 @@ class EncryptedIntegerField(EncryptedFieldMixin, models.IntegerField):
218
227
def validators (self ):
219
228
"""
220
229
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.
223
232
"""
224
233
self .get_internal_type = lambda : 'IntegerField'
225
234
return models .IntegerField .validators .__get__ (self )
0 commit comments