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!' }
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()) == []
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
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()
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
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'))
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!' }
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)
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
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
class MyForm(Form): foo = Field() bar = Field()
class MyForm(Form): foo = Field(is_valid=lambda **_: (False, 'field error'))
class FooForm(Form): foo = Field(required=True) bar = Field(required=True) baz = Field.boolean(initial=True)
def test_initial_from_instance(): assert Form(instance=Struct(a=Struct(b=7)), fields=[Field(name='a__b')]).fields[0].initial == 7
class MyForm(Form): class Meta: fields = [Field(name='foo')] bar = Field()
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() == ' '
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() == ' '
class CreateRoomForm(Form): name = Field() description = Field.textarea()
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]
class Meta: fields = [Field(name='foo')]
class ForgotPasswordForm(Form): username_or_email = Field( is_valid=lambda parsed_data, **_: (parsed_data is not None, 'Unknown username or email'), parse=parse)
def test_render_template(): assert '<form' in Form(request=RequestFactory().get('/'), data=Data(foo='7'), fields=[Field(name='foo')]).render()
def test_non_editable(): assert Form(data=Data(), fields=[ Field(name='foo', editable=False) ]).fields[0].input_template == 'tri_form/non_editable.html'