Skip to content

Commit

Permalink
Handling deferred widget
Browse files Browse the repository at this point in the history
  • Loading branch information
trollfot committed Jan 11, 2024
1 parent 71576f8 commit 7a648db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/jsonschema_colander/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def __init__(self,
self.label = label or name
self.description = description
self.required = required
self.readonly = self.fieldconf.get('readonly', False)
self.readonly = self.fieldconf.get(
'readonly', self.config.get('readonly', False)
)
self.validators = validators
if validators := self.fieldconf.get('validators'):
self.validators.extend(validators)
Expand Down Expand Up @@ -152,6 +154,8 @@ def get_factory(self):

def get_widget(self, factory, options):
if widget := default_widget_makers.get(factory):
if isinstance(widget, colander.deferred):
return widget
if READONLY_WIDGET:
return widget(readonly=self.readonly)
return widget()
Expand Down
22 changes: 17 additions & 5 deletions src/jsonschema_colander/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
try:
import deform.widget
string_widgets = {
"password": deform.widget.PasswordWidget
"password": deform.widget.PasswordWidget,
"textarea": deform.widget.TextAreaWidget
}
enum_widgets = {
colander.String: deform.widget.SelectWidget,
colander.Set: deform.widget.CheckboxChoiceWidget,
}
except ImportError:
string_widgets = {}
enum_widgets = {
}
enum_widgets = {}


@converter.register('string')
Expand Down Expand Up @@ -59,8 +59,15 @@ def get_widget(self, factory, options):
"""
if 'choices' in self.attributes:
if widget := enum_widgets.get(factory):
return widget(values=self.attributes['choices'])
if isinstance(widget, colander.deferred):
return widget
return widget(
values=self.attributes['choices'],
readonly=self.readonly
)
elif widget := self.widgets.get(self.format):
if isinstance(widget, colander.deferred):
return widget
if READONLY_WIDGET:
return widget(readonly=self.readonly)
return widget()
Expand Down Expand Up @@ -175,7 +182,12 @@ def get_factory(self):
def get_widget(self, factory, options):
if 'choices' in self.attributes:
if widget := enum_widgets.get(factory):
return widget(values=self.attributes['choices'])
if isinstance(widget, colander.deferred):
return widget
return widget(
values=self.attributes['choices'],
readonly=self.readonly
)
return super().get_widget(factory, options)

def __call__(self):
Expand Down

0 comments on commit 7a648db

Please sign in to comment.