Skip to content

Commit 88358b5

Browse files
author
John Murray
committed
Added fix
2 parents 92c6310 + 781c3bd commit 88358b5

File tree

4 files changed

+64
-64
lines changed

4 files changed

+64
-64
lines changed

mongodbforms/documents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.forms.widgets import media_property
99
from django.core.exceptions import FieldError
1010
from django.core.validators import EMPTY_VALUES
11-
from django.forms.util import ErrorList
11+
from django.forms.utils import ErrorList
1212
from django.forms.formsets import BaseFormSet, formset_factory
1313
from django.utils.translation import ugettext_lazy as _, ugettext
1414
from django.utils.text import capfirst, get_valid_filename

mongodbforms/fieldgenerator.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434

3535
class MongoFormFieldGenerator(object):
3636
"""This class generates Django form-fields for mongoengine-fields."""
37-
37+
3838
# used for fields that fit in one of the generate functions
3939
# but don't actually have the name.
4040
generator_map = {
4141
'sortedlistfield': 'generate_listfield',
4242
'longfield': 'generate_intfield',
4343
}
44-
44+
4545
form_field_map = {
4646
'stringfield': MongoCharField,
4747
'stringfield_choices': forms.TypedChoiceField,
@@ -63,12 +63,12 @@ class MongoFormFieldGenerator(object):
6363
'filefield': forms.FileField,
6464
'imagefield': forms.ImageField,
6565
}
66-
66+
6767
# uses the same keys as form_field_map
6868
widget_override_map = {
6969
'stringfield_long': forms.Textarea,
7070
}
71-
71+
7272
def __init__(self, field_overrides={}, widget_overrides={}):
7373
self.form_field_map.update(field_overrides)
7474
self.widget_override_map.update(widget_overrides)
@@ -83,22 +83,22 @@ def generate(self, field, **kwargs):
8383
# to handle then a simple field
8484
if isinstance(field, MongoEmbeddedDocumentField):
8585
return
86-
86+
8787
attr_name = 'generate_%s' % field.__class__.__name__.lower()
8888
if hasattr(self, attr_name):
8989
return getattr(self, attr_name)(field, **kwargs)
9090

9191
for cls in field.__class__.__bases__:
9292
cls_name = cls.__name__.lower()
93-
93+
9494
attr_name = 'generate_%s' % cls_name
9595
if hasattr(self, attr_name):
9696
return getattr(self, attr_name)(field, **kwargs)
9797

9898
if cls_name in self.form_field_map:
9999
attr = self.generator_map.get(cls_name)
100100
return getattr(self, attr)(field, **kwargs)
101-
101+
102102
raise NotImplementedError('%s is not supported by MongoForm' %
103103
field.__class__.__name__)
104104

