def test_auto_rowspan(): class TestTable(NoSortTable): foo = Column(auto_rowspan=True) data = [ Struct(foo=1), Struct(foo=1), Struct(foo=2), Struct(foo=2), ] verify_table_html(TestTable(data), """ <table class="listview"> <thead> <tr><th class="subheader first_column"> Foo </th></tr> </thead> <tr class="row1"> <td rowspan="2"> 1 </td> </tr> <tr class="row2"> <td style="display: none"> 1 </td> </tr> <tr class="row1"> <td rowspan="2"> 2 </td> </tr> <tr class="row2"> <td style="display: none"> 2 </td> </tr> </table>""")
def test_auto_rowspan_and_render_twice(): class TestTable(NoSortTable): foo = Column(auto_rowspan=True) data = [ Struct(foo=1), Struct(foo=1), Struct(foo=2), Struct(foo=2), ] expected = """ <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> <td rowspan="2"> 1 </td> </tr> <tr class="row2"> <td style="display: none"> 1 </td> </tr> <tr class="row1"> <td rowspan="2"> 2 </td> </tr> <tr class="row2"> <td style="display: none"> 2 </td> </tr> </tbody> </table>""" t = TestTable(data=data) verify_table_html(t, expected) verify_table_html(t, expected)
def test_auto_rowspan_and_render_twice(): class TestTable(NoSortTable): foo = Column(auto_rowspan=True) data = [ Struct(foo=1), Struct(foo=1), Struct(foo=2), Struct(foo=2), ] expected = """ <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> <td rowspan="2"> 1 </td> </tr> <tr class="row2"> <td style="display: none"> 1 </td> </tr> <tr class="row1"> <td rowspan="2"> 2 </td> </tr> <tr class="row2"> <td style="display: none"> 2 </td> </tr> </tbody> </table>""" t = TestTable(data=data) verify_table_html(table=t, expected_html=expected) verify_table_html(table=t, expected_html=expected)
def test_attr(): class TestTable(NoSortTable): foo = Column() bar = Column(attr="foo") data = [Struct(foo="foo")] verify_table_html( table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> Foo </th> <th class="first_column subheader"> Bar </th> </tr> </thead> <tbody> <tr class="row1"> <td> foo </td> <td> foo </td> </tr> </tbody> </table>""", )
def test_show_lambda(): def show_callable(table, column): assert isinstance(table, TestTable) assert column.name == "bar" return False class TestTable(NoSortTable): foo = Column() bar = Column.icon("foo", show=show_callable) data = [Struct(foo="foo", bar="bar")] verify_table_html( table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> <td> foo </td> </tr> </tbody> </table>""", )
def test_render_impl(table): verify_table_html(table, """ <table class="listview" id="table_id"> <thead> <tr> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> <th class="first_column subheader"> <a href="?order=bar"> Bar </a> </th> </tr> </thead> <tbody> <tr class="row1"> <td> Hello </td> <td class="rj"> 17 </td> </tr> <tr class="row2"> <td> <evil/> & </td> <td class="rj"> 42 </td> </tr> </tbody> </table>""")
def test_django_table(): Foo(a=17, b="Hej").save() Foo(a=42, b="Hopp").save() class TestTable(Table): a = Column.number() b = Column() verify_table_html(TestTable(Foo.objects.all()), """ <table class="listview"> <thead> <tr> <th class="subheader first_column"> <a href="?order=a"> A </a> </th> <th class="subheader first_column"> <a href="?order=b"> B </a> </th> </tr> </thead> <tr class="row1" data-pk="1"> <td class="rj"> 17 </td> <td> Hej </td> </tr> <tr class="row2" data-pk="2"> <td class="rj"> 42 </td> <td> Hopp </td> </tr> </table>""")
def test_sort_list_bad_parameter(): class TestTable(Table): foo = Column() bar = Column.number(sort_key='bar') data = [Struct(foo='b', bar=2), Struct(foo='a', bar=1)] verify_table_html(table=TestTable(data=data), query=dict(order='barfology'), expected_html="""\ <table class="listview"> <thead> <tr> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> <th class="first_column subheader"> <a href="?order=bar"> Bar </a> </th> </tr> </thead> <tbody> <tr> <td> b </td> <td class="rj"> 2 </td> </tr> <tr> <td> a </td> <td class="rj"> 1 </td> </tr> </tbody> </table> """)
def test_render_impl(table): verify_table_html(table=table, expected_html=""" <table class="listview" id="table_id"> <thead> <tr> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> <th class="first_column subheader"> <a href="?order=bar"> Bar </a> </th> </tr> </thead> <tbody> <tr class="row1"> <td> Hello </td> <td class="rj"> 17 </td> </tr> <tr class="row2"> <td> <evil/> & </td> <td class="rj"> 42 </td> </tr> </tbody> </table>""")
def test_attrs_new_syntax(): class TestTable(NoSortTable): class Meta: attrs__class__classy = True attrs__foo = lambda table: 'bar' row__attrs__class__classier = True row__attrs__foo = lambda table: "barier" yada = Column() verify_table_html( table=TestTable(data=[Struct(yada=1), Struct(yada=2)]), expected_html=""" <table class="classy listview" foo="bar"> <thead> <tr> <th class="first_column subheader"> Yada </th> </tr> </thead> <tbody> <tr class="classier row1" foo="barier"> <td> 1 </td> </tr> <tr class="classier row2" foo="barier"> <td> 2 </td> </tr> </tbody> </table>""")
def test_cell_template(): def explode(**_): assert False class TestTable(NoSortTable): foo = Column( cell__template="test_cell_template.html", cell__format=explode, cell__url=explode, cell__url_title=explode ) data = [Struct(foo="sentinel")] verify_table_html( table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> Custom rendered: sentinel </tr> </tbody> </table>""", )
def test_attrs(): class TestTable(NoSortTable): class Meta: attrs = {'class': 'classy', 'foo': lambda table: 'bar'} row__attrs = { 'class': 'classier', 'foo': lambda table, row: "barier" } yada = Column() verify_table_html( table=TestTable(data=[Struct(yada=1), Struct(yada=2)]), expected_html=""" <table class="classy listview" foo="bar"> <thead> <tr> <th class="first_column subheader"> Yada </th> </tr> </thead> <tbody> <tr class="classier row1" foo="barier"> <td> 1 </td> </tr> <tr class="classier row2" foo="barier"> <td> 2 </td> </tr> </tbody> </table>""")
def test_attrs_new_syntax(): class TestTable(NoSortTable): class Meta: attrs__class__classy = True attrs__foo = lambda table: 'bar' row__attrs__class__classier = True row__attrs__foo = lambda table: "barier" yada = Column() verify_table_html(table=TestTable(data=[Struct(yada=1), Struct(yada=2)]), expected_html=""" <table class="classy listview" foo="bar"> <thead> <tr> <th class="first_column subheader"> Yada </th> </tr> </thead> <tbody> <tr class="classier row1" foo="barier"> <td> 1 </td> </tr> <tr class="classier row2" foo="barier"> <td> 2 </td> </tr> </tbody> </table>""")
def test_row_template(): class TestTable(NoSortTable): foo = Column() bar = Column() class Meta: row__template = lambda table: 'test_table_row.html' data = [Struct(foo="sentinel", bar="schmentinel")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> Foo </th> <th class="first_column subheader"> Bar </th> </tr> </thead> <tbody> All columns: <td> sentinel </td> <td> schmentinel </td> One by name: <td> sentinel </td> </tbody> </table>""")
def test_css_class(): class TestTable(NoSortTable): foo = Column(attrs__class__some_class=True) legacy_foo = Column(css_class={"some_other_class"}) legacy_bar = Column(cell__attrs={'class': 'foo'}, cell__attrs__class__bar=True) data = [Struct(foo="foo", legacy_foo="foo", legacy_bar="bar")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column some_class subheader"> Foo </th> <th class="first_column some_other_class subheader"> Legacy foo </th> <th class="first_column subheader"> Legacy bar </th> </tr> </thead> <tbody> <tr class="row1"> <td> foo </td> <td> foo </td> <td class="bar foo"> bar </td> </tr> </tbody> </table>""")
def test_sort_list_bad_parameter(): class TestTable(Table): foo = Column() bar = Column.number(sort_key='bar') data = [Struct(foo='b', bar=2), Struct(foo='a', bar=1)] verify_table_html(table=TestTable(data=data), query=dict(order='barfology'), expected_html="""\ <table class="listview"> <thead> <tr> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> <th class="first_column subheader"> <a href="?order=bar"> Bar </a> </th> </tr> </thead> <tbody> <tr class="row1"> <td> b </td> <td class="rj"> 2 </td> </tr> <tr class="row2"> <td> a </td> <td class="rj"> 1 </td> </tr> </tbody> </table> """)
def test_dict_data(): class TestTable(Table): class Meta: sortable = False a = Column() b = Column() c = Column() data = [{'a': 'a', 'b': 'b', 'c': 'c'}] verify_table_html(TestTable(data), """ <table class="listview"> <thead> <tr> <th class="subheader first_column"> A </th> <th class="subheader first_column"> B </th> <th class="subheader first_column"> C </th> </tr> </thead> <tr class="row1"> <td> a </td> <td> b </td> <td> c </td> </tr> </table>""")
def test_link(): class TestTable(NoSortTable): foo = Column.link(cell__url='https://whereever', cell__url_title="whatever") bar = Column.link(cell__value='bar', cell__url_title=lambda **_: "url_title_goes_here") data = [ Struct(foo='foo', bar=Struct(get_absolute_url=lambda: '/get/absolute/url/result')) ] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> Foo </th> <th class="first_column subheader"> Bar </th> </tr> </thead> <tbody> <tr class="row1"> <td> <a href="https://whereever" title="whatever"> foo </a> </td> <td> <a href="/get/absolute/url/result" title="url_title_goes_here"> bar </a> </td> </tr> </tbody> </table>""")
def test_attrs(): class TestTable(NoSortTable): class Meta: attrs = { 'class': 'classy', 'foo': 'bar' } row_attrs = { 'class': 'classier', 'foo': lambda row: "barier" } yada = Column() verify_table_html(TestTable([(1,), (2,)]), """ <table class="classy" foo="bar"> <thead> <tr> <th class="subheader first_column"> Yada </th> </tr> </thead> <tr class="row1 classier" foo="barier"> <td> 1 </td> </tr> <tr class="row2 classier" foo="barier"> <td> 2 </td> </tr> </table>""")
def test_css_class(): class TestTable(NoSortTable): foo = Column(attrs__class__some_class=True) legacy_foo = Column(css_class={"some_other_class"}) legacy_bar = Column(cell__attrs={'class': 'foo'}, cell__attrs__class__bar=True) data = [Struct(foo="foo", legacy_foo="foo", legacy_bar="bar")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column some_class subheader"> Foo </th> <th class="first_column some_other_class subheader"> Legacy foo </th> <th class="first_column subheader"> Legacy bar </th> </tr> </thead> <tbody> <tr class="row1"> <td> foo </td> <td> foo </td> <td class="bar foo"> bar </td> </tr> </tbody> </table>""")
def test_cell_lambda(): class TestTable(NoSortTable): sentinel1 = "sentinel1" sentinel2 = Column( cell__value=lambda table, column, row, **_: "%s %s %s" % (table.sentinel1, column.name, row.sentinel3) ) data = [Struct(sentinel3="sentinel3")] verify_table_html( table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> Sentinel2 </th></tr> </thead> <tbody> <tr class="row1"> <td> sentinel1 sentinel2 sentinel3 </td> </tr> </tbody> </table>""", )
def test_django_table_pagination(): for x in range(30): Foo(a=x, b="foo").save() class TestTable(Table): a = Column.number( sortable=False ) # turn off sorting to not get the link with random query params b = Column( show=False) # should still be able to filter on this though! verify_table_html(table=TestTable(data=Foo.objects.all()), query=dict(page_size=2, page=2, query='b="foo"'), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> A </th> </tr> </thead> <tbody> <tr class="row1" data-pk="3"> <td class="rj"> 2 </td> </tr> <tr class="row2" data-pk="4"> <td class="rj"> 3 </td> </tr> </tbody> </table>""")
def test_link(): class TestTable(NoSortTable): foo = Column.link(cell__url="https://whereever", cell__url_title="whatever") bar = Column.link(cell__value="bar", cell__url_title=lambda **_: "url_title_goes_here") data = [Struct(foo="foo", bar=Struct(get_absolute_url=lambda: "/get/absolute/url/result"))] verify_table_html( table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> Foo </th> <th class="first_column subheader"> Bar </th> </tr> </thead> <tbody> <tr class="row1"> <td> <a href="https://whereever" title="whatever"> foo </a> </td> <td> <a href="/get/absolute/url/result" title="url_title_goes_here"> bar </a> </td> </tr> </tbody> </table>""", )
def test_row_template(): class TestTable(NoSortTable): foo = Column() bar = Column() class Meta: row__template = lambda table: 'test_table_row.html' data = [Struct(foo="sentinel", bar="schmentinel")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> Foo </th> <th class="first_column subheader"> Bar </th> </tr> </thead> <tbody> All columns: <td> sentinel </td> <td> schmentinel </td> One by name: <td> sentinel </td> </tbody> </table>""")
def test_django_table_pagination(): for x in xrange(30): Foo(a=x, b="foo").save() class TestTable(Table): a = Column.number(sortable=False) # turn off sorting to not get the link with random query params b = Column(show=False) # should still be able to filter on this though! verify_table_html(TestTable(data=Foo.objects.all()), query=dict(page_size=2, page=2, query='b="foo"'), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> A </th> </tr> </thead> <tbody> <tr class="row1" data-pk="3"> <td class="rj"> 2 </td> </tr> <tr class="row2" data-pk="4"> <td class="rj"> 3 </td> </tr> </tbody> </table>""")
def test_attrs(): class TestTable(NoSortTable): class Meta: attrs = { 'class': 'classy', 'foo': lambda table: 'bar' } row__attrs = { 'class': 'classier', 'foo': lambda table, row: "barier" } yada = Column() verify_table_html(table=TestTable(data=[Struct(yada=1), Struct(yada=2)]), expected_html=""" <table class="classy listview" foo="bar"> <thead> <tr> <th class="first_column subheader"> Yada </th> </tr> </thead> <tbody> <tr class="classier row1" foo="barier"> <td> 1 </td> </tr> <tr class="classier row2" foo="barier"> <td> 2 </td> </tr> </tbody> </table>""")
def test_attrs(): class TestTable(NoSortTable): class Meta: attrs = { 'class': 'classy', 'foo': lambda table: 'bar' } row__attrs = { 'class': 'classier', 'foo': lambda table: "barier" } yada = Column() verify_table_html(TestTable(data=[Struct(yada=1), Struct(yada=2)]), """ <table class="classy" foo="bar"> <thead> <tr> <th class="first_column subheader"> Yada </th> </tr> </thead> <tbody> <tr class="classier row1" foo="barier"> <td> 1 </td> </tr> <tr class="classier row2" foo="barier"> <td> 2 </td> </tr> </tbody> </table>""", find=dict(class_='classy'))
def test_django_table(): f1 = Foo.objects.create(a=17, b="Hej") f2 = Foo.objects.create(a=42, b="Hopp") Bar(foo=f1, c=True).save() Bar(foo=f2, c=False).save() class TestTable(Table): foo__a = Column.number() foo__b = Column() foo = Column.choice_queryset( model=Foo, choices=lambda table, column, **_: Foo.objects.all(), query__show=True, bulk__show=True, query__gui__show=True, ) t = TestTable(data=Bar.objects.all()) t.prepare(RequestFactory().get("/", "")) assert list(t.bound_columns[-1].choices) == list(Foo.objects.all()) assert list(t.bulk_form.fields[-1].choices) == list(Foo.objects.all()) assert list(t.query_form.fields[-1].choices) == list(Foo.objects.all()) verify_table_html( table=t, expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> <a href="?order=foo__a"> A </a> </th> <th class="first_column subheader"> <a href="?order=foo__b"> B </a> </th> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> </tr> </thead> <tbody> <tr class="row1" data-pk="1"> <td class="rj"> 17 </td> <td> Hej </td> <td> Foo(17, Hej) </td> </tr> <tr class="row2" data-pk="2"> <td class="rj"> 42 </td> <td> Hopp </td> <td> Foo(42, Hopp) </td> </tr> </tbody> </table>""", )
def test_column_presets(): is_report = False class TestTable(NoSortTable): icon = Column.icon(is_report) edit = Column.edit(is_report) delete = Column.delete(is_report) download = Column.download(is_report) run = Column.run(is_report) select = Column.select(is_report) boolean = Column.boolean(is_report) link = Column.link(cell__format="Yadahada name") number = Column.number() data = [ Struct( pk=123, get_absolute_url=lambda: "http://yada/", boolean=lambda: True, link=Struct(get_absolute_url=lambda: "http://yadahada/"), number=123, ) ] verify_table_html( table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader thin" /> <th class="first_column subheader thin" title="Edit" /> <th class="first_column subheader thin" title="Delete" /> <th class="first_column subheader thin" title="Download" /> <th class="first_column subheader thin" title="Run"> Run </th> <th class="first_column nopad subheader thin" title="Select all"> <i class="fa fa-check-square-o" /> </th> <th class="first_column subheader"> Boolean </th> <th class="first_column subheader"> Link </th> <th class="first_column subheader"> Number </th> </tr> </thead> <tbody> <tr class="row1" data-pk="123"> <td class="cj"> <i class="fa fa-lg fa-False" /> </td> <td class="cj"> <a href="http://yada/edit/"> <i class="fa fa-lg fa-pencil-square-o" title="Edit" /> </a> </td> <td class="cj"> <a href="http://yada/delete/"> <i class="fa fa-lg fa-trash-o" title="Delete" /> </a> </td> <td class="cj"> <a href="http://yada/download/"> <i class="fa fa-lg fa-download" title="Download" /> </a> </td> <td> <a href="http://yada/run/"> Run </a> </td> <td class="cj"> <input class="checkbox" name="pk_123" type="checkbox"/> </td> <td class="cj"> <i class="fa fa-check" title="Yes" /> </td> <td> <a href="http://yadahada/"> Yadahada name </a> </td> <td class="rj"> 123 </td> </tr> </tbody> </table>""", )
def test_output(): is_report = False class TestTable(Table): class Meta: attrs = { 'class': 'listview', 'id': 'table_id', } foo = Column() bar = Column.number() icon = Column.icon('history', is_report, group="group") edit = Column.edit(is_report, group="group") delete = Column.delete(is_report) data = [ Struct(foo="Hello räksmörgås ><&>", bar=17, get_absolute_url=lambda: '/somewhere/'), ] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview" id="table_id"> <thead> <tr> <th class="first_column superheader" colspan="1"> </th> <th class="superheader" colspan="1"> </th> <th class="superheader" colspan="2"> group </th> <th class="superheader" colspan="1"> </th> </tr> <tr> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> <th class="first_column subheader"> <a href="?order=bar"> Bar </a> </th> <th class="first_column subheader thin"> </th> <th class="subheader thin" title="Edit"> </th> <th class="first_column subheader thin" title="Delete"> </th> </tr> </thead> <tbody> <tr class="row1"> <td> Hello räksmörgås ><&> </td> <td class="rj"> 17 </td> <td class="cj"> <i class="fa fa-lg fa-history"> </i> </td> <td class="cj"> <a href="/somewhere/edit/"> <i class="fa fa-lg fa-pencil-square-o" title="Edit"> </i> </a> </td> <td class="cj"> <a href="/somewhere/delete/"> <i class="fa fa-lg fa-trash-o" title="Delete"> </i> </a> </td> </tr> </tbody> </table> """)
def test_django_table(): f1 = Foo.objects.create(a=17, b="Hej") f2 = Foo.objects.create(a=42, b="Hopp") Bar(foo=f1, c=True).save() Bar(foo=f2, c=False).save() class TestTable(Table): foo__a = Column.number() foo__b = Column() foo = Column.choice_queryset( model=Foo, choices=lambda table, column: Foo.objects.all(), query__show=True, bulk__show=True, query__gui__show=True) t = TestTable(data=Bar.objects.all()) t.prepare(RequestFactory().get("/", '')) assert list(t.bound_columns[-1].choices) == list(Foo.objects.all()) assert list(t.bulk_form.fields[-1].choices) == list(Foo.objects.all()) assert list(t.query_form.fields[-1].choices) == list(Foo.objects.all()) verify_table_html(table=t, expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> <a href="?order=foo__a"> A </a> </th> <th class="first_column subheader"> <a href="?order=foo__b"> B </a> </th> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> </tr> </thead> <tbody> <tr class="row1" data-pk="1"> <td class="rj"> 17 </td> <td> Hej </td> <td> Foo(17, Hej) </td> </tr> <tr class="row2" data-pk="2"> <td class="rj"> 42 </td> <td> Hopp </td> <td> Foo(42, Hopp) </td> </tr> </tbody> </table>""")
def test_output(): is_report = False class TestTable(Table): class Meta: attrs = { 'class': 'listview', 'id': 'table_id', } foo = Column() bar = Column.number() icon = Column.icon('history', is_report, group="group") edit = Column.edit(is_report, group="group") delete = Column.delete(is_report) data = [ Struct(foo="Hello räksmörgås ><&>", bar=17, get_absolute_url=lambda: '/somewhere/'), ] verify_table_html(TestTable(data=data), """ <table class="listview" id="table_id"> <thead> <tr> <th class="first_column superheader" colspan="1"> </th> <th class="superheader" colspan="1"> </th> <th class="superheader" colspan="2"> group </th> <th class="superheader" colspan="1"> </th> </tr> <tr> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> <th class="first_column subheader"> <a href="?order=bar"> Bar </a> </th> <th class="first_column subheader thin"> </th> <th class="subheader thin" title="Edit"> </th> <th class="first_column subheader thin" title="Delete"> </th> </tr> </thead> <tbody> <tr class="row1"> <td> Hello räksmörgås ><&> </td> <td class="rj"> 17 </td> <td class="cj"> <i class="fa fa-lg fa-history"> </i> </td> <td class="cj"> <a href="/somewhere/edit/"> <i class="fa fa-lg fa-pencil-square-o" title="Edit"> </i> </a> </td> <td class="cj"> <a href="/somewhere/delete/"> <i class="fa fa-lg fa-trash-o" title="Delete"> </i> </a> </td> </tr> </tbody> </table> """)
def test_actions(): class TestTable(Table): foo = Column(header__attrs__title="Some title") class Meta: sortable = False actions = dict( a=Action(display_name='Foo', attrs__href='/foo/', include=lambda table, **_: table.rows is not rows), b=Action(display_name='Bar', attrs__href='/bar/', include=lambda table, **_: table.rows is rows), c=Action(display_name='Baz', attrs__href='/bar/', group='Other'), d=dict(display_name='Qux', attrs__href='/bar/', group='Other'), e=Action.icon('icon_foo', display_name='Icon foo', attrs__href='/icon_foo/'), f=Action.icon('icon_bar', icon_classes=['lg'], display_name='Icon bar', attrs__href='/icon_bar/'), g=Action.icon('icon_baz', icon_classes=['one', 'two'], display_name='Icon baz', attrs__href='/icon_baz/'), ) rows = [Struct(foo="foo")] verify_table_html( table=TestTable(rows=rows), find=dict(class_='links'), expected_html=""" <div class="links"> <div class="btn-group"> <button class="btn btn-primary dropdown-toggle" data-target="#" data-toggle="dropdown" href="/page.html" id="id_dropdown_other" role="button" type="button"> Other </button> <div aria-labelledby="id_dropdown_Other" class="dropdown-menu" role="menu"> <a class="dropdown-item" href="/bar/" role="menuitem"> Baz </a> <a class="dropdown-item" href="/bar/" role="menuitem"> Qux </a> </div> </div> <a href="/bar/"> Bar </a> <a href="/icon_foo/"> <i class="fa fa-icon_foo " /> Icon foo </a> <a href="/icon_bar/"> <i class="fa fa-icon_bar fa-lg" /> Icon bar </a> <a href="/icon_baz/"> <i class="fa fa-icon_baz fa-one fa-two" /> Icon baz </a> </div>""", )
def test_column_presets(): is_report = False class TestTable(NoSortTable): icon = Column.icon(is_report) edit = Column.edit(is_report) delete = Column.delete(is_report) download = Column.download(is_report) run = Column.run(is_report) select = Column.select(is_report) boolean = Column.boolean(is_report) link = Column.link(cell__format="Yadahada name") number = Column.number() data = [ Struct(pk=123, get_absolute_url=lambda: "http://yada/", boolean=lambda: True, link=Struct(get_absolute_url=lambda: "http://yadahada/"), number=123) ] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader thin" /> <th class="first_column subheader thin" title="Edit" /> <th class="first_column subheader thin" title="Delete" /> <th class="first_column subheader thin" title="Download" /> <th class="first_column subheader thin" title="Run"> Run </th> <th class="first_column nopad subheader thin" title="Select all"> <i class="fa fa-check-square-o" /> </th> <th class="first_column subheader"> Boolean </th> <th class="first_column subheader"> Link </th> <th class="first_column subheader"> Number </th> </tr> </thead> <tbody> <tr class="row1" data-pk="123"> <td class="cj"> <i class="fa fa-lg fa-False" /> </td> <td class="cj"> <a href="http://yada/edit/"> <i class="fa fa-lg fa-pencil-square-o" title="Edit" /> </a> </td> <td class="cj"> <a href="http://yada/delete/"> <i class="fa fa-lg fa-trash-o" title="Delete" /> </a> </td> <td class="cj"> <a href="http://yada/download/"> <i class="fa fa-lg fa-download" title="Download" /> </a> </td> <td> <a href="http://yada/run/"> Run </a> </td> <td class="cj"> <input class="checkbox" name="pk_123" type="checkbox"/> </td> <td class="cj"> <i class="fa fa-check" title="Yes" /> </td> <td> <a href="http://yadahada/"> Yadahada name </a> </td> <td class="rj"> 123 </td> </tr> </tbody> </table>""")
def test_title(): class TestTable(NoSortTable): foo = Column(title="Some title") data = [Struct(foo="foo")] verify_table_html(TestTable(data), """ <table class="listview"> <thead> <tr><th class="subheader first_column" title="Some title"> Foo </th></tr> </thead> <tr class="row1"> <td> foo </td> </tr> </table>""")
def test_show(): class TestTable(NoSortTable): foo = Column() bar = Column(show=False) data = [Struct(foo="foo", bar="bar")] verify_table_html(TestTable(data), """ <table class="listview"> <thead> <tr><th class="subheader first_column"> Foo </th></tr> </thead> <tr class="row1"> <td> foo </td> </tr> </table>""")
def test_links(): class TestTable(NoSortTable): foo = Column(title="Some title") data = [Struct(foo="foo")] links = [ Link('Foo', url='/foo/', show=lambda table: table.data is not data), Link('Bar', url='/bar/', show=lambda table: table.data is data), Link('Baz', url='/bar/', group='Other'), Link('Qux', url='/bar/', group='Other'), Link.icon('icon_foo', title='Icon foo', url='/icon_foo/'), Link.icon('icon_bar', icon_classes=['lg'], title='Icon bar', url='/icon_bar/'), Link.icon('icon_baz', icon_classes=['one', 'two'], title='Icon baz', url='/icon_baz/'), ] verify_table_html(table=TestTable(data=data), find=dict(class_='links'), links=links, expected_html=""" <div class="links"> <div class="dropdown"> <a class="button button-primary" data-target="#" data-toggle="dropdown" href="/page.html" id="id_dropdown_other" role="button"> Other <i class="fa fa-lg fa-caret-down" /> </a> <ul aria-labelledby="id_dropdown_Other" class="dropdown-menu" role="menu"> <li role="presentation"> <a href="/bar/" role="menuitem"> Baz </a> </li> <li role="presentation"> <a href="/bar/" role="menuitem"> Qux </a> </li> </ul> </div> <a href="/bar/"> Bar </a> <a href="/icon_foo/"> <i class="fa fa-icon_foo " /> Icon foo </a> <a href="/icon_bar/"> <i class="fa fa-icon_bar fa-lg" /> Icon bar </a> <a href="/icon_baz/"> <i class="fa fa-icon_baz fa-one fa-two" /> Icon baz </a> </div>""")
def test_display_name(): class TestTable(NoSortTable): foo = Column(display_name="Bar") data = [Struct(foo="foo")] verify_table_html(TestTable(data), """ <table class="listview"> <thead> <tr> <th class="subheader first_column"> Bar </th> </tr> </thead> <tr class="row1"> <td> foo </td> </tr> </table>""")
def test_header_url(): class TestTable(NoSortTable): foo = Column(url="/some/url") data = [Struct(foo="foo")] verify_table_html(TestTable(data), """ <table class="listview"> <thead> <tr><th class="subheader first_column"> <a href="/some/url"> Foo </a> </th></tr> </thead> <tr class="row1"> <td> foo </td> </tr> </table>""")
def test_css_class(): class TestTable(NoSortTable): foo = Column(css_class={"some_class"}) data = [Struct(foo="foo")] verify_table_html(TestTable(data=data), """ <table class="listview"> <thead> <tr><th class="first_column some_class subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> <td> foo </td> </tr> </tbody> </table>""")
def test_cell_template(): class TestTable(NoSortTable): foo = Column(cell_template='test_cell_template.html') data = [Struct(foo="foo")] verify_table_html(TestTable(data), """ <table class="listview"> <thead> <tr><th class="subheader first_column"> Foo </th></tr> </thead> <tr class="row1"> <td> test_cell_template.html contents </td> </tr> </table>""")
def test_name_traversal(): class TestTable(Table): foo__bar = Column(sortable=False) data = [Struct(foo=Struct(bar="bar"))] verify_table_html(TestTable(data), """ <table class="listview"> <thead> <tr> <th class="subheader first_column"> Bar </th> </tr> </thead> <tr class="row1"> <td> bar </td> </tr> </table>""")
def test_sort_list_with_none_values(): class TestTable(Table): foo = Column() bar = Column.number(sort_key='bar') rows = [ Struct(foo='c', bar=3), Struct(foo='b', bar=2), Struct(foo='a', bar=None), Struct(foo='a', bar=None), ] verify_table_html(table=TestTable(rows=rows), query=dict(order='bar'), expected_html="""\ <table class="table" data-endpoint="/tbody"> <thead> <tr> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> <th class="ascending first_column sorted_column subheader"> <a href="?order=-bar"> Bar </a> </th> </tr> </thead> <tbody> <tr> <td> a </td> <td class="rj"> </td> </tr> <tr> <td> a </td> <td class="rj"> </td> </tr> <tr> <td> b </td> <td class="rj"> 2 </td> </tr> <tr> <td> c </td> <td class="rj"> 3 </td> </tr> </tbody> </table> """)
def test_sort_with_name(): class TestTable(Table): class Meta: name = 'my_table' foo = Column() bar = Column.number(sort_key='bar') rows = [ Struct(foo='c', bar=3), Struct(foo='b', bar=2), Struct(foo='a', bar=1), ] table = TestTable(rows=rows, default_child=False) verify_table_html(table=table, query={'my_table/order': 'bar'}, expected_html="""\ <table class="table" data-endpoint="/my_table/tbody"> <thead> <tr> <th class="first_column subheader"> <a href="?my_table%2Forder=foo"> Foo </a> </th> <th class="ascending first_column sorted_column subheader"> <a href="?my_table%2Forder=-bar"> Bar </a> </th> </tr> </thead> <tbody> <tr> <td> a </td> <td class="rj"> 1 </td> </tr> <tr> <td> b </td> <td class="rj"> 2 </td> </tr> <tr> <td> c </td> <td class="rj"> 3 </td> </tr> </tbody> </table> """)
def test_row_template(): class TestTable(NoSortTable): foo = Column() class Meta: row__template = lambda table: 'test_table_row.html' data = [Struct(foo="sentinel")] verify_table_html(TestTable(data=data), """ <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> sentinel </tbody> </table>""")
def test_title(): class TestTable(NoSortTable): foo = Column(title="Some title") data = [Struct(foo="foo")] verify_table_html(table=TestTable(data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader" title="Some title"> Foo </th></tr> </thead>\ <tbody> <tr class="row1"> <td> foo </td> </tr> </tbody> </table>""")
def test_sort_with_name(): class TestTable(Table): class Meta: name = 'my_table' foo = Column() bar = Column.number(sort_key='bar') data = [ Struct(foo='c', bar=3), Struct(foo='b', bar=2), Struct(foo='a', bar=1) ] verify_table_html(table=(TestTable(data=data)), query={'my_table/order': 'bar'}, expected_html="""\ <table class="listview"> <thead> <tr> <th class="first_column subheader"> <a href="?my_table%2Forder=foo"> Foo </a> </th> <th class="ascending first_column sorted_column subheader"> <a href="?my_table%2Forder=-bar"> Bar </a> </th> </tr> </thead> <tbody> <tr> <td> a </td> <td class="rj"> 1 </td> </tr> <tr> <td> b </td> <td class="rj"> 2 </td> </tr> <tr> <td> c </td> <td class="rj"> 3 </td> </tr> </tbody> </table> """)
def test_no_header_template(): class TestTable(NoSortTable): class Meta: header__template = None foo = Column() data = [Struct(foo="bar")] verify_table_html(TestTable(data=data), """ <table class="listview"> <tbody> <tr class="row1"> <td> bar </td> </tr> </tbody> </table>""")
def test_sort_default_desc_already_sorted(): class TestTable(Table): foo = Column() bar = Column(sort_default_desc=True) verify_table_html(table=TestTable(rows=[]), query=dict(order='bar'), find=dict(name='thead'), expected_html="""\ <thead> <tr> <th class="first_column subheader"> <a href="?order=foo"> Foo </a> </th> <th class="ascending first_column sorted_column subheader"> <a href="?order=-bar"> Bar </a> </th> </thead> """)
def test_show(): class TestTable(NoSortTable): foo = Column() bar = Column(show=False) data = [Struct(foo="foo", bar="bar")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> <td> foo </td> </tr> </tbody> </table>""")
def test_no_header_template(): class TestTable(NoSortTable): class Meta: header__template = None foo = Column() data = [Struct(foo="bar")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <tbody> <tr class="row1"> <td> bar </td> </tr> </tbody> </table>""")
def test_name_traversal(): class TestTable(Table): foo__bar = Column(sortable=False) data = [Struct(foo=Struct(bar="bar"))] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> Bar </th> </tr> </thead> <tbody> <tr class="row1"> <td> bar </td> </tr> </tbody> </table>""")
def test_display_name(): class TestTable(NoSortTable): foo = Column(display_name="Bar") data = [Struct(foo="foo")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr> <th class="first_column subheader"> Bar </th> </tr> </thead> <tbody> <tr class="row1"> <td> foo </td> </tr> </tbody> </table>""")
def test_header_url(): class TestTable(NoSortTable): foo = Column(url="/some/url") data = [Struct(foo="foo")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> <a href="/some/url"> Foo </a> </th></tr> </thead> <tbody> <tr class="row1"> <td> foo </td> </tr> </tbody> </table>""")
def test_sort_django_table(): Foo(a=4711, b="c").save() Foo(a=17, b="a").save() Foo(a=42, b="b").save() class TestTable(Table): a = Column.number() b = Column() verify_table_html(table=TestTable(data=Foo.objects.all()), query=dict(order='a'), expected_html="""\ <table class="listview"> <thead> <tr> <th class="first_column sorted_column subheader"> <a href="?order=-a"> A </a> </th> <th class="first_column subheader"> <a href="?order=b"> B </a> </th> </tr> </thead> <tbody> <tr class="row1" data-pk="2"> <td class="rj"> 17 </td> <td> a </td> </tr> <tr class="row2" data-pk="3"> <td class="rj"> 42 </td> <td> b </td> </tr> <tr class="row1" data-pk="1"> <td class="rj"> 4711 </td> <td> c </td> </tr> </tbody> </table> """)
def test_cell_lambda(): class TestTable(NoSortTable): sentinel1 = 'sentinel1' sentinel2 = Column(cell__value=lambda table, column, row: '%s %s %s' % (table.sentinel1, column.name, row.sentinel3)) data = [Struct(sentinel3="sentinel3")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> Sentinel2 </th></tr> </thead> <tbody> <tr class="row1"> <td> sentinel1 sentinel2 sentinel3 </td> </tr> </tbody> </table>""")
def test_cell_template(): def explode(**_): assert False class TestTable(NoSortTable): foo = Column(cell__template='test_cell_template.html', cell__format=explode, cell__url=explode, cell__url_title=explode) data = [Struct(foo="sentinel")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> Custom rendered: sentinel </tr> </tbody> </table>""")
def test_sort_django_table_from_model(): TFoo(a=4711, b="c").save() TFoo(a=17, b="a").save() TFoo(a=42, b="b").save() verify_table_html(table__rows=TFoo.objects.all(), query=dict(order='a'), expected_html="""\ <table class="table" data-endpoint="/tbody"> <thead> <tr> <th class="ascending first_column sorted_column subheader"> <a href="?order=-a"> A </a> </th> <th class="first_column subheader"> <a href="?order=b"> B </a> </th> </tr> </thead> <tbody> <tr data-pk="2"> <td class="rj"> 17 </td> <td> a </td> </tr> <tr data-pk="3"> <td class="rj"> 42 </td> <td> b </td> </tr> <tr data-pk="1"> <td class="rj"> 4711 </td> <td> c </td> </tr> </tbody> </table> """)
def test_show_lambda(): def show_callable(table, column): assert isinstance(table, TestTable) assert column.name == 'bar' return False class TestTable(NoSortTable): foo = Column() bar = Column.icon('foo', show=show_callable) data = [Struct(foo="foo", bar="bar")] verify_table_html(table=TestTable(data=data), expected_html=""" <table class="listview"> <thead> <tr><th class="first_column subheader"> Foo </th></tr> </thead> <tbody> <tr class="row1"> <td> foo </td> </tr> </tbody> </table>""")
def test_sort_django_table(): TFoo(a=4711, b="c").save() TFoo(a=17, b="a").save() TFoo(a=42, b="b").save() class TestTable(Table): a = Column.number() b = Column() verify_table_html(table=TestTable(rows=TFoo.objects.all()), query=dict(order='a'), expected_html="""\ <table class="table" data-endpoint="/tbody"> <thead> <tr> <th class="ascending first_column sorted_column subheader"> <a href="?order=-a"> A </a> </th> <th class="first_column subheader"> <a href="?order=b"> B </a> </th> </tr> </thead> <tbody> <tr data-pk="2"> <td class="rj"> 17 </td> <td> a </td> </tr> <tr data-pk="3"> <td class="rj"> 42 </td> <td> b </td> </tr> <tr data-pk="1"> <td class="rj"> 4711 </td> <td> c </td> </tr> </tbody> </table> """) # now reversed verify_table_html(table=TestTable(rows=TFoo.objects.all()), query=dict(order='-a'), expected_html="""\ <table class="table" data-endpoint="/tbody"> <thead> <tr> <th class="descending first_column sorted_column subheader"> <a href="?order=a"> A </a> </th> <th class="first_column subheader"> <a href="?order=b"> B </a> </th> </tr> </thead> <tbody> <tr data-pk="1"> <td class="rj"> 4711 </td> <td> c </td> </tr> <tr data-pk="3"> <td class="rj"> 42 </td> <td> b </td> </tr> <tr data-pk="2"> <td class="rj"> 17 </td> <td> a </td> </tr> </tbody> </table> """)