Esempio n. 1
0
    def __moyaconsole__(self, console):
        if self.bound:
            console.text("Bound form", fg="cyan", bold=True)
        else:
            console.text("Unbound form", fg="blue", bold=True)

        table = []
        for field in self.all_fields:
            if field.value is None:
                v = Cell('None', dim=True)
            else:
                v = field.value
            table.append([field.name, v])
        console.table(table, ['name', 'value'])

        if self.validated:
            if self.errors:
                console.text("Form errors", fg="red", bold=True)
                error_table = [(field, '\n'.join('* %s' % e.strip()
                                                 for e in _errors))
                               for field, _errors in iteritems(self.errors)]
                console.table(error_table, ["field", "error"])
            else:
                if self.error:
                    console.text('Form error "%s"' % self.error.strip(),
                                 fg="red",
                                 bold=True)
                else:
                    console.text("Validated, no errors", fg="green", bold=True)
Esempio n. 2
0
    def __moyaconsole__(self, console):
        if self.bound:
            console.text("Bound form", fg="cyan", bold=True)
        else:
            console.text("Unbound form", fg="blue", bold=True)

        table = []
        for field in self.all_fields:
            if field.value is None:
                v = Cell('None', dim=True)
            else:
                v = field.value
            table.append([field.name, v])
        console.table(table, ['name', 'value'])

        if self.validated:
            if self.errors:
                console.text("Form errors", fg="red", bold=True)
                error_table = [(field, '\n' .join('* %s' % e.strip() for e in _errors))
                               for field, _errors in iteritems(self.errors)]
                console.table(error_table, ["field", "error"])
            else:
                if self.error:
                    console.text('Form error "%s"' % self.error.strip(), fg="red", bold=True)
                else:
                    console.text("Validated, no errors", fg="green", bold=True)

        if not self.csrf_check:
            console.text('CSRF check failed -- form did not originate from here!', fg="red", bold=True)
Esempio n. 3
0
    def logic(self, context):
        form = self.src(context)
        if not form.bound:
            return

        extends = [form.element]
        el = form.element
        while 1:
            extend = el.extends(context)
            if not extend:
                break
            app, el = el.get_element(extend)
            extends.append(el)

        values = {
            name: field.value
            for name, field in iteritems(form.fields_map)
        }
        app = self.get_app(context)
        new_values = {}

        for field in form.fields:
            new_values[field.name] = field.value
            if field.required and field.value in ('', None):
                form.add_fail(field.name, "This field is required")
            else:
                for validate_field in form.get_field_validators(field.name):
                    with self.closure_call(context,
                                           app,
                                           validate_field.data,
                                           _field=field,
                                           form=form,
                                           value=values[field.name],
                                           values=values,
                                           field=field.value):
                        yield logic.DeferNodeContents(validate_field.element)

        if not form.errors:
            for field in form.fields:
                if field.adapt_value is not None:
                    value = new_values[field.name]
                    new_value = field.adapt_value(context, value=value)
                    new_values[field.name] = new_value

            for field in form.fields:
                for adapt_field in form.get_field_adapters(field.name):
                    value = new_values[field.name]
                    with self.closure_call(context,
                                           app,
                                           adapt_field.data,
                                           form=form,
                                           values=values,
                                           value=value) as call:
                        yield logic.DeferNodeContents(adapt_field.element)

                    if call.has_return:
                        new_values[field.name] = call.return_value

        for field in form.fields:
            form.set_field_data(field.name, new_values[field.name])

        form.validated = True
        if form.ok:
            yield logic.DeferNodeContents(self)
            yield logic.SkipNext((namespaces.default, "else"))
Esempio n. 4
0
    def logic(self, context):
        form = self.src(context)
        if not form.bound:
            return

        extends = [form.element]
        el = form.element
        while 1:
            extend = el.extends(context)
            if not extend:
                break
            app, el = el.get_element(extend)
            extends.append(el)

        values = {name: field.value for name, field in iteritems(form.fields_map)}
        app = self.get_app(context)
        new_values = {}

        if self.csrf(context):
            form.validate_csrf(context)

        for field in form.fields:
            new_values[field.name] = field.value
            if field.required and field.value in ('', None):
                form.add_fail(field.name, "This field is required")
            else:
                for validate_field in form.get_field_validators(field.name):
                    with self.closure_call(context,
                                           app,
                                           validate_field.data,
                                           _field=field,
                                           form=form,
                                           value=values[field.name],
                                           values=values,
                                           field=field.value):
                        yield logic.DeferNodeContents(validate_field.element)

        if not form.errors:
            for field in form.fields:
                if field.adapt_value is not None:
                    value = new_values[field.name]
                    new_value = field.adapt_value(context, value=value)
                    new_values[field.name] = new_value

            for field in form.fields:
                for adapt_field in form.get_field_adapters(field.name):
                    value = new_values[field.name]
                    with self.closure_call(context,
                                           app,
                                           adapt_field.data,
                                           form=form,
                                           values=values,
                                           value=value) as call:
                        yield logic.DeferNodeContents(adapt_field.element)

                    if call.has_return:
                        new_values[field.name] = call.return_value

        for field in form.fields:
            form.set_field_data(field.name, new_values[field.name])

        form.validated = True
        if form.ok:
            yield logic.DeferNodeContents(self)
            yield logic.SkipNext((namespaces.default, "else"))