From 7a648db72a72ca2eb655e632b592d3a8b1e7d116 Mon Sep 17 00:00:00 2001 From: Souheil CHELFOUH Date: Thu, 11 Jan 2024 11:04:18 +0100 Subject: [PATCH] Handling deferred widget --- src/jsonschema_colander/meta.py | 6 +++++- src/jsonschema_colander/types.py | 22 +++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/jsonschema_colander/meta.py b/src/jsonschema_colander/meta.py index ad4d503..98207bc 100644 --- a/src/jsonschema_colander/meta.py +++ b/src/jsonschema_colander/meta.py @@ -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) @@ -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() diff --git a/src/jsonschema_colander/types.py b/src/jsonschema_colander/types.py index aa3ddde..26147a4 100644 --- a/src/jsonschema_colander/types.py +++ b/src/jsonschema_colander/types.py @@ -10,7 +10,8 @@ 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, @@ -18,8 +19,7 @@ } except ImportError: string_widgets = {} - enum_widgets = { - } + enum_widgets = {} @converter.register('string') @@ -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() @@ -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):