Permalink
Browse files

Do not force a validator if it is already defined.

Now we can pass a validator in the hint without overwriting it!
  • Loading branch information...
1 parent cbc36db commit 480870d222f54bd9403a688f5007a4100880e41d @LeResKP LeResKP committed Sep 6, 2013
Showing with 43 additions and 3 deletions.
  1. +42 −2 tests/test_widgets.py
  2. +1 −1 tw2/sqla/factory.py
View
@@ -37,6 +37,8 @@ def setUp(self):
class InfoField(twf.InputField): pass
+class FakeValidator(twc.Validator): pass
+
class ElixirBase(object):
def setUp(self):
@@ -120,10 +122,18 @@ def __unicode__(self):
return self.name
class DbTestCls13(el.Entity):
- name = el.Field(el.String, info={'edit_widget': InfoField})
+ name = el.Field(el.String, info={'edit_widget': InfoField},
+ required=True)
def __unicode__(self):
return self.name
+ class DbTestCls14(el.Entity):
+ name = el.Field(
+ el.String,
+ info={'edit_widget': InfoField(validator=FakeValidator)},
+ required=True)
+ def __unicode__(self):
+ return self.name
self.DbTestCls1 = DbTestCls1
self.DbTestCls2 = DbTestCls2
@@ -138,6 +148,7 @@ def __unicode__(self):
self.DbTestCls11 = DbTestCls11
self.DbTestCls12 = DbTestCls12
self.DbTestCls13 = DbTestCls13
+ self.DbTestCls14 = DbTestCls14
el.setup_all()
el.metadata.create_all()
@@ -274,7 +285,18 @@ def __unicode__(self):
class DbTestCls13(Base):
__tablename__ = 'Test13'
id = sa.Column(sa.Integer, primary_key=True)
- name = sa.Column(sa.String(50), info={'edit_widget': InfoField})
+ name = sa.Column(sa.String(50), info={'edit_widget': InfoField},
+ nullable=False)
+ def __unicode__(self):
+ return self.name
+
+ class DbTestCls14(Base):
+ __tablename__ = 'Test14'
+ id = sa.Column(sa.Integer, primary_key=True)
+ name = sa.Column(
+ sa.String(50),
+ info={'edit_widget': InfoField(validator=FakeValidator)},
+ nullable=False)
def __unicode__(self):
return self.name
@@ -292,6 +314,7 @@ def __unicode__(self):
self.DbTestCls11 = DbTestCls11
self.DbTestCls12 = DbTestCls12
self.DbTestCls13 = DbTestCls13
+ self.DbTestCls14 = DbTestCls14
Base.metadata.create_all()
@@ -1124,6 +1147,23 @@ class AwesomePolicy(tws.EditPolicy):
try:
w = AwesomePolicy.factory(props[0])
assert(issubclass(w, InfoField))
+ assert(w.validator)
+ except twc.WidgetError, e:
+ assert(False)
+
+ def test_info_validator_on_prop(self):
+ """Test we use first the data defined in the hint
+ """
+ class AwesomePolicy(tws.EditPolicy): pass
+
+ props = filter(
+ lambda x: x.key == 'name',
+ sa.orm.class_mapper(self.DbTestCls14).iterate_properties)
+ assert(len(props) == 1)
+ try:
+ w = AwesomePolicy.factory(props[0])
+ assert(issubclass(w, InfoField))
+ assert(isinstance(w.validator, FakeValidator))
except twc.WidgetError, e:
assert(False)
View
@@ -220,7 +220,7 @@ def factory(cls, prop):
if widget:
widget_kw['id'] = prop.key
- if required_widget(prop):
+ if not getattr(widget, 'validator', None) and required_widget(prop):
widget_kw['validator'] = twc.Required
widget = widget(**widget_kw)

0 comments on commit 480870d

Please sign in to comment.