Skip to content

Commit

Permalink
Cleanups and some renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
jschrewe committed Jun 13, 2013
1 parent e6a4fb5 commit fc23b1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
39 changes: 15 additions & 24 deletions mongodbforms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from pymongo.objectid import ObjectId
from pymongo.errors import InvalidId

from .widgets import MultiWidget
from .widgets import ListWidget

class MongoChoiceIterator(object):
def __init__(self, field):
Expand Down Expand Up @@ -148,17 +148,18 @@ def clean(self, value):
return []
if not isinstance(value, (list, tuple)):
raise forms.ValidationError(self.error_messages['list'])

key = 'pk'

filter_ids = []
for pk in value:
try:
oid = ObjectId(pk)
filter_ids.append(oid)
except InvalidId:
raise forms.ValidationError(self.error_messages['invalid_pk_value'] % pk)
# filter_ids = []
# for pk in value:
# filter_ids.append(pk)
# try:
# oid = ObjectId(pk)
# filter_ids.append(oid)
# except InvalidId:
# raise forms.ValidationError(self.error_messages['invalid_pk_value'] % pk)
qs = self.queryset.clone()
qs = qs.filter(**{'%s__in' % key: filter_ids})
qs = qs.filter(**{'%s__in' % key: value})
pks = set([force_unicode(getattr(o, key)) for o in qs])
for val in value:
if force_unicode(val) not in pks:
Expand Down Expand Up @@ -194,11 +195,10 @@ class ListField(forms.Field):
default_error_messages = {
'invalid': _('Enter a list of values.'),
}
widget = MultiWidget
widget = ListWidget

def __init__(self, field_type, *args, **kwargs):
self.field_type = field_type
self.fields = []
widget = self.field_type().widget
if isinstance(widget, type):
w_type = widget
Expand All @@ -210,17 +210,6 @@ def __init__(self, field_type, *args, **kwargs):

if not hasattr(self, 'empty_values'):
self.empty_values = list(EMPTY_VALUES)

def _init_fields(self, initial):
empty_val = ['',]
if initial is None:
initial = empty_val
else:
initial = initial + empty_val

fields = [self.field_type(initial=d) for d in initial]

return fields

def validate(self, value):
pass
Expand Down Expand Up @@ -268,7 +257,9 @@ def clean(self, value):
def _has_changed(self, initial, data):
if initial is None:
initial = ['' for x in range(0, len(data))]
for field, initial, data in zip(self.fields, initial, data):

field = self.field_type(required=self.required)
for initial, data in zip(initial, data):
if field._has_changed(initial, data):
return True
return False
Expand Down
23 changes: 14 additions & 9 deletions mongodbforms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.core.validators import EMPTY_VALUES


class MultiWidget(Widget):
class ListWidget(Widget):
"""
A widget that is composed of multiple widgets.
Expand Down Expand Up @@ -35,23 +35,25 @@ class MultiWidget(Widget):
def __init__(self, widget_type, attrs=None):
self.widget_type = widget_type
self.widgets = []
super(MultiWidget, self).__init__(attrs)
super(ListWidget, self).__init__(attrs)

def render(self, name, value, attrs=None):
if not isinstance(value, list):
# Yes! This is a stub. Some exception should be raised here I suppose
# I'm totally not sure which one though.
pass
if value is not None and not isinstance(value, (list, tuple)):
raise TypeError("Value supplied for %s must be a list or tuple." % name)

if value is not None:
self.widgets = [self.widget_type() for v in value]

if value is None or (len(value[-1:]) == 0 or value[-1:][0] != ''):
# add an empty field to be able to add data
# there should be exactly one empty widget at the end of the list
empty_widget = self.widget_type()
empty_widget.is_required = False
self.widgets.append(empty_widget)

if self.is_localized:
for widget in self.widgets:
widget.is_localized = self.is_localized

output = []
final_attrs = self.build_attrs(attrs)
id_ = final_attrs.get('id', None)
Expand Down Expand Up @@ -101,7 +103,10 @@ def _get_media(self):
media = property(_get_media)

def __deepcopy__(self, memo):
obj = super(MultiWidget, self).__deepcopy__(memo)
obj = super(ListWidget, self).__deepcopy__(memo)
obj.widgets = copy.deepcopy(self.widgets)
obj.widget_type = copy.deepcopy(self.widget_type)
return obj
return obj



0 comments on commit fc23b1a

Please sign in to comment.