2 from django.template import Template, Context, VariableDoesNotExist
3 from django.http import HttpRequest
4 from django.conf import settings
5 import django_tables2 as tables
6 from attest import Tests, Assert
7 from xml.etree import ElementTree as ET
13 class CountryTable(tables.Table):
14 name = tables.Column()
15 capital = tables.Column(sortable=False)
16 population = tables.Column(verbose_name='Population Size')
17 currency = tables.Column(visible=False)
18 tld = tables.Column(visible=False, verbose_name='Domain')
19 calling_code = tables.Column(accessor='cc',
20 verbose_name='Phone Ext.')
24 {'name': 'Germany', 'capital': 'Berlin', 'population': 83,
25 'currency': 'Euro (€)', 'tld': 'de', 'cc': 49},
26 {'name': 'France', 'population': 64, 'currency': 'Euro (€)',
27 'tld': 'fr', 'cc': 33},
28 {'name': 'Netherlands', 'capital': 'Amsterdam', 'cc': '31'},
29 {'name': 'Austria', 'cc': 43, 'currency': 'Euro (€)',
36 table = CountryTable(MEMORY_DATA)
37 root = ET.fromstring(table.as_html())
38 Assert(len(root.findall('.//thead/tr'))) == 1
39 Assert(len(root.findall('.//thead/tr/th'))) == 4
40 Assert(len(root.findall('.//tbody/tr'))) == 4
41 Assert(len(root.findall('.//tbody/tr/td'))) == 16
43 # no data with no empty_text
44 table = CountryTable([])
45 root = ET.fromstring(table.as_html())
46 Assert(1) == len(root.findall('.//thead/tr'))
47 Assert(4) == len(root.findall('.//thead/tr/th'))
48 Assert(0) == len(root.findall('.//tbody/tr'))
50 # no data WITH empty_text
51 table = CountryTable([], empty_text='this table is empty')
52 root = ET.fromstring(table.as_html())
53 Assert(1) == len(root.findall('.//thead/tr'))
54 Assert(4) == len(root.findall('.//thead/tr/th'))
55 Assert(1) == len(root.findall('.//tbody/tr'))
56 Assert(1) == len(root.findall('.//tbody/tr/td'))
57 Assert(int(root.find('.//tbody/tr/td').attrib['colspan'])) == len(root.findall('.//thead/tr/th'))
58 Assert(root.find('.//tbody/tr/td').text) == 'this table is empty'
62 def custom_rendering():
63 """For good measure, render some actual templates."""
64 countries = CountryTable(MEMORY_DATA)
65 context = Context({'countries': countries})
67 # automatic and manual column verbose names
68 template = Template('{% for column in countries.columns %}{{ column }}/'
69 '{{ column.name }} {% endfor %}')
70 result = ('Name/name Capital/capital Population Size/population '
71 'Phone Ext./calling_code ')
72 Assert(result) == template.render(context)
75 template = Template('{% for row in countries.rows %}{% for value in row %}'
76 '{{ value }} {% endfor %}{% endfor %}')
77 result = ('Germany Berlin 83 49 France None 64 33 Netherlands Amsterdam '
78 'None 31 Austria None 8 43 ')
79 Assert(result) == template.render(context)
84 # ensure it works with a multi-order-by
85 table = CountryTable(MEMORY_DATA, order_by=('name', 'population'))
86 t = Template('{% load django_tables2 %}{% render_table table %}')
87 html = t.render(Context({'request': HttpRequest(), 'table': table}))
89 root = ET.fromstring(html)
90 Assert(len(root.findall('.//thead/tr'))) == 1
91 Assert(len(root.findall('.//thead/tr/th'))) == 4
92 Assert(len(root.findall('.//tbody/tr'))) == 4
93 Assert(len(root.findall('.//tbody/tr/td'))) == 16
95 # no data with no empty_text
96 table = CountryTable([])
97 t = Template('{% load django_tables2 %}{% render_table table %}')
98 html = t.render(Context({'request': HttpRequest(), 'table': table}))
99 root = ET.fromstring(html)
100 Assert(len(root.findall('.//thead/tr'))) == 1
101 Assert(len(root.findall('.//thead/tr/th'))) == 4
102 Assert(len(root.findall('.//tbody/tr'))) == 0
104 # no data WITH empty_text
105 table = CountryTable([], empty_text='this table is empty')
106 t = Template('{% load django_tables2 %}{% render_table table %}')
107 html = t.render(Context({'request': HttpRequest(), 'table': table}))
108 root = ET.fromstring(html)
109 Assert(len(root.findall('.//thead/tr'))) == 1
110 Assert(len(root.findall('.//thead/tr/th'))) == 4
111 Assert(len(root.findall('.//tbody/tr'))) == 1
112 Assert(len(root.findall('.//tbody/tr/td'))) == 1
113 Assert(int(root.find('.//tbody/tr/td').attrib['colspan'])) == len(root.findall('.//thead/tr/th'))
114 Assert(root.find('.//tbody/tr/td').text) == 'this table is empty'
116 # variable that doesn't exist (issue #8)
117 t = Template('{% load django_tables2 %}{% render_table this_doesnt_exist %}')
118 with Assert.raises(VariableDoesNotExist):
119 settings.DEBUG = True
122 # Should be silent with debug off
123 settings.DEBUG = False