Adding the Meta attribute `group` to Blocks.

The idea is that Streamfield-Blocks can be grouped in the CMS
by setting Meta's `group` attribute.
This gives a better overview, particularly with many blocks.
If the attribute remains unset, nothing changes.
pull/3531/head
Andreas Nüßlein 2016-10-20 16:56:39 +02:00 zatwierdzone przez Matt Westcott
rodzic 922260833b
commit bfd4a2ef4b
7 zmienionych plików z 51 dodań i 7 usunięć

Wyświetl plik

@ -31,6 +31,7 @@ Changelog
* Added support for custom date and datetime formats in input fields (Bojan Mihelac)
* Added support for custom Django REST framework serialiser fields in `Page.api_fields` using a new `APIField` class (Karl Hobley)
* Added `classname` argument to `StreamFieldPanel` (Christine Ho).
* Added `group` keyword argument to StreamField blocks for grouping related blocks together in the block menu (Andreas Nüßlein)
* Fix: Marked 'Date from' / 'Date to' strings in wagtailforms for translation (Vorlif)
* Fix: "File" field label on image edit form is now translated (Stein Strindhaug)
* Fix: Unreliable preview is now reliable by always opening in a new window (Kjartan Sverrisson)

Wyświetl plik

@ -41,7 +41,8 @@ Other features
* Page explorer menu item, search and summary panel are now hidden for users with no page permissions (Tim Heap)
* Added support for custom date and datetime formats in input fields (Bojan Mihelac)
* Added support for custom Django REST framework serialiser fields in ``Page.api_fields`` using a new ``APIField`` class (Karl Hobley)
* Added ``classname`` argument to ``StreamFieldPanel`` (Christine Ho).
* Added ``classname`` argument to ``StreamFieldPanel`` (Christine Ho)
* Added ``group`` keyword argument to StreamField blocks for grouping related blocks together in the block menu (Andreas Nüßlein)
Bug fixes

Wyświetl plik

@ -64,6 +64,9 @@ All block types accept the following optional keyword arguments:
``template``
The path to a Django template that will be used to render this block on the front end. See `Template rendering`_.
``group``
The group used to categorize this block, i.e. any blocks with the same group name will be shown together in the editor interface with the group name as a heading.
The basic block types provided by Wagtail are as follows:
CharBlock

Wyświetl plik

@ -1,10 +1,14 @@
<div class="stream-menu {% if state == 'closed' %}stream-menu-closed{% endif %}" id="{{ menu_id }}">
<a href="#" class="toggle"><span>Insert block</span></a>
<div class="stream-menu-inner">
<ul>
{% for child_block in child_blocks %}
<li><button type="button" class="button action-add-block-{{ child_block.name }} icon icon-{{ child_block.meta.icon }}"><span>{{ child_block.label }}</span></button></li>
{% endfor %}
</ul>
{% regroup child_blocks by meta.group as grouped_child_blocks %}
{% for child_blocks in grouped_child_blocks %}
{% if child_blocks.grouper %}<h3>{{child_blocks.grouper}}</h3>{% endif %}
<ul>
{% for child_block in child_blocks.list %}
<li><button type="button" class="button action-add-block-{{ child_block.name }} icon icon-{{ child_block.meta.icon }}"><span>{{ child_block.label }}</span></button></li>
{% endfor %}
</ul>
{% endfor %}
</div>
</div>

Wyświetl plik

@ -52,6 +52,7 @@ class Block(six.with_metaclass(BaseBlock, object)):
label = None
icon = "placeholder"
classname = None
group = ''
"""
Setting a 'dependencies' list serves as a shortcut for the common case where a complex block type

Wyświetl plik

@ -140,7 +140,7 @@ class BaseStreamBlock(Block):
return render_to_string('wagtailadmin/block_forms/stream.html', {
'prefix': prefix,
'list_members_html': list_members_html,
'child_blocks': self.child_blocks.values(),
'child_blocks': sorted(self.child_blocks.values(), key=lambda child_block: child_block.meta.group),
'header_menu_prefix': '%s-before' % prefix,
'block_errors': error_dict.get(NON_FIELD_ERRORS),
})

Wyświetl plik

@ -2231,6 +2231,40 @@ class TestStreamBlock(WagtailTestUtils, SimpleTestCase):
self.assertFalse(value1 == value3)
self.assertTrue(value1 != value3)
def test_render_considers_group_attribute(self):
"""If group attributes are set in Block Meta classes, render a <h3> for each different block"""
class Group1Block1(blocks.CharBlock):
class Meta:
group = 'group1'
class Group1Block2(blocks.CharBlock):
class Meta:
group = 'group1'
class Group2Block1(blocks.CharBlock):
class Meta:
group = 'group2'
class Group2Block2(blocks.CharBlock):
class Meta:
group = 'group2'
class NoGroupBlock(blocks.CharBlock):
pass
block = blocks.StreamBlock([
('b1', Group1Block1()),
('b2', Group1Block2()),
('b3', Group2Block1()),
('b4', Group2Block2()),
('ngb', NoGroupBlock()),
])
html = block.render_form('')
self.assertNotIn('<h3></h3>', block.render_form(''))
self.assertIn('<h3>group1</h3>', html)
self.assertIn('<h3>group2</h3>', html)
class TestPageChooserBlock(TestCase):
fixtures = ['test.json']