示例#1
0
def test_comma_separated_errors_on_parse():
    def raise_always_value_error(string_value, **_):
        raise ValueError('foo %s!' % string_value)

    def raise_always_validation_error(string_value, **_):
        raise ValidationError(
            ['foo %s!' % string_value,
             'bar %s!' % string_value])

    assert Form(data=Data(foo='5, 7'),
                fields=[
                    Field.comma_separated(
                        Field(name='foo', parse=raise_always_value_error))
                ]).fields[0].errors == {
                    u'Invalid value "5": foo 5!', u'Invalid value "7": foo 7!'
                }

    assert Form(data=Data(foo='5, 7'),
                fields=[
                    Field.comma_separated(
                        Field(name='foo', parse=raise_always_validation_error))
                ]).fields[0].errors == {
                    u'Invalid value "5": foo 5!', u'Invalid value "5": bar 5!',
                    u'Invalid value "7": foo 7!', u'Invalid value "7": bar 7!'
                }
示例#2
0
def test_show():
    assert list(
        Form(data=Data(),
             fields=[Field(name='foo',
                           show=False)]).fields_by_name.keys()) == []
    assert list(
        Form(data=Data(),
             fields=[Field(name='foo', show=lambda form, field: False)
                     ]).fields_by_name.keys()) == []
示例#3
0
def test_render_template_string():
    assert Form(data=Data(foo='7'),
                fields=[
                    Field(name='foo',
                          template=None,
                          template_string='{{ field.value }} {{ form.style }}')
                ]).compact() == '7 compact\n' + AVOID_EMPTY_FORM
示例#4
0
def test_render_custom():
    sentinel = '!!custom!!'
    assert sentinel in Form(fields=[
        Field(name='foo',
              initial='not sentinel value',
              render_value=lambda form, field, value: sentinel),
    ]).table()
示例#5
0
文件: views.py 项目: jlubcke/forum
    class LoginForm(Form):
        username = Field()
        password = Field.password()
        next = Field.hidden(initial=request.GET.get('next', '/'))

        def is_valid(self):
            if not super(LoginForm, self).is_valid():
                return False

            username = self.fields_by_name['username'].value
            password = self.fields_by_name['password'].value

            if username and password:
                user = User.objects.get(username=username)
                self.extra.user = user
                if authenticate(request=request,
                                username=username,
                                password=password):
                    return True

                try:
                    username = User.objects.get(username=username)
                    if b64encode(sha1(password.encode()).digest()).decode(
                    ) == user.password:
                        user.set_password(password)  # upgrade password
                        user.save()
                    authenticate(request=request,
                                 username=username,
                                 password=password)
                except User.DoesNotExist:
                    pass

            return False
示例#6
0
文件: views.py 项目: jlubcke/forum
 class ResetPasswordForm(Form):
     reset_code = Field(
         is_valid=lambda parsed_data, **_:
         (parsed_data is not None, 'Invalid reset password code'),
         parse=parse)
     new_password = Field.password()
     confirm_password = Field.password(
         is_valid=lambda parsed_data, **_: (parsed_data == request.POST.get(
             'new_password'), 'Passwords do not match'))
示例#7
0
def test_comma_separated_errors_on_validation():
    assert Form(data=Data(foo='5, 7'),
                fields=[
                    Field.comma_separated(
                        Field(name='foo',
                              is_valid=lambda parsed_data, **_:
                              (False, 'foo %s!' % parsed_data)))
                ]).fields[0].errors == {
                    u'Invalid value "5": foo 5!', u'Invalid value "7": foo 7!'
                }
示例#8
0
class MyTestForm(Form):
    party = Field.choice(choices=['ABC'], required=False)
    username = Field(is_valid=lambda form, field, parsed_data: (
        parsed_data.startswith(form.fields_by_name['party'].parsed_data.lower(
        ) + '_')
        if parsed_data is not None else None, 'Username must begin with "%s_"'
        % form.fields_by_name['party'].parsed_data))
    joined = Field.datetime(attr='contact__joined')
    a_date = Field.date()
    a_time = Field.time()
    staff = Field.boolean()
    admin = Field.boolean()
    manages = Field.multi_choice(choices=['DEF', 'KTH', 'LIU'], required=False)
    not_editable = Field.text(initial='Some non-editable text', editable=False)
