Skip to content

Commit

Permalink
Merge pull request #15 from crodriguezgarci/hotfix/unknwon_widget_gen…
Browse files Browse the repository at this point in the history
…erate_empty_remote_widget

Add default widget class to use them when field widget class is unknown
  • Loading branch information
crodriguezgarci authored Jan 13, 2022
2 parents d5875ff + 3a19d8c commit 8f9b346
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions django_remote_forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from django_remote_forms import logger, widgets
from django import forms

DEFAULT_REMOTE_WIDGET_CLASS_NAME = 'DefaultRemoteInput'


class RemoteField(object):
"""
Expand Down Expand Up @@ -40,12 +42,12 @@ def as_dict(self):
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 as e:
logger.warning('Error serializing %s: %s', remote_widget_class_name, str(e))
widget_dict = {}
else:
widget_dict = remote_widget.as_dict()
remote_widget_class = getattr(widgets, DEFAULT_REMOTE_WIDGET_CLASS_NAME)

remote_widget = remote_widget_class(self.field.widget, field_name=self.field_name)
widget_dict = remote_widget.as_dict()

field_dict['widget'] = widget_dict

Expand Down
9 changes: 9 additions & 0 deletions django_remote_forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def as_dict(self):
return widget_dict


class DefaultRemoteInput(RemoteWidget):
def as_dict(self):
widget_dict = super(DefaultRemoteInput, self).as_dict()

widget_dict['input_type'] = 'text'

return widget_dict


class RemoteTextInput(RemoteInput):
def as_dict(self):
return super(RemoteTextInput, self).as_dict()
Expand Down

0 comments on commit 8f9b346

Please sign in to comment.