Permalink
Browse files

Add FactoryWidget which is used to pass parameters to the widget choo…

…sed by the factory
  • Loading branch information...
1 parent 9c60f4e commit cebe3c75d790bf1c78ae2d169f1147e1b8f35053 @LeResKP LeResKP committed Sep 23, 2013
Showing with 40 additions and 4 deletions.
  1. +20 −2 tests/test_widgets.py
  2. +1 −1 tw2/sqla/__init__.py
  3. +19 −1 tw2/sqla/factory.py
View
@@ -141,7 +141,8 @@ class DbTestCls14(el.Entity):
DbTestCls13,
field=other_id,
backref='others',
- info={'view_widget': tws.NoWidget}
+ info={'view_widget': tws.NoWidget,
+ 'edit_widget': tws.FactoryWidget(css_class='myclass')}
)
def __unicode__(self):
return self.name
@@ -312,7 +313,8 @@ class DbTestCls14(Base):
other = sa.orm.relation(
DbTestCls13,
backref=sa.orm.backref('others'),
- info={'view_widget': tws.NoWidget}
+ info={'view_widget': tws.NoWidget,
+ 'edit_widget': tws.FactoryWidget(css_class='myclass')}
)
def __unicode__(self):
return self.name
@@ -1199,6 +1201,22 @@ class AwesomePolicy(tws.ViewPolicy): pass
except twc.WidgetError, e:
assert(False)
+ def test_info_factory_widget_on_relation(self):
+ """Test we use first the data defined in the hint
+ """
+ class AwesomePolicy(tws.EditPolicy): pass
+
+ props = filter(
+ lambda x: x.key == 'other',
+ sa.orm.class_mapper(self.DbTestCls14).iterate_properties)
+ assert(len(props) == 1)
+ try:
+ w = AwesomePolicy.factory(props[0])
+ assert(issubclass(w, tws.DbSingleSelectField))
+ assert(w.css_class == 'myclass')
+ except twc.WidgetError, e:
+ assert(False)
+
def test_orig_children(self):
""" Tests overriding properties (`orig_children`) """
@@ -8,7 +8,7 @@
AutoTableForm, AutoViewGrid, AutoGrowingGrid,
AutoListPage, AutoListPageEdit,
AutoEditFieldSet, AutoViewFieldSet,
- NoWidget)
+ NoWidget, FactoryWidget)
import utils
import widgets
View
@@ -145,6 +145,7 @@ class WidgetPolicy(object):
def factory(cls, prop):
widget = None
widget_kw = {}
+ factory_widget = None
cols = getattr(prop, 'columns', [])
if cls.hint_name:
if is_relation(prop):
@@ -155,6 +156,12 @@ def factory(cls, prop):
if issubclass(widget, NoWidget):
# We don't want to display this field!
return None
+ if issubclass(widget, FactoryWidget):
+ factory_widget = widget
+ widget = None
+
+ if widget:
+ pass
elif is_onetomany(prop):
if not cls.onetomany_widget:
raise twc.WidgetError(
@@ -226,7 +233,12 @@ def factory(cls, prop):
if widget:
widget_kw['id'] = prop.key
- if not getattr(widget, 'validator', None) and required_widget(prop):
+ if factory_widget:
+ for k, v in widget._all_params.items():
+ value = getattr(factory_widget, k, None)
+ if value and value != v.default:
+ widget_kw[k] = value
+ if 'validator' not in widget_kw and not getattr(widget, 'validator', None) and required_widget(prop):
widget_kw['validator'] = twc.Required
widget = widget(**widget_kw)
@@ -237,6 +249,12 @@ class NoWidget(twc.Widget):
pass
+class FactoryWidget(twc.Widget):
+ """Widget to use when we want to let the factory decides the widget to use
+ but we want to apply some specifics parameters
+ """
+ pass
+
class ViewPolicy(WidgetPolicy):
"""Base WidgetPolicy for viewing data."""
hint_name = 'view_widget'

0 comments on commit cebe3c7

Please sign in to comment.