示例#9
0
def test_boolean_initial_true():
    fields = [
        Field.boolean(name='foo', initial=True),
        Field(name='bar', required=False)
    ]

    form = Form(data=Data(), fields=fields)
    assert form.fields_by_name['foo'].value is True

    # If there are arguments, but not for key foo it means checkbox for foo has been unchecked.
    # Field foo should therefore be false.
    form = Form(data=Data(bar='baz', **{'-': '-'}), fields=fields)
    assert form.fields_by_name['foo'].value is False

    form = Form(data=Data(foo='on', bar='baz', **{'-': '-'}), fields=fields)
    assert form.fields_by_name['foo'].value is True
示例#10
0
def test_render_table():
    form = Form(data=Data(foo='!!!7!!!'),
                fields=[
                    Field(
                        name='foo',
                        input_container_css_classes={'###5###'},
                        label_container_css_classes={'$$$11$$$'},
                        help_text='^^^13^^^',
                        label='***17***',
                    )
                ])
    table = form.table()
    assert '!!!7!!!' in table
    assert '###5###' in table
    assert '$$$11$$$' in table
    assert '^^^13^^^' in table
    assert '***17***' in table
    assert '<tr' in table

    # Assert that table is the default
    assert table == "%s" % form
示例#11
0
文件: views.py 项目: dhaggen/tri.form
 class MyForm(Form):
     foo = Field()
     bar = Field()
示例#12
0
 class MyForm(Form):
     foo = Field(is_valid=lambda **_: (False, 'field error'))
示例#13
0
 class FooForm(Form):
     foo = Field(required=True)
     bar = Field(required=True)
     baz = Field.boolean(initial=True)
示例#14
0
def test_initial_from_instance():
    assert Form(instance=Struct(a=Struct(b=7)),
                fields=[Field(name='a__b')]).fields[0].initial == 7
示例#15
0
    class MyForm(Form):
        class Meta:
            fields = [Field(name='foo')]

        bar = Field()
示例#16
0
def test_render_attrs_new_style():
    assert Form(data=Data(foo='7'),
                fields=[Field(name='foo', attrs__foo='1')
                        ]).fields[0].render_attrs() == ' foo="1"'
    assert Form(data=Data(foo='7'),
                fields=[Field(name='foo')]).fields[0].render_attrs() == ' '
示例#17
0
def test_render_attrs():
    assert Form(data=Data(foo='7'),
                fields=[Field(name='foo', attrs={'foo': '1'})
                        ]).fields[0].render_attrs() == ' foo="1"'
    assert Form(data=Data(foo='7'),
                fields=[Field(name='foo')]).fields[0].render_attrs() == ' '
示例#18
0
 class CreateRoomForm(Form):
     name = Field()
     description = Field.textarea()
示例#19
0
def test_initial_list_from_instance():
    assert Form(instance=Struct(a=Struct(b=[7])),
                fields=[Field(name='a__b',
                              is_list=True)]).fields[0].initial_list == [7]
示例#20
0
 class Meta:
     fields = [Field(name='foo')]
示例#21
0
文件: views.py 项目: jlubcke/forum
 class ForgotPasswordForm(Form):
     username_or_email = Field(
         is_valid=lambda parsed_data, **_:
         (parsed_data is not None, 'Unknown username or email'),
         parse=parse)
示例#22
0
def test_render_template():
    assert '<form' in Form(request=RequestFactory().get('/'),
                           data=Data(foo='7'),
                           fields=[Field(name='foo')]).render()
示例#23
0
def test_non_editable():
    assert Form(data=Data(), fields=[
        Field(name='foo', editable=False)
    ]).fields[0].input_template == 'tri_form/non_editable.html'