@@ -134,7 +134,7 @@ def get_field_help_text(self, field):
134134
return field.help_text
135135
else:
136136
return ''
137-
137+
138138
def get_field_default(self, field):
139139
if isinstance(field, (MongoListField, MongoMapField)):
140140
f = field.field
@@ -148,7 +148,7 @@ def get_field_default(self, field):
148148
else:
149149
d['initial'] = field.default
150150
return f.default
151-
151+
152152
def check_widget(self, map_key):
153153
if map_key in self.widget_override_map:
154154
return {'widget': self.widget_override_map.get(map_key)}
@@ -181,7 +181,7 @@ def generate_stringfield(self, field, **kwargs):
181181
})
182182
if field.regex:
183183
defaults['validators'] = [RegexValidator(regex=field.regex)]
184-
184+
185185
form_class = self.form_field_map.get(map_key)
186186
defaults.update(self.check_widget(map_key))
187187
defaults.update(kwargs)
@@ -324,7 +324,7 @@ def generate_listfield(self, field, **kwargs):
324324
# So we just ignore them
325325
if isinstance(field.field, MongoEmbeddedDocumentField):
326326
return
327-
327+
328328
defaults = {
329329
'label': self.get_field_label(field),
330330
'help_text': self.get_field_help_text(field),
@@ -351,13 +351,13 @@ def generate_listfield(self, field, **kwargs):
351351
defaults.update(self.check_widget(map_key))
352352
defaults.update(kwargs)
353353
return form_class(**defaults)
354-
354+
355355
def generate_mapfield(self, field, **kwargs):
356356
# We can't really handle embedded documents here.
357357
# So we just ignore them
358358
if isinstance(field.field, MongoEmbeddedDocumentField):
359359
return
360-
360+
361361
map_key = 'mapfield'
362362
form_field = self.generate(field.field)
363363
defaults = {
@@ -433,10 +433,10 @@ def check_widget(self, map_key):
433433
override = super(Html5FormFieldGenerator, self).check_widget(map_key)
434434
if override != {}:
435435
return override
436-
436+
437437
chunks = map_key.split('field')
438438
kind = chunks[0]
439-
439+
440440
if kind == 'email':
441441
if hasattr(forms, 'EmailInput'):
442442
return {'widget': forms.EmailInput}

mongodbforms/fields.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
from django.utils.encoding import force_text as force_unicode
1515
except ImportError:
1616
from django.utils.encoding import force_unicode
17-
17+
1818
try:
1919
from django.utils.encoding import smart_text as smart_unicode
2020
except ImportError:
2121
try:
2222
from django.utils.encoding import smart_unicode
2323
except ImportError:
2424
from django.forms.util import smart_unicode
25-
25+
2626
from django.utils.translation import ugettext_lazy as _
27-
from django.forms.util import ErrorList
27+
from django.forms.utils import ErrorList
2828
from django.core.exceptions import ValidationError
2929

3030
try: # objectid was moved into bson in pymongo 1.9
3131
from bson.errors import InvalidId
3232
except ImportError:
3333
from pymongo.errors import InvalidId
34-
34+
3535
from mongodbforms.widgets import ListWidget, MapWidget, HiddenMapWidget
3636

3737

@@ -66,23 +66,23 @@ def to_python(self, value):
6666
if value in EMPTY_VALUES:
6767
return None
6868
return value
69-
70-
69+
70+
7171
class MongoCharField(NormalizeValueMixin, forms.CharField):
7272
pass
73-
73+
7474

7575
class MongoEmailField(NormalizeValueMixin, forms.EmailField):
7676
pass
77-
77+
7878

7979
class MongoSlugField(NormalizeValueMixin, forms.SlugField):
8080
pass
81-
81+
8282

8383
class MongoURLField(NormalizeValueMixin, forms.URLField):
8484
pass
85-
85+
8686

8787
class ReferenceField(forms.ChoiceField):
8888
"""
@@ -96,7 +96,7 @@ def __init__(self, queryset, empty_label="---------", *args, **kwargs):
9696

9797
def _get_queryset(self):
9898
return self._queryset.clone()
99-
99+
100100
def _set_queryset(self, queryset):
101101
self._queryset = queryset
102102
self.widget.choices = self.choices
@@ -130,15 +130,15 @@ def clean(self, value):
130130
return None
131131

132132
oid = super(ReferenceField, self).clean(value)
133-
133+
134134
try:
135135
obj = self.queryset.get(pk=oid)
136136
except (TypeError, InvalidId, self.queryset._document.DoesNotExist):
137137
raise forms.ValidationError(
138138
self.error_messages['invalid_choice'] % {'value': value}
139139
)
140140
return obj
141-
141+
142142
def __deepcopy__(self, memo):
143143
result = super(forms.ChoiceField, self).__deepcopy__(memo)
144144
result.queryset = self.queryset # self.queryset calls clone()
@@ -169,7 +169,7 @@ def clean(self, value):
169169
return []
170170
if not isinstance(value, (list, tuple)):
171171
raise forms.ValidationError(self.error_messages['list'])
172-
172+
173173
qs = self.queryset
174174
try:
175175
qs = qs.filter(pk__in=value)
@@ -193,8 +193,8 @@ def prepare_value(self, value):
193193
sup = super(DocumentMultipleChoiceField, self)
194194
return [sup.prepare_value(v) for v in value]
195195
return super(DocumentMultipleChoiceField, self).prepare_value(value)
196-
197-
196+
197+
198198
class ListField(forms.Field):
199199
default_error_messages = {
200200
'invalid': _('Enter a list of values.'),
@@ -205,23 +205,23 @@ class ListField(forms.Field):
205205
def __init__(self, contained_field, *args, **kwargs):
206206
if 'widget' in kwargs:
207207
self.widget = kwargs.pop('widget')
208-
208+
209209
if isinstance(contained_field, type):
210210
contained_widget = contained_field().widget
211211
else:
212212
contained_widget = contained_field.widget
213-
213+
214214
if isinstance(contained_widget, type):
215215
contained_widget = contained_widget()
216216
self.widget = self.widget(contained_widget)
217-
217+
218218
super(ListField, self).__init__(*args, **kwargs)
219-
219+
220220
if isinstance(contained_field, type):
221221
self.contained_field = contained_field(required=self.required)
222222
else:
223223
self.contained_field = contained_field
224-
224+
225225
if not hasattr(self, 'empty_values'):
226226
self.empty_values = list(EMPTY_VALUES)
227227

@@ -241,7 +241,7 @@ def clean(self, value):
241241
return []
242242
else:
243243
raise ValidationError(self.error_messages['invalid'])
244-
244+
245245
for field_value in value:
246246
try:
247247
clean_data.append(self.contained_field.clean(field_value))
@@ -262,12 +262,12 @@ def clean(self, value):
262262
def _has_changed(self, initial, data):
263263
if initial is None:
264264
initial = ['' for x in range(0, len(data))]
265-
265+
266266
for initial, data in zip(initial, data):
267267
if self.contained_field._has_changed(initial, data):
268268
return True
269269
return False
270-
270+
271271
def prepare_value(self, value):
272272
value = [] if value is None else value
273273
value = super(ListField, self).prepare_value(value)
@@ -290,30 +290,30 @@ def __init__(self, contained_field, max_key_length=None,
290290
*args, **kwargs):
291291
if 'widget' in kwargs:
292292
self.widget = kwargs.pop('widget')
293-
293+
294294
if isinstance(contained_field, type):
295295
contained_widget = contained_field().widget
296296
else:
297297
contained_widget = contained_field.widget
298-
298+
299299
if isinstance(contained_widget, type):
300300
contained_widget = contained_widget()
301301
self.widget = self.widget(contained_widget)
302-
302+
303303
super(MapField, self).__init__(*args, **kwargs)
304-
304+
305305
if isinstance(contained_field, type):
306306
field_kwargs['required'] = self.required
307307
self.contained_field = contained_field(**field_kwargs)
308308
else:
309309
self.contained_field = contained_field
310-
310+
311311
self.key_validators = key_validators
312312
if min_key_length is not None:
313313
self.key_validators.append(MinLengthValidator(int(min_key_length)))
314314
if max_key_length is not None:
315315
self.key_validators.append(MaxLengthValidator(int(max_key_length)))
316-
316+
317317
# type of field used to store the dicts value
318318
if not hasattr(self, 'empty_values'):
319319
self.empty_values = list(EMPTY_VALUES)
@@ -351,34 +351,34 @@ def clean(self, value):
351351
return {}
352352
else:
353353
raise ValidationError(self.error_messages['invalid'])
354-
354+
355355
# sort out required => at least one element must be in there
356356
for key, val in value.items():
357357
# ignore empties. Can they even come up here?
358358
if key in self.empty_values and val in self.empty_values:
359359
continue
360-
360+
361361
try:
362362
val = self.contained_field.clean(val)
363363
except ValidationError as e:
364364
# Collect all validation errors in a single list, which we'll
365365
# raise at the end of clean(), rather than raising a single
366366
# exception for the first error we encounter.
367367
errors.extend(e.messages)
368-
368+
369369
try:
370370
self._validate_key(key)
371371
except ValidationError as e:
372372
# Collect all validation errors in a single list, which we'll
373373
# raise at the end of clean(), rather than raising a single
374374
# exception for the first error we encounter.
375375
errors.extend(e.messages)
376-
376+
377377
clean_data[key] = val
378-
378+
379379
if self.contained_field.required:
380380
self.contained_field.required = False
381-
381+
382382
if errors:
383383
raise ValidationError(errors)
384384

0 commit comments

Comments
 (0)