Exemplo n.º 1
0
def test_regular_gettext():
    catalog = GetTextish()

    # translators placed at the top of the form
    schema = Dict.of(String.named(u'age').using(validators=[Converted()])).\
             using(ugettext=catalog.ugettext,
                   ungettext=catalog.ungettext)

    data = schema()
    data.validate()
    assert data[u'age'].errors == [u'reg AGE']
Exemplo n.º 2
0
def test_local_gettext():
    catalog = GetTextish()

    # translators placed on a specific form element
    schema = Dict.of(
        String.named(u'age').using(validators=[Converted()],
                                   ugettext=catalog.ugettext,
                                   ungettext=catalog.ungettext))

    data = schema()
    data.validate()
    assert data[u'age'].errors == [u'reg AGE']
Exemplo n.º 3
0
def test_state_gettext():
    catalog = GetTextish()

    schema = Dict.of(String.named(u'age').using(validators=[Converted()]))

    # if state has ugettext or ungettext attributes, those will be used
    data = schema()
    data.validate(catalog)
    assert data[u'age'].errors == [u'reg AGE']

    # also works if state is dict-like
    data = schema()
    state = dict(ugettext=catalog.ugettext, ungettext=catalog.ungettext)
    data.validate(state)
    assert data[u'age'].errors == [u'reg AGE']
Exemplo n.º 4
0
def test_local_gettext_search_is_not_overeager():
    catalog = GetTextish()

    def poison(*a):
        assert False

    # if a translator is found on an element, its parents won't be searched
    schema = (Dict.of(
        String.named(u'age').using(validators=[Converted()],
                                   ugettext=catalog.ugettext,
                                   ungettext=catalog.ungettext)).using(
                                       ugettext=poison, ungettext=poison))

    data = schema()
    data.validate()
    assert data[u'age'].errors == [u'reg AGE']
Exemplo n.º 5
0
def test_builtin_gettext():
    catalog = GetTextish()

    schema = Dict.of(String.named(u'age').using(validators=[Converted()]))

    data = schema()

    try:
        # translators can go into the builtins
        import __builtin__
        __builtin__.ugettext = catalog.ugettext
        __builtin__.ungettext = catalog.ungettext
        data.validate()
        assert data[u'age'].errors == [u'reg AGE']
    finally:
        del __builtin__.ugettext
        del __builtin__.ungettext
Exemplo n.º 6
0
def test_validator_validated():
    sentinel = []

    def listener(**kw):
        sentinel.append(kw)

    signals.validator_validated.connect(listener)

    schema = String.using(validators=[Present(), Converted(), NoLongerThan(5)])
    el = schema()
    assert not el.validate()
    eq_(sentinel, [
        dict(sender=schema.validators[0], element=el, state=None, result=False)
    ])
    del sentinel[:]
    el = schema(value='abcd')
    assert el.validate()
    assert len(sentinel) == 3
    assert sentinel[-1]['result']

    del sentinel[:]
    el = schema('squiznart')
    assert not el.validate()
    assert len(sentinel) == 3
    assert not sentinel[-1]['result']

    s2 = String.using(optional=False)

    del sentinel[:]
    el = s2()
    assert not el.validate()
    eq_(sentinel,
        [dict(sender=NotEmpty, element=el, state=None, result=False)])

    del sentinel[:]
    el = s2('squiznart')
    assert el.validate()
    eq_(sentinel, [dict(sender=NotEmpty, element=el, state=None, result=True)])

    del listener
    del sentinel[:]
    el = schema('squiznart')
    assert not el.validate()
    assert not sentinel

    signals.validator_validated._clear_state()
Exemplo n.º 7
0
    cols=COLS).using(label=L_('Subscriptions'),
                     optional=True,
                     separator='\n',
                     separator_regex=re.compile(r'[\r\n]+'))

Quicklinks = MyJoinedString.of(String).with_properties(
    widget=WIDGET_MULTILINE_TEXT, rows=ROWS,
    cols=COLS).using(label=L_('Quick Links'),
                     optional=True,
                     separator='\n',
                     separator_regex=re.compile(r'[\r\n]+'))

Search = Text.using(default='', optional=True).with_properties(
    widget=WIDGET_SEARCH, placeholder=L_("Search Query"))

_Integer = Integer.validated_by(Converted())

AnyInteger = _Integer.with_properties(widget=WIDGET_ANY_INTEGER)

Natural = AnyInteger.validated_by(ValueAtLeast(0))

SmallNatural = _Integer.with_properties(widget=WIDGET_SMALL_NATURAL)

RadioChoice = Text.with_properties(widget=WIDGET_RADIO_CHOICE)


class DateTimeUNIX(_DateTime):
    """
    A DateTime that uses a UNIX timestamp instead of datetime as internal
    representation of DateTime.
    """
Exemplo n.º 8
0
CommonString = fl.String.using(optional=True) \
                        .with_properties(widget='input')

CommonEnum = fl.Enum.using(optional=True) \
                    .including_validators(EnumValue()) \
                    .with_properties(widget="select")
CommonEnum.value_labels = None

# CommonBoolean has optional=False because booleans are
# required to be True or False (None is not allowed)
CommonBoolean = fl.Boolean.using(optional=True).with_properties(widget="checkbox")
CommonDict = fl.Dict.with_properties(widget="group")
CommonList = fl.List.using(optional=True)
CommonInteger = fl.Integer.using(optional=True)
_valid_date = Converted(incorrect=u"%(label)s is not a valid date")
CommonDate = fl.Date.using(optional=True).including_validators(_valid_date) \
                    .with_properties(widget='date', attr={"class": "picker"}
                    )
CommonDateTime = fl.DateTime.using(optional=True).including_validators(_valid_date) \
                            .with_properties(widget='date', attr={"class": "picker"})


class SourceField(CommonEnum, object):
    valid_values = ValuesFromTable('sources', field=None)
    value_labels = DictFromTable('sources', value_field='short_name', key_field=None)

class ThematicCategoryField(CommonEnum, object):
    valid_values = ValuesFromTable('thematic_categories', field=None)
    value_labels = DictFromTable('thematic_categories',
            value_field='description', key_field=None)