def test_row_and_column_headers(self): """ Test row and column headers at the same time. """ value = { 'first_row_is_table_header': True, 'first_col_is_header': True, 'data': [['Foo', 'Bar', 'Baz'], ['one', 'two', 'three'], ['four', 'five', 'six']] } expected = """ <table> <thead> <tr><th scope="col">Foo</th><th scope="col">Bar</th><th scope="col">Baz</th></tr> </thead> <tbody> <tr><th scope="row">one</th><td>two</td><td>three</td></tr> <tr><th scope="row">four</th><td>five</td><td>six</td></tr> </tbody> </table> """ block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
class BankingStreamFieldPage(Page): body = StreamField([ ('paragraph', blocks.RichTextBlock()), ('bankrate_widget', BankrateWidgetBlock()), ('bankrate_cd_widget', BankrateCDRatesBlock()), ('bankrate_mortgage_widget', BankrateMortgageRatesBlock()), ('table_block', TableBlock(table_options=banking_table_options)), ]) comparisons = StreamField([ ('paragraph', blocks.RichTextBlock()), ('banking_comparison', BankingComparisonBlock()), ('bankrate_widget', BankrateWidgetBlock()), ('bankrate_cd_widget', BankrateCDRatesBlock()), ('bankrate_mortgage_widget', BankrateMortgageRatesBlock()), ('table_block', TableBlock(table_options=banking_table_options)), ], null=True, blank=True) parent_page_types = ['banking.BankingIndexPage'] class Meta: verbose_name = 'Banking and Savings Streamfield page' content_panels = Page.content_panels + [ StreamFieldPanel('body'), StreamFieldPanel('comparisons'), ]
def test_render_empty_table(self): """ An empty table should render okay. """ block = TableBlock() result = block.render(self.default_value) self.assertHTMLEqual(result, self.default_expected)
def test_searchable_content(self): value = {'first_row_is_table_header': False, 'first_col_is_header': False, 'data': [['Test 1', 'Test 2', 'Test 3'], [None, 'Bar', None], [None, 'Foo', None]]} block = TableBlock() content = block.get_searchable_content(value) self.assertEqual(content, ['Test 1', 'Test 2', 'Test 3', 'Bar', 'Foo', ])
def test_column_headers(self): """ Ensure that column headers are properly rendered. """ value = { 'first_row_is_table_header': False, 'first_col_is_header': True, 'data': [['Foo', 'Bar', 'Baz'], ['one', 'two', 'three'], ['four', 'five', 'six']] } expected = """ <table> <tbody> <tr><th scope="row">Foo</th><td>Bar</td><td>Baz</td></tr> <tr><th scope="row">one</th><td>two</td><td>three</td></tr> <tr><th scope="row">four</th><td>five</td><td>six</td></tr> </tbody> </table> """ block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_column_headers(self): """ Ensure that column headers are properly rendered. """ value = { "first_row_is_table_header": False, "first_col_is_header": True, "data": [ ["Foo", "Bar", "Baz"], ["one", "two", "three"], ["four", "five", "six"], ], } expected = """ <table> <tbody> <tr><th scope="row">Foo</th><td>Bar</td><td>Baz</td></tr> <tr><th scope="row">one</th><td>two</td><td>three</td></tr> <tr><th scope="row">four</th><td>five</td><td>six</td></tr> </tbody> </table> """ block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_table_block_alignment_render(self): """ Test a generic render with some cells aligned. """ value = { "first_row_is_table_header": True, "first_col_is_header": False, "cell": [ {"row": 0, "col": 1, "className": "htLeft"}, {"row": 1, "col": 1, "className": "htRight"}, ], "data": [ ["Test 1", "Test 2", "Test 3"], [None, None, None], [None, None, None], ], } block = TableBlock() result = block.render(value) expected = """ <table> <thead> <tr><th scope="col">Test 1</th><th scope="col" class="htLeft">Test 2</th><th scope="col">Test 3</th></tr> </thead> <tbody> <tr><td></td><td class="htRight"></td><td></td></tr> <tr><td></td><td></td><td></td></tr> </tbody> </table> """ self.assertHTMLEqual(result, expected) self.assertIn("Test 2", result)
class TwoColumnPage(Page): body = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('table', TableBlock()), ('image', ImageChooserBlock(icon="image")), ('embedded_video', EmbedBlock(icon="media")), ], blank=True) sidebar = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('table', TableBlock()), ('image', ImageChooserBlock(icon="image")), ('embedded_video', EmbedBlock(icon="media")), ], blank=True) content_panels = Page.content_panels + [ StreamFieldPanel('body'), ] sidebar_panel = [ StreamFieldPanel('sidebar'), ] edit_handler = TabbedInterface([ ObjectList(content_panels, heading='Main'), ObjectList(sidebar_panel, heading='Sidebar'), ObjectList(Page.promote_panels, heading='Promote'), ObjectList(Page.settings_panels, heading='Settings', classname="settings"), ]) template = "wagtail-2-col.html"
def test_row_and_column_headers(self): """ Test row and column headers at the same time. """ value = { "first_row_is_table_header": True, "first_col_is_header": True, "data": [ ["Foo", "Bar", "Baz"], ["one", "two", "three"], ["four", "five", "six"], ], } expected = """ <table> <thead> <tr><th scope="col">Foo</th><th scope="col">Bar</th><th scope="col">Baz</th></tr> </thead> <tbody> <tr><th scope="row">one</th><td>two</td><td>three</td></tr> <tr><th scope="row">four</th><td>five</td><td>six</td></tr> </tbody> </table> """ block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_table_block_caption_render(self): """ Test a generic render with caption. """ value = { "table_caption": "caption", "first_row_is_table_header": False, "first_col_is_header": False, "data": [ ["Test 1", "Test 2", "Test 3"], [None, None, None], [None, None, None], ], } block = TableBlock() result = block.render(value) expected = """ <table> <caption>caption</caption> <tbody> <tr><td>Test 1</td><td>Test 2</td><td>Test 3</td></tr> <tr><td></td><td></td><td></td></tr> <tr><td></td><td></td><td></td></tr> </tbody> </table> """ self.assertHTMLEqual(result, expected) self.assertIn("Test 2", result)
def test_table_block_alignment_render(self): """ Test a generic render with some cells aligned. """ value = {'first_row_is_table_header': True, 'first_col_is_header': False, 'cell': [{'row': 0, 'col': 1, 'className': 'htLeft'}, {'row': 1, 'col': 1, 'className': 'htRight'}], 'data': [['Test 1', 'Test 2', 'Test 3'], [None, None, None], [None, None, None]]} block = TableBlock() result = block.render(value) expected = """ <table> <thead> <tr><th>Test 1</th><th class="htLeft">Test 2</th><th>Test 3</th></tr> </thead> <tbody> <tr><td></td><td class="htRight"></td><td></td></tr> <tr><td></td><td></td><td></td></tr> </tbody> </table> """ self.assertHTMLEqual(result, expected) self.assertIn('Test 2', result)
def test_table_options_context_menu(self): """ Test how contextMenu is set to default. """ default_context_menu = list( DEFAULT_TABLE_OPTIONS["contextMenu"]) # create copy # confirm the default is correct table_options = TableBlock().table_options self.assertEqual(table_options["contextMenu"], default_context_menu) # confirm that when custom option is True, default is still used table_options_menu_true = TableBlock(table_options={ "contextMenu": True }).table_options self.assertEqual(table_options_menu_true["contextMenu"], default_context_menu) # confirm menu is removed if False is passed in table_options_menu_false = TableBlock(table_options={ "contextMenu": False }).table_options self.assertIs(table_options_menu_false["contextMenu"], False) # confirm if list passed in, it is used table_options_menu_list = TableBlock(table_options={ "contextMenu": ["undo", "redo"] }).table_options self.assertEqual(table_options_menu_list["contextMenu"], ["undo", "redo"]) # test if empty array passed in table_options_menu_list = TableBlock(table_options={ "contextMenu": [] }).table_options self.assertEqual(table_options_menu_list["contextMenu"], [])
class ArticlePage(common.GenericPage): class Meta: verbose_name = '呼吸疾病中心页面模板/一般文章页' template = 'ncrcrd/article.html' content = StreamField([ ('title', blocks.TitleBlock()), ('heading', blocks.HeadingBlock()), ('paragraph', blocks.ParagraphBlock()), ('image', blocks.ImageBlock()), ('person_card', blocks.PersonCardBlock()), ('organization_card', blocks.OrganizationCardBlock()), ('study_card', blocks.StudyCardBlock()), ('book_card', blocks.BookCardBlock()), ('table', TableBlock(label='表格')), ('table_corporation', TableBlock(label='协作单位表格', template='ncrcrd/blocks/table_corporation.html')), ('table_guide', TableBlock(label='指南规范表格', template='ncrcrd/blocks/table_guide.html')), ], verbose_name='正文') show_sidebar = models.BooleanField(default=True, verbose_name='显示侧边栏') show_breadcrumb = models.BooleanField(default=True, verbose_name='显示面包屑导航')
def test_row_headers(self): """ Ensure that row headers are properly rendered. """ value = { "first_row_is_table_header": True, "first_col_is_header": False, "data": [["Foo", "Bar", "Baz"], [None, None, None], [None, None, None]], } expected = """ <table> <thead> <tr><th scope="col">Foo</th><th scope="col">Bar</th><th scope="col">Baz</th></tr> </thead> <tbody> <tr><td></td><td></td><td></td></tr> <tr><td></td><td></td><td></td></tr> </tbody> </table> """ block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_do_not_render_html(self): """ Ensure that raw html doesn't render by default. """ value = { "first_row_is_table_header": False, "first_col_is_header": False, "data": [ ["<p><strong>Test</strong></p>", None, None], [None, None, None], [None, None, None], ], } expected = """ <table> <tbody> <tr><td><p><strong>Test</strong></p></td><td></td><td></td></tr> <tr><td></td><td></td><td></td></tr> <tr><td></td><td></td><td></td></tr> </tbody> </table> """ block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_row_headers(self): """ Ensure that row headers are properly rendered. """ value = { 'first_row_is_table_header': True, 'first_col_is_header': False, 'data': [['Foo', 'Bar', 'Baz'], [None, None, None], [None, None, None]] } expected = """ <table> <thead> <tr><th scope="col">Foo</th><th scope="col">Bar</th><th scope="col">Baz</th></tr> </thead> <tbody> <tr><td></td><td></td><td></td></tr> <tr><td></td><td></td><td></td></tr> </tbody> </table> """ block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_table_block_render(self): """ Test a generic render. """ value = { 'first_row_is_table_header': False, 'first_col_is_header': False, 'data': [['Test 1', 'Test 2', 'Test 3'], [None, None, None], [None, None, None]] } block = TableBlock() result = block.render(value) expected = """ <table> <tbody> <tr><td>Test 1</td><td>Test 2</td><td>Test 3</td></tr> <tr><td></td><td></td><td></td></tr> <tr><td></td><td></td><td></td></tr> </tbody> </table> """ self.assertHTMLEqual(result, expected) self.assertIn('Test 2', result)
def test_searchable_content(self): """ Test searchable content is created correctly. """ block = TableBlock() search_content = block.get_searchable_content(value=self.value) self.assertIn("Galactica", search_content) self.assertIn("Brenik", search_content)
def test_searchable_content(self): """ Test searchable content is created correctly. """ block = TableBlock() search_content = block.get_searchable_content(value=self.value) self.assertIn('Galactica', search_content) self.assertIn('Brenik', search_content)
class BlogPage(Page): body = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('small_table', TableBlock(table_options=small_table)), ('large_table', TableBlock(table_options=large_table)), ]) thumbnail = models.ForeignKey('wagtailimages.Image', null=True, blank=True, on_delete=models.SET_NULL, related_name='+') tags = ClusterTaggableManager(through=BlogPageTag, blank=True) @property def intro(self): intro_text = '' blocks = self.body.stream_data for block in blocks: if block['type'] == "paragraph": intro_text = "   " + remove_html_tags( block['value'][:300]) + "..." break return intro_text @property def npcs(self): npcs = [n.npc for n in self.npc_blog_relationship.all()] return npcs @property def quests(self): quests = [q.quest for q in self.quest_blog_relationship.all()] return quests content_panels = Page.content_panels + [ StreamFieldPanel('body'), MultiFieldPanel([ FieldPanel('thumbnail'), ], heading="Thumbnail", classname="collapsible collapsed"), MultiFieldPanel([ InlinePanel('npc_blog_relationship'), InlinePanel('quest_blog_relationship'), ], heading="Blog Extras -- NPC's and Quests", classname="collapsible collapsed"), FieldPanel('tags', heading="Tags") ] def save(self, *args, **kwargs): if self.id is None: self.show_in_menus = True self.show_in_menus_default = True return super().save()
class CreditCardReview(RoutablePageMixin, Page): ''' The review of a card. Includes both the summary and the full review. Inherits from Page because it generates a page on the site. ''' card = models.OneToOneField(CreditCard, related_name='review', on_delete=models.PROTECT) intro = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('blockquote', blocks.BlockQuoteBlock()), ('table_block', TableBlock()), ], blank=True) body = StreamField([ ('heading', blocks.CharBlock(classname="full title")), ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ('blockquote', blocks.BlockQuoteBlock()), ('table_block', TableBlock()), ]) pros = RichTextField(default='<ul><li></li></ul>') cons = RichTextField(default='<ul><li></li></ul>') tags = ClusterTaggableManager(through=CreditCardReviewTag, blank=True) content_panels = Page.content_panels + [ SnippetChooserPanel('card'), #FieldPanel('card', classname="full"), StreamFieldPanel('intro'), StreamFieldPanel('body'), FieldPanel('pros', classname="full"), FieldPanel('cons', classname="full"), FieldPanel('tags', classname="full"), ] @route(r'^card/$', name='conduit') def conduit(self, request): self.template = 'creditcards/credit_card_only.html' self.conduit = True return self.serve(request) @route(r'^$') #need default for revision view it seems def regular(self, request): return self.serve(request) @cached_property def conduit_url(self): subpage_url = self.reverse_subpage('conduit') if not self.url.endswith('/') and not subpage_url.startswith('/'): return self.url + '/' + subpage_url return self.url + subpage_url parent_page_types = ['creditcards.CreditCardIndex'] def disclosure(self): return disclosure_generator(self.card.processing_network)
def test_row_and_column_headers(self): """ Test row and column headers at the same time. """ value = {'first_row_is_table_header': True, 'first_col_is_header': True, 'data': [['Foo', 'Bar', 'Baz'], ['one', 'two', 'three'], ['four', 'five', 'six']]} expected = get_test_html_from_value(value) block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_column_headers(self): """ Ensure that column headers are properly rendered. """ value = {'first_row_is_table_header': False, 'first_col_is_header': True, 'data': [['Foo', 'Bar', 'Baz'], ['one', 'two', 'three'], ['four', 'five', 'six']]} expected = get_test_html_from_value(value) block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_empty_table_block_is_not_rendered(self): """ Test an empty table is not rendered. """ value = None block = TableBlock() result = block.render(value) expected = "" self.assertHTMLEqual(result, expected) self.assertNotIn("None", result)
def test_tableblock_render_form(self): """ Test the rendered form field generated by TableBlock. """ block = TableBlock() html = block.render_form(value=self.value) self.assertIn('<script>initTable', html) self.assertIn('<div class="field char_field widget-table_input">', html) # check that options render in the init function self.assertIn('"editor": "text"', html) self.assertIn('"autoColumnSize": false', html) self.assertIn('"stretchH": "all"', html)
def test_table_block_render(self): """ Test a generic render. """ value = {'first_row_is_table_header': False, 'first_col_is_header': False, 'data': [['Test 1', 'Test 2', 'Test 3'], [None, None, None], [None, None, None]]} block = TableBlock() result = block.render(value) expected = get_test_html_from_value(value) self.assertHTMLEqual(result, expected) self.assertIn('Test 2', result)
def test_value_for_and_from_form(self): """ Make sure we get back good json and make sure it translates back to python. """ value = {'first_row_is_table_header': False, 'first_col_is_header': False, 'data': [['Foo', 1, None], [3.5, 'Bar', 'Baz']]} block = TableBlock() expected_json = '{"first_row_is_table_header": false, "first_col_is_header": false, "data": [["Foo", 1, null], [3.5, "Bar", "Baz"]]}' returned_json = block.value_for_form(value) self.assertJSONEqual(expected_json, returned_json) self.assertEqual(block.value_from_form(returned_json), value)
def test_do_not_render_html(self): """ Ensure that raw html doesn't render by default. """ value = {'first_row_is_table_header': False, 'first_col_is_header': False, 'data': [['<p><strong>Test</strong></p>', None, None], [None, None, None], [None, None, None]]} expected = get_test_html_from_value(value) block = TableBlock() result = block.render(value) self.assertHTMLEqual(result, expected)
def test_table_options_others(self): """ Test simple options overrides get passed correctly. """ block_1_opts = TableBlock(table_options={'startRows': 5, 'startCols': 2}).table_options self.assertEqual(block_1_opts['startRows'], 5) self.assertEqual(block_1_opts['startCols'], 2) block_2_opts = TableBlock(table_options={'stretchH': 'none'}).table_options self.assertEqual(block_2_opts['stretchH'], 'none') # check value that is not part of the defaults block_3_opts = TableBlock(table_options={'allowEmpty': False}).table_options self.assertEqual(block_3_opts['allowEmpty'], False)
def test_is_html_renderer(self): """ Test that settings flow through correctly to the is_html_renderer method. """ # TableBlock with default table_options block1 = TableBlock() self.assertEqual(block1.is_html_renderer(), False) # TableBlock with altered table_options new_options = self.default_table_options.copy() new_options['renderer'] = 'html' block2 = TableBlock(table_options=new_options) self.assertEqual(block2.is_html_renderer(), True)
def test_default_table_options(self): """ Test options without any custom table_options provided. """ block = TableBlock() # check that default_table_options created correctly self.assertEqual(block.table_options, block.get_table_options()) # check that default_table_options used on self self.assertEqual(self.default_table_options, block.table_options) # check a few individual keys from DEFAULT_TABLE_OPTIONS self.assertEqual(DEFAULT_TABLE_OPTIONS['startRows'], block.table_options['startRows']) self.assertEqual(DEFAULT_TABLE_OPTIONS['colHeaders'], block.table_options['colHeaders']) self.assertEqual(DEFAULT_TABLE_OPTIONS['contextMenu'], block.table_options['contextMenu']) self.assertEqual(DEFAULT_TABLE_OPTIONS['editor'], block.table_options['editor']) self.assertEqual(DEFAULT_TABLE_OPTIONS['stretchH'], block.table_options['stretchH'])
def test_render_with_extra_context(self): """ Test that extra context variables passed in block.render are passed through to the template. """ block = TableBlock(template="tests/blocks/table_block_with_caption.html") value = {'first_row_is_table_header': False, 'first_col_is_header': False, 'data': [['Test 1', 'Test 2', 'Test 3'], [None, None, None], [None, None, None]]} result = block.render(value, context={ 'caption': "A fascinating table." }) self.assertIn("Test 1", result) self.assertIn("<div>A fascinating table.</div>", result)