Skip to content

Simple refactoring to allow customization of field/widget as_dict(). #16

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions django_remote_forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ def __init__(self, field, form_initial_data=None, field_name=None):
self.field = field
self.form_initial_data = form_initial_data

def get_widget_class(self):
remote_widget_class_name = 'Remote%s' % self.field.widget.__class__.__name__
remote_widget_class = getattr(widgets, remote_widget_class_name)
return remote_widget_class

def get_widget_dict(self):
# Instantiate the Remote Forms equivalent of the widget if possible
# in order to retrieve the widget contents as a dictionary.
try:
remote_widget_class = self.get_widget_class()
remote_widget = remote_widget_class(
self.field.widget, field_name=self.field_name)
except Exception, e:
logger.warning(
'Error serializing %s: %s',
self.field.widget.__class__.__name__, e)
widget_dict = {}
else:
widget_dict = remote_widget.as_dict()
return widget_dict

def as_dict(self):
field_dict = SortedDict()
field_dict['title'] = self.field.__class__.__name__
Expand All @@ -33,17 +54,7 @@ def as_dict(self):

field_dict['error_messages'] = self.field.error_messages

# Instantiate the Remote Forms equivalent of the widget if possible
# in order to retrieve the widget contents as a dictionary.
remote_widget_class_name = 'Remote%s' % self.field.widget.__class__.__name__
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:
logger.warning('Error serializing %s: %s', remote_widget_class_name, str(e))
widget_dict = {}
else:
widget_dict = remote_widget.as_dict()
widget_dict = self.get_widget_dict()

field_dict['widget'] = widget_dict

Expand Down
34 changes: 21 additions & 13 deletions django_remote_forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ def __init__(self, form, *args, **kwargs):
logger.warning('Following fieldset fields are excluded %s' % (fieldset_fields - set(self.fields)))
self.fieldsets = {}

def get_remote_field_class(self, name, field):
remote_field_class_name = 'Remote%s' % field.__class__.__name__
remote_field_class = getattr(fields, remote_field_class_name)
return remote_field_class

def get_field_dict(self, name, field):
form_initial_field_data = self.form.initial.get(name)
# Instantiate the Remote Forms equivalent of the field if possible
# in order to retrieve the field contents as a dictionary.
try:
remote_field_class = self.get_remote_field_class(name, field)
remote_field = remote_field_class(field, form_initial_field_data,
field_name=name)
except Exception, e:
logger.warning('Error serializing field %s: %s', name, e)
field_dict = {}
else:
field_dict = remote_field.as_dict()
return field_dict

def as_dict(self):
"""
Returns a form as a dictionary that looks like the following:
Expand Down Expand Up @@ -122,19 +142,7 @@ def as_dict(self):
# 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
form_initial_field_data = self.form.initial.get(name)

# Instantiate the Remote Forms equivalent of the field if possible
# in order to retrieve the field contents as a dictionary.
remote_field_class_name = 'Remote%s' % field.__class__.__name__
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:
logger.warning('Error serializing field %s: %s', remote_field_class_name, str(e))
field_dict = {}
else:
field_dict = remote_field.as_dict()
field_dict = self.get_field_dict(name, field)

if name in self.readonly_fields:
field_dict['readonly'] = True
Expand Down