def render_table_templatetag(): # ensure it works with a multi-order-by request = build_request('/') table = CountryTable(MEMORY_DATA, order_by=('name', 'population')) RequestConfig(request).configure(table) template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render(Context({'request': request, 'table': table})) root = parse(html) assert len(root.findall('.//thead/tr')) == 1 assert len(root.findall('.//thead/tr/th')) == 4 assert len(root.findall('.//tbody/tr')) == 4 assert len(root.findall('.//tbody/tr/td')) == 16 assert root.find( 'ul[@class="pagination"]/li[@class="cardinality"]').text == '4 items' # no data with no empty_text table = CountryTable([]) template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render( Context({ 'request': build_request('/'), 'table': table })) root = parse(html) assert len(root.findall('.//thead/tr')) == 1 assert len(root.findall('.//thead/tr/th')) == 4 assert len(root.findall('.//tbody/tr')) == 0 # no data WITH empty_text request = build_request('/') table = CountryTable([], empty_text='this table is empty') RequestConfig(request).configure(table) template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render(Context({'request': request, 'table': table})) root = parse(html) assert len(root.findall('.//thead/tr')) == 1 assert len(root.findall('.//thead/tr/th')) == 4 assert len(root.findall('.//tbody/tr')) == 1 assert len(root.findall('.//tbody/tr/td')) == 1 assert int(root.find('.//tbody/tr/td').attrib['colspan']) == len( root.findall('.//thead/tr/th')) assert root.find('.//tbody/tr/td').text == 'this table is empty' # variable that doesn't exist (issue #8) template = Template('{% load django_tables2 %}' '{% render_table this_doesnt_exist %}') with raises(ValueError): with settings(DEBUG=True): template.render(Context()) # Should still be noisy with debug off with raises(ValueError): with settings(DEBUG=False): template.render(Context())
def render_table_templatetag(): # ensure it works with a multi-order-by request = build_request('/') table = CountryTable(MEMORY_DATA, order_by=('name', 'population')) RequestConfig(request).configure(table) template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render(Context({'request': request, 'table': table})) root = parse(html) assert len(root.findall('.//thead/tr')) == 1 assert len(root.findall('.//thead/tr/th')) == 4 assert len(root.findall('.//tbody/tr')) == 4 assert len(root.findall('.//tbody/tr/td')) == 16 assert root.find('ul[@class="pagination"]/li[@class="cardinality"]').text == '4 items' # no data with no empty_text table = CountryTable([]) template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render(Context({'request': build_request('/'), 'table': table})) root = parse(html) assert len(root.findall('.//thead/tr')) == 1 assert len(root.findall('.//thead/tr/th')) == 4 assert len(root.findall('.//tbody/tr')) == 0 # no data WITH empty_text request = build_request('/') table = CountryTable([], empty_text='this table is empty') RequestConfig(request).configure(table) template = Template('{% load django_tables2 %}{% render_table table %}') html = template.render(Context({'request': request, 'table': table})) root = parse(html) assert len(root.findall('.//thead/tr')) == 1 assert len(root.findall('.//thead/tr/th')) == 4 assert len(root.findall('.//tbody/tr')) == 1 assert len(root.findall('.//tbody/tr/td')) == 1 assert int(root.find('.//tbody/tr/td').attrib['colspan']) == len(root.findall('.//thead/tr/th')) assert root.find('.//tbody/tr/td').text == 'this table is empty' # variable that doesn't exist (issue #8) template = Template('{% load django_tables2 %}' '{% render_table this_doesnt_exist %}') with raises(ValueError): with settings(DEBUG=True): template.render(Context()) # Should still be noisy with debug off with raises(ValueError): with settings(DEBUG=False): template.render(Context())
def localization_check_in_meta(): class TableNoLocalize(tables.Table): name = tables.Column(verbose_name="my column") class Meta: default = "---" class TableLocalize(tables.Table): name = tables.Column(verbose_name="my column") class Meta: default = "---" localize = ('name',) class TableUnlocalize(tables.Table): name = tables.Column(verbose_name="my column") class Meta: default = "---" unlocalize = ('name',) class TableLocalizePrecedence(tables.Table): name = tables.Column(verbose_name="my column") class Meta: default = "---" unlocalize = ('name',) localize = ('name',) simple_test_data = [{'name': 1234.5}] expected_reults = { None: '1234.5', False: '1234.5', True: '1{0}234,5'.format(' ') # non-breaking space } # No localize html = TableNoLocalize(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[None]) in html with settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): with translation("pl"): # the same as in localization_check. # with localization and polish locale we get formatted output html = TableNoLocalize(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[True]) in html # localize html = TableLocalize(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[True]) in html # unlocalize html = TableUnlocalize(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[False]) in html # test unlocalize higher precedence html = TableLocalizePrecedence(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[False]) in html
def should_handle_short_format(dt): class TestTable(tables.Table): date = tables.DateTimeColumn(short=True) class Meta: default = "—" with settings(SHORT_DATETIME_FORMAT="b Y D A f"): table = TestTable([{"date": dt}, {"date": None}]) assert table.rows[0]["date"] == "sep 2012 Tue PM 12:30" assert table.rows[1]["date"] == "—"
def should_handle_long_format(dt): class TestTable(tables.Table): date = tables.DateTimeColumn(short=False) class Meta: default = "—" with settings(DATETIME_FORMAT="D Y b A f"): table = TestTable([{"date": dt}, {"date": None}]) assert table.rows[0]["date"] == "Tue 2012 sep PM 12:30" assert table.rows[1]["date"] == "—"
def should_handle_short_format(): with settings(SHORT_DATE_FORMAT="b Y D"): class TestTable(tables.Table): date = tables.DateColumn(short=True) class Meta: default = "—" table = TestTable([{"date": date(2012, 9, 11)}, {"date": None}]) assert table.rows[0]["date"] == "sep 2012 Tue" assert table.rows[1]["date"] == "—"
def should_handle_long_format(): with settings(DATE_FORMAT="D Y b"): class TestTable(tables.Table): date = tables.DateColumn(short=False) class Meta: default = "—" table = TestTable([{"date": date(2012, 9, 11)}, {"date": None}]) assert table.rows[0]["date"] == "Tue 2012 sep" assert table.rows[1]["date"] == "—"
def localization_check(): import django if django.VERSION < (1, 3): # there's no `l10n` library tag prior to Django 1.3 pass else: from django_tables2.utils import override_translation def get_cond_localized_table(localizeit=None): ''' helper function for defining Table class conditionally ''' class TestTable(tables.Table): name = tables.Column(verbose_name="my column", localize=localizeit) return TestTable simple_test_data = [{ 'name' : 1234.5 }] expected_reults = { None : '1234.5', False: '1234.5', True : '1{0}234,5'.format(' ') # non-breaking space } # no localization html = get_cond_localized_table(None)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[None]) in html # unlocalize html = get_cond_localized_table(False)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[False]) in html with settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): with override_translation("pl"): # with default polish locales and enabled thousand separator # 1234.5 is formatted as "1 234,5" with nbsp html = get_cond_localized_table(True)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[True]) in html # with localize = False there should be no formatting html = get_cond_localized_table(False)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[False]) in html # with localize = None and USE_L10N = True # there should be the same formatting as with localize = True html = get_cond_localized_table(None)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[True]) in html
def localization_check(): def get_cond_localized_table(localizeit=None): ''' helper function for defining Table class conditionally ''' class TestTable(tables.Table): name = tables.Column(verbose_name="my column", localize=localizeit) return TestTable simple_test_data = [{'name': 1234.5}] expected_reults = { None: '1234.5', False: '1234.5', True: '1 234,5' # non-breaking space } # no localization html = get_cond_localized_table(None)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[None]) in html # unlocalize html = get_cond_localized_table(False)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[False]) in html with settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): with translation("pl"): # with default polish locales and enabled thousand separator # 1234.5 is formatted as "1 234,5" with nbsp html = get_cond_localized_table(True)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format( expected_reults[True]) in html # with localize = False there should be no formatting html = get_cond_localized_table(False)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format( expected_reults[False]) in html # with localize = None and USE_L10N = True # there should be the same formatting as with localize = True html = get_cond_localized_table(None)(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format( expected_reults[True]) in html
def changed_settings(): from django.conf import settings sentinel = object() catcher = [] # use a list for scoping original = settings.DEBUG def pitcher(sender, setting, value, **kwargs): catcher.append({"setting": setting, "value": value}) signals.setting_changed.connect(pitcher) with django_attest.settings(DEBUG=sentinel): assert settings.DEBUG is sentinel assert settings.DEBUG == original assert catcher == [{"setting": "DEBUG", "value": sentinel}, {"setting": "DEBUG", "value": original}] signals.setting_changed.disconnect(pitcher)
def localization_check_in_meta(): import django if django.VERSION < (1, 3): # there's no `l10n` library tag prior to Django 1.3 pass else: from django_tables2.utils import override_translation class TableNoLocalize(tables.Table): name = tables.Column(verbose_name="my column") class Meta: default = "---" class TableLocalize(tables.Table): name = tables.Column(verbose_name="my column") class Meta: default = "---" localize = ('name',) class TableUnlocalize(tables.Table): name = tables.Column(verbose_name="my column") class Meta: default = "---" unlocalize = ('name',) class TableLocalizePrecedence(tables.Table): name = tables.Column(verbose_name="my column") class Meta: default = "---" unlocalize = ('name',) localize = ('name',) simple_test_data = [{ 'name' : 1234.5 }] expected_reults = { None : '1234.5', False: '1234.5', True : u'1{0}234,5'.format(u' ') # non-breaking space } # No localize html = TableNoLocalize(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[None]) in html with settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True): with override_translation("pl"): # the same as in localization_check. # with localization and polish locale we get formatted output html = TableNoLocalize(simple_test_data).as_html() assert u'<td class="name">{0}</td>'.format(expected_reults[True]) in html # localize html = TableLocalize(simple_test_data).as_html() assert u'<td class="name">{0}</td>'.format(expected_reults[True]) in html # unlocalize html = TableUnlocalize(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[False]) in html # test unlocalize higher precedence html = TableLocalizePrecedence(simple_test_data).as_html() assert '<td class="name">{0}</td>'.format(expected_reults[False]) in html