def test_query_namespace_inject(): class FooException(Exception): pass def post_validation(form): del form raise FooException() with pytest.raises(FooException): foo = Table( data=[], model=Foo, request=Struct(method='POST', POST={}, GET=Struct(urlencode=lambda: None)), columns=[Column(name='foo', query__show=True, query__gui__show=True)], query__gui__post_validation=post_validation) foo.prepare(foo.request)
class Meta: model = Person columns = Table.columns_from_model( model=Person, column__id__show=lambda table, **_: table.request.user. is_staff, column__last_name__show=lambda table, **_: table.request.user. is_admin)
def test_from_model(): t = Table.from_model( model=Foo, data=Foo.objects.all(), column__a__display_name="Some a", column__a__extra__stuff="Some stuff" ) assert [x.name for x in t.columns] == ["id", "a", "b"] assert [x.name for x in t.columns if x.show] == ["a", "b"] t.prepare(RequestFactory().get("/")) assert "Some a" == t.bound_column_by_name["a"].display_name assert "Some stuff" == t.bound_column_by_name["a"].extra.stuff
def test_from_model(): t = Table.from_model( model=Foo, data=Foo.objects.all(), column__a__display_name='Some a', column__a__extra__stuff='Some stuff', ) assert [x.name for x in t.columns] == ['id', 'a', 'b'] assert [x.name for x in t.columns if x.show] == ['a', 'b'] t.prepare(RequestFactory().get("/")) assert 'Some a' == t.bound_column_by_name['a'].display_name assert 'Some stuff' == t.bound_column_by_name['a'].extra.stuff
def test_query_namespace_inject(): class FooException(Exception): pass def post_validation(form): del form raise FooException() with pytest.raises(FooException): foo = Table(data=[], model=Foo, request=Struct(method='POST', POST={'-': '-'}, GET=Struct(urlencode=lambda: None)), columns=[ Column(name='foo', query__show=True, query__gui__show=True) ], query__gui__post_validation=post_validation) foo.prepare(foo.request)
def explicit_table(): columns = [ Column(name="foo"), Column.number(name="bar"), ] return Table(data=get_data(), columns=columns, attrs=lambda table: { 'class': 'listview', 'id': 'table_id' })
def test_from_model(): t = Table.from_model(data=Foo.objects.all(), model=Foo) assert [x.name for x in t.columns] == ['id', 'a', 'b'] assert [x.name for x in t.columns if x.show] == ['a', 'b']