Inspecteur bloc de champs - ajout onglet formulaires + nombre de champs (#66387) #773

Merged
fpeters merged 2 commits from wip/66387-block-inspector into main 2023-10-27 08:34:19 +02:00
6 changed files with 143 additions and 0 deletions

View File

@ -466,7 +466,10 @@ def test_block_edit_field_warnings(pub):
def test_block_inspect(pub):
create_superuser(pub)
Workflow.wipe()
BlockDef.wipe()
FormDef.wipe()
block = BlockDef()
block.name = 'foobar'
block.fields = [
@ -475,10 +478,25 @@ def test_block_inspect(pub):
]
block.store()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = [fields.BlockField(id='0', label='first test', block_slug=block.slug)]
formdef.store()
formdef = FormDef()
formdef.name = 'form title 2'
formdef.fields = [
fields.BlockField(id='0', label='second test', block_slug=block.slug, max_items=3, remove_button=True)
]
formdef.store()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/blocks/%s/' % block.id)
resp = resp.click('Inspector')
assert resp.pyquery('#inspect-fields .inspect-field').length == 2
assert '2 fields.' in resp.text
assert resp.pyquery('table.block-usage tbody tr').length == 2
assert 'second test 3 yes' in resp.pyquery('table.block-usage tbody tr td').text()
assert 'first test 1 no' in resp.pyquery('table.block-usage tbody tr td').text()
def test_block_duplicate(pub):

View File

@ -1477,3 +1477,82 @@ def test_snapshot_broken_xml(pub):
assert pub.snapshot_class.count() == 2
snapshot2 = pub.snapshot_class.get_latest('formdef', formdef.id)
assert '>testform2<' in snapshot2.serialization
def test_block_snapshot_inspect_diff(pub):
create_superuser(pub)
create_role(pub)
BlockDef.wipe()
blockdef = BlockDef()
blockdef.name = 'testform'
blockdef.fields = []
blockdef.store()
assert pub.snapshot_class.count() == 1
snapshot1 = pub.snapshot_class.get_latest('block', blockdef.id)
blockdef.fields = [StringField(id=1, label='Test')]
blockdef.store()
assert pub.snapshot_class.count() == 2
snapshot2 = pub.snapshot_class.get_latest('block', blockdef.id)
blockdef.fields += [StringField(id=2, label='Test bis')]
blockdef.store()
assert pub.snapshot_class.count() == 3
snapshot3 = pub.snapshot_class.get_latest('block', blockdef.id)
app = login(get_app(pub))
resp = app.get('/backoffice/forms/blocks/%s/history/' % blockdef.id)
assert 'name="version1" value="%s"' % snapshot3.id in resp
assert 'name="version2" value="%s"' % snapshot3.id not in resp
assert 'name="version1" value="%s"' % snapshot2.id in resp
assert 'name="version2" value="%s"' % snapshot2.id in resp
assert 'name="version1" value="%s"' % snapshot1.id not in resp
assert 'name="version2" value="%s"' % snapshot1.id in resp
resp = app.get(
'/backoffice/forms/blocks/%s/history/compare?version1=%s&version2=%s'
% (blockdef.id, snapshot1.id, snapshot3.id)
)
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot1.id, snapshot1.id) in resp
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot3.id, snapshot3.id) in resp
assert resp.text.count('diff_sub') == 1
assert resp.text.count('diff_add') == 23
resp = resp.click('Compare inspect')
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot1.id, snapshot1.id) in resp
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot3.id, snapshot3.id) in resp
assert 'http://example.net/backoffice/forms/blocks/%s/1/' % blockdef.id in resp
assert 'http://example.net/backoffice/forms/blocks/%s/2/' % blockdef.id in resp
assert 'tab-usage' not in resp.text
resp = app.get(
'/backoffice/forms/blocks/%s/history/compare?version1=%s&version2=%s'
% (blockdef.id, snapshot3.id, snapshot1.id)
)
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot1.id, snapshot1.id) in resp
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot3.id, snapshot3.id) in resp
assert resp.text.count('diff_sub') == 1
assert resp.text.count('diff_add') == 23
resp = app.get(
'/backoffice/forms/blocks/%s/history/compare?version1=%s&version2=%s'
% (blockdef.id, snapshot2.id, snapshot3.id)
)
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot2.id, snapshot2.id) in resp
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot3.id, snapshot3.id) in resp
assert resp.text.count('diff_sub') == 0
assert resp.text.count('diff_add') == 11
blockdef.fields = [StringField(id=1, label='Test')]
blockdef.store()
assert pub.snapshot_class.count() == 4
snapshot4 = pub.snapshot_class.get_latest('block', blockdef.id)
resp = app.get(
'/backoffice/forms/blocks/%s/history/compare?version1=%s&version2=%s'
% (blockdef.id, snapshot3.id, snapshot4.id)
)
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot3.id, snapshot3.id) in resp
assert 'Snapshot <a href="%s/view/">%s</a>' % (snapshot4.id, snapshot4.id) in resp
assert resp.text.count('diff_sub') == 11
assert resp.text.count('diff_add') == 0

View File

@ -187,9 +187,11 @@ class SnapshotsDirectory(Directory):
return panel.html()
inspect1 = backoffice_class(component=None, instance=snapshot1.instance).render_inspect()
inspect1.context['snapshots_diff'] = True
inspect1 = template.render(inspect1.templates, inspect1.context)
d1 = pq(str(inspect1))
inspect2 = backoffice_class(component=None, instance=snapshot2.instance).render_inspect()
inspect2.context['snapshots_diff'] = True
inspect2 = template.render(inspect2.templates, inspect2.context)
d2 = pq(str(inspect2))
panels_attrs = [tab.attrib for tab in d1('[role="tabpanel"]')]

View File

@ -279,6 +279,15 @@ class BlockDef(StorableObject):
return blockdef
def get_usage_fields(self):
from wcs.formdef import get_formdefs_of_all_kinds
for formdef in get_formdefs_of_all_kinds():
for field in formdef.fields:
if field.key == 'block' and field.block_slug == self.slug:
field.formdef = formdef
yield field
def get_usage_formdefs(self):
from wcs.formdef import get_formdefs_of_all_kinds

View File

@ -3040,3 +3040,9 @@ fieldset.foldable {
}
}
}
table.block-usage {
.field-column {
width: 50%;
}
}

View File

@ -12,6 +12,9 @@
{% if deprecations.report_lines %}
<button role="tab" aria-selected="false" aria-controls="inspect-deprecations" id="tab-deprecations" tabindex="-1">{% trans "Deprecations" %}</button>
{% endif %}
{% if not snapshots_diff %}
<button role="tab" aria-selected="false" aria-controls="inspect-usage" id="tab-usage" tabindex="-1">{% trans "Usage" %}</button>
{% endif %}
</div>
<div class="pk-tabs--container">
@ -24,6 +27,9 @@
</div>
<div id="inspect-fields" role="tabpanel" tabindex="0" aria-labelledby="tab-fields" hidden>
<div class="pk-information page-field-counters"><p>
{% blocktrans count fields_count=blockdef.fields|count %}{{ fields_count }} field{% plural %}{{ fields_count }} fields{% endblocktrans %}.
</p></div>
{% for field in blockdef.fields %}
{% include "wcs/backoffice/includes/inspect-field.html" with path=blockdef.get_admin_url %}
{% endfor %}
@ -47,6 +53,29 @@
</div>
{% endif %}
{% if not snapshots_diff %}
<div id="inspect-usage" role="tabpanel" tabindex="0" aria-labelledby="tab-usage" hidden>
<table class="main block-usage">
<thead>
<tr>
<th class="field-column">{% trans "Field" %}</th>
<th>{% trans "Maximum number of items" %}</th>
<th>{% trans "Include remove button" %}</th>
</tr>
</thead>
<tbody>
{% for field in blockdef.get_usage_fields %}
<tr>
<td><a href="{{ field.formdef.get_admin_url }}">{{ field.label }}</a></td>
<td>{{ field.max_items|default:1 }}</td>
<td>{{ field.remove_button|yesno }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
</div>
</div>