Ejemplo n.º 1
0
    def generate_schema(self):
        """
        If the widget has children this method generates a `Schema` to validate
        including the validators from all children once these are all known.
        """
        if _has_child_validators(self) and not isinstance(
                self, WidgetRepeater):
            if isinstance(self.validator, Schema):
                log.debug("Extending Schema for %r", self)
                self.validator = _update_schema(_copy_schema(self.validator),
                                                self.children)
            elif isclass(self.validator) and issubclass(
                    self.validator, Schema):
                log.debug("Instantiating Schema class for %r", self)
                self.validator = _update_schema(self.validator(),
                                                self.children)
            elif self.validator is DefaultValidator:
                self.validator = _update_schema(Schema(), self.children)

            if self.is_root and hasattr(self.validator, 'pre_validators'):
                #XXX: Maybe add VariableDecoder to every Schema??
                log.debug("Appending decoder to %r", self)
                self.validator.pre_validators.insert(0, VariableDecoder)
            for c in self.children:
                if c.strip_name:
                    v = self.validator.fields.pop(c._id)
                    merge_schemas(self.validator, v, True)
Ejemplo n.º 2
0
def test_state_manipulation():
    """
    Verify that full_dict push and pop works
    """
    state = State()
    old_dict = state.full_dict = {"a": 1}
    old_key = state.key = "a"
    new_dict = {"b": 2}

    class MyValidator(Validator):
        check_key = None
        pre_validator = False
        post_validator = False
        __unpackargs__ = ("check_key",)

        def to_python(self, value, state):
            if not self.pre_validator:
                assert getattr(state, "full_dict", {}) == new_dict, "full_dict not added"
            assert state.key == self.check_key, "key not updated"

            return value

        def from_python(self, value, state):
            if not self.post_validator:
                assert getattr(state, "full_dict", {}) == new_dict, "full_dict not added"
            assert state.key == self.check_key, "key not updated"

            return value

    s = Schema(
        if_key_missing=None,
        b=MyValidator("b"),
        c=MyValidator("c"),
        pre_validators=[MyValidator("a", pre_validator=True)],
        chained_validators=[MyValidator("a", post_validator=True)],
    )

    s.to_python(new_dict, state)

    assert state.full_dict == old_dict, "full_dict not restored"
    assert state.key == old_key, "key not restored"

    s.from_python(new_dict, state)

    assert state.full_dict == old_dict, "full_dict not restored"
    assert state.key == old_key, "key not restored"
Ejemplo n.º 3
0
def test_state_manipulation():
    """
    Verify that full_dict push and pop works
    """
    state = State()
    old_dict = state.full_dict = {'a': 1}
    old_key = state.key = 'a'
    new_dict = {'b': 2}

    class MyValidator(Validator):
        check_key = None
        pre_validator = False
        post_validator = False
        __unpackargs__ = ('check_key', )

        def to_python(self, value, state):
            if not self.pre_validator:
                assert getattr(state, 'full_dict',
                               {}) == new_dict, "full_dict not added"
            assert state.key == self.check_key, "key not updated"

            return value

        def from_python(self, value, state):
            if not self.post_validator:
                assert getattr(state, 'full_dict',
                               {}) == new_dict, "full_dict not added"
            assert state.key == self.check_key, "key not updated"

            return value

    s = Schema(if_key_missing=None,
               b=MyValidator('b'),
               c=MyValidator('c'),
               pre_validators=[MyValidator('a', pre_validator=True)],
               chained_validators=[MyValidator('a', post_validator=True)])

    s.to_python(new_dict, state)

    assert state.full_dict == old_dict, "full_dict not restored"
    assert state.key == old_key, "key not restored"

    s.from_python(new_dict, state)

    assert state.full_dict == old_dict, "full_dict not restored"
    assert state.key == old_key, "key not restored"
 def test_returns_default_for_missing_items(self):
     schema = Schema()
     schema.add_field('limit', self.validator)
     assert_equals(dict(limit=42), schema.to_python({}))
Ejemplo n.º 5
0
class SproxTableForm(TableForm):
    available_engines = ['mako', 'genshi']
    validator = Schema(ignore_missing_keys=True, allow_extra_fields=True)
    template = "sprox.widgets.tw1widgets.templates.tableForm"
Ejemplo n.º 6
0
from formencode.schema import Schema
from sprox.formbase import AddRecordForm
import tw2.forms as twf

from mozzarella.model import DBSession
from mozzarella.model.presentation import Presentation, PresentationFile
from mozzarella.model.auth import User
from mozzarella.lib.base import BaseController

__all__ = ['PresentationsController']

form_validator = Schema(allow_extra_fields=True,
                        chained_validators=[
                            RequireIfPresent('file_description',
                                             present='file'),
                            RequireIfPresent('file_2_description',
                                             present='file_2'),
                            RequireIfPresent('file_3_description',
                                             present='file_3')
                        ])


class NewPresentationForm(AddRecordForm):
    __model__ = Presentation
    __require_fields__ = ['title', 'date']
    __omit_fields__ = ['files', '_other_authors']
    __field_order__ = [
        'title', 'description', 'date', 'thumbnail', 'repo_url', 'authors',
        'other_authors', 'file', 'file_description', 'file_2',
        'file_2_description', 'file_3', 'file_3_description'
    ]