Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error with splited error messages #38

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.*~
*.egg-info
*.pyc
.vscode/settings.json
20 changes: 19 additions & 1 deletion django_remote_forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings

from django_remote_forms import logger, widgets
from django import forms


class RemoteField(object):
Expand Down Expand Up @@ -40,7 +41,7 @@ def as_dict(self):
try:
remote_widget_class = getattr(widgets, remote_widget_class_name)
remote_widget = remote_widget_class(self.field.widget, field_name=self.field_name)
except Exception, e:
except Exception as e:
logger.warning('Error serializing %s: %s', remote_widget_class_name, str(e))
widget_dict = {}
else:
Expand Down Expand Up @@ -208,6 +209,10 @@ def as_dict(self):
return super(RemoteMultipleChoiceField, self).as_dict()


class RemoteCommaSeparatedField(RemoteMultipleChoiceField):
pass


class RemoteModelMultipleChoiceField(RemoteMultipleChoiceField):
def as_dict(self):
return super(RemoteModelMultipleChoiceField, self).as_dict()
Expand Down Expand Up @@ -276,3 +281,16 @@ def as_dict(self):
class RemoteSlugField(RemoteCharField):
def as_dict(self):
return super(RemoteSlugField, self).as_dict()


class CommaSeparatedField(forms.MultipleChoiceField):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def prepare_value(self, value):
return value.split(',') if value else value

def clean(self, value):
value = super().clean(value)
return ','.join(value)
29 changes: 27 additions & 2 deletions django_remote_forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django_remote_forms import fields, logger
from django_remote_forms.utils import resolve_promise

from django.forms import ModelMultipleChoiceField

class RemoteForm(object):
def __init__(self, form, *args, **kwargs):
Expand Down Expand Up @@ -121,12 +122,19 @@ def as_dict(self):
form_dict['ordered_fields'] = self.fields

initial_data = {}
foreign_key_fields = []
comma_separated_fields = []

for name, field in [(x, self.form.fields[x]) for x in self.fields]:
# Retrieve the initial data from the form itself if it exists so
# that we properly handle which initial data should be returned in
# the dictionary.

if type(field) in [ModelMultipleChoiceField]:
foreign_key_fields.append(name)
elif type(field) in [fields.CommaSeparatedField]:
comma_separated_fields.append(name)

# Please refer to the Django Form API documentation for details on
# why this is necessary:
# https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values
Expand All @@ -138,7 +146,7 @@ def as_dict(self):
try:
remote_field_class = getattr(fields, remote_field_class_name)
remote_field = remote_field_class(field, form_initial_field_data, field_name=name)
except Exception, e:
except Exception as e:
logger.warning('Error serializing field %s: %s', remote_field_class_name, str(e))
field_dict = {}
else:
Expand All @@ -155,9 +163,26 @@ def as_dict(self):

initial_data[name] = form_dict['fields'][name]['initial']

form_data = self.form.data.copy()

# Filter data to include only form fields data
form_data = {k: v for k, v in self.form.data.items() if k in list(self.fields)}

if self.form.data:
form_dict['data'] = self.form.data
form_dict['data'] = form_data
else:
form_dict['data'] = initial_data

for field_name in foreign_key_fields:
obj_list = form_dict['data'].get(field_name, [])
if obj_list:
form_dict['data'][field_name] = [obj.pk for obj in obj_list]

for field_name in comma_separated_fields:
obj = form_dict['data'].get(field_name, '')
if obj:
form_dict['data'][field_name] = obj.split(',')
else:
form_dict['data'][field_name] = []

return resolve_promise(form_dict)
4 changes: 2 additions & 2 deletions django_remote_forms/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.utils.functional import Promise
from django.utils.encoding import force_unicode
from django.utils.encoding import force_text


def resolve_promise(o):
Expand All @@ -10,7 +10,7 @@ def resolve_promise(o):
o = [resolve_promise(x) for x in o]
elif isinstance(o, Promise):
try:
o = force_unicode(o)
o = force_text(o)
except:
# Item could be a lazy tuple or list
try:
Expand Down
4 changes: 4 additions & 0 deletions django_remote_forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ def as_dict(self):
return widget_dict


class RemoteFilteredSelectMultiple(RemoteSelectMultiple):
pass


class RemoteRadioInput(RemoteWidget):
def as_dict(self):
widget_dict = OrderedDict()
Expand Down