14
14
from django .utils .encoding import force_text as force_unicode
15
15
except ImportError :
16
16
from django .utils .encoding import force_unicode
17
-
17
+
18
18
try :
19
19
from django .utils .encoding import smart_text as smart_unicode
20
20
except ImportError :
21
21
try :
22
22
from django .utils .encoding import smart_unicode
23
23
except ImportError :
24
24
from django .forms .util import smart_unicode
25
-
25
+
26
26
from django .utils .translation import ugettext_lazy as _
27
- from django .forms .util import ErrorList
27
+ from django .forms .utils import ErrorList
28
28
from django .core .exceptions import ValidationError
29
29
30
30
try : # objectid was moved into bson in pymongo 1.9
31
31
from bson .errors import InvalidId
32
32
except ImportError :
33
33
from pymongo .errors import InvalidId
34
-
34
+
35
35
from mongodbforms .widgets import ListWidget , MapWidget , HiddenMapWidget
36
36
37
37
@@ -66,23 +66,23 @@ def to_python(self, value):
66
66
if value in EMPTY_VALUES :
67
67
return None
68
68
return value
69
-
70
-
69
+
70
+
71
71
class MongoCharField (NormalizeValueMixin , forms .CharField ):
72
72
pass
73
-
73
+
74
74
75
75
class MongoEmailField (NormalizeValueMixin , forms .EmailField ):
76
76
pass
77
-
77
+
78
78
79
79
class MongoSlugField (NormalizeValueMixin , forms .SlugField ):
80
80
pass
81
-
81
+
82
82
83
83
class MongoURLField (NormalizeValueMixin , forms .URLField ):
84
84
pass
85
-
85
+
86
86
87
87
class ReferenceField (forms .ChoiceField ):
88
88
"""
@@ -96,7 +96,7 @@ def __init__(self, queryset, empty_label="---------", *args, **kwargs):
96
96
97
97
def _get_queryset (self ):
98
98
return self ._queryset .clone ()
99
-
99
+
100
100
def _set_queryset (self , queryset ):
101
101
self ._queryset = queryset
102
102
self .widget .choices = self .choices
@@ -130,15 +130,15 @@ def clean(self, value):
130
130
return None
131
131
132
132
oid = super (ReferenceField , self ).clean (value )
133
-
133
+
134
134
try :
135
135
obj = self .queryset .get (pk = oid )
136
136
except (TypeError , InvalidId , self .queryset ._document .DoesNotExist ):
137
137
raise forms .ValidationError (
138
138
self .error_messages ['invalid_choice' ] % {'value' : value }
139
139
)
140
140
return obj
141
-
141
+
142
142
def __deepcopy__ (self , memo ):
143
143
result = super (forms .ChoiceField , self ).__deepcopy__ (memo )
144
144
result .queryset = self .queryset # self.queryset calls clone()
@@ -169,7 +169,7 @@ def clean(self, value):
169
169
return []
170
170
if not isinstance (value , (list , tuple )):
171
171
raise forms .ValidationError (self .error_messages ['list' ])
172
-
172
+
173
173
qs = self .queryset
174
174
try :
175
175
qs = qs .filter (pk__in = value )
@@ -193,8 +193,8 @@ def prepare_value(self, value):
193
193
sup = super (DocumentMultipleChoiceField , self )
194
194
return [sup .prepare_value (v ) for v in value ]
195
195
return super (DocumentMultipleChoiceField , self ).prepare_value (value )
196
-
197
-
196
+
197
+
198
198
class ListField (forms .Field ):
199
199
default_error_messages = {
200
200
'invalid' : _ ('Enter a list of values.' ),
@@ -205,23 +205,23 @@ class ListField(forms.Field):
205
205
def __init__ (self , contained_field , * args , ** kwargs ):
206
206
if 'widget' in kwargs :
207
207
self .widget = kwargs .pop ('widget' )
208
-
208
+
209
209
if isinstance (contained_field , type ):
210
210
contained_widget = contained_field ().widget
211
211
else :
212
212
contained_widget = contained_field .widget
213
-
213
+
214
214
if isinstance (contained_widget , type ):
215
215
contained_widget = contained_widget ()
216
216
self .widget = self .widget (contained_widget )
217
-
217
+
218
218
super (ListField , self ).__init__ (* args , ** kwargs )
219
-
219
+
220
220
if isinstance (contained_field , type ):
221
221
self .contained_field = contained_field (required = self .required )
222
222
else :
223
223
self .contained_field = contained_field
224
-
224
+
225
225
if not hasattr (self , 'empty_values' ):
226
226
self .empty_values = list (EMPTY_VALUES )
227
227
@@ -241,7 +241,7 @@ def clean(self, value):
241
241
return []
242
242
else :
243
243
raise ValidationError (self .error_messages ['invalid' ])
244
-
244
+
245
245
for field_value in value :
246
246
try :
247
247
clean_data .append (self .contained_field .clean (field_value ))
@@ -262,12 +262,12 @@ def clean(self, value):
262
262
def _has_changed (self , initial , data ):
263
263
if initial is None :
264
264
initial = ['' for x in range (0 , len (data ))]
265
-
265
+
266
266
for initial , data in zip (initial , data ):
267
267
if self .contained_field ._has_changed (initial , data ):
268
268
return True
269
269
return False
270
-
270
+
271
271
def prepare_value (self , value ):
272
272
value = [] if value is None else value
273
273
value = super (ListField , self ).prepare_value (value )
@@ -290,30 +290,30 @@ def __init__(self, contained_field, max_key_length=None,
290
290
* args , ** kwargs ):
291
291
if 'widget' in kwargs :
292
292
self .widget = kwargs .pop ('widget' )
293
-
293
+
294
294
if isinstance (contained_field , type ):
295
295
contained_widget = contained_field ().widget
296
296
else :
297
297
contained_widget = contained_field .widget
298
-
298
+
299
299
if isinstance (contained_widget , type ):
300
300
contained_widget = contained_widget ()
301
301
self .widget = self .widget (contained_widget )
302
-
302
+
303
303
super (MapField , self ).__init__ (* args , ** kwargs )
304
-
304
+
305
305
if isinstance (contained_field , type ):
306
306
field_kwargs ['required' ] = self .required
307
307
self .contained_field = contained_field (** field_kwargs )
308
308
else :
309
309
self .contained_field = contained_field
310
-
310
+
311
311
self .key_validators = key_validators
312
312
if min_key_length is not None :
313
313
self .key_validators .append (MinLengthValidator (int (min_key_length )))
314
314
if max_key_length is not None :
315
315
self .key_validators .append (MaxLengthValidator (int (max_key_length )))
316
-
316
+
317
317
# type of field used to store the dicts value
318
318
if not hasattr (self , 'empty_values' ):
319
319
self .empty_values = list (EMPTY_VALUES )
@@ -351,34 +351,34 @@ def clean(self, value):
351
351
return {}
352
352
else :
353
353
raise ValidationError (self .error_messages ['invalid' ])
354
-
354
+
355
355
# sort out required => at least one element must be in there
356
356
for key , val in value .items ():
357
357
# ignore empties. Can they even come up here?
358
358
if key in self .empty_values and val in self .empty_values :
359
359
continue
360
-
360
+
361
361
try :
362
362
val = self .contained_field .clean (val )
363
363
except ValidationError as e :
364
364
# Collect all validation errors in a single list, which we'll
365
365
# raise at the end of clean(), rather than raising a single
366
366
# exception for the first error we encounter.
367
367
errors .extend (e .messages )
368
-
368
+
369
369
try :
370
370
self ._validate_key (key )
371
371
except ValidationError as e :
372
372
# Collect all validation errors in a single list, which we'll
373
373
# raise at the end of clean(), rather than raising a single
374
374
# exception for the first error we encounter.
375
375
errors .extend (e .messages )
376
-
376
+
377
377
clean_data [key ] = val
378
-
378
+
379
379
if self .contained_field .required :
380
380
self .contained_field .required = False
381
-
381
+
382
382
if errors :
383
383
raise ValidationError (errors )
384
384
0 commit comments