settings: use a template to render import results (#53201)

This adds proper use of singular/plural forms and the display of card
categories.
This commit is contained in:
Frédéric Péters 2021-04-17 09:09:41 +02:00
parent 14c2aa6f9f
commit 6819a292c7
4 changed files with 64 additions and 34 deletions

View File

@ -130,6 +130,7 @@ def test_settings_export_import(pub):
carddef.name = 'bar'
carddef.store()
Category(name='baz').store()
Category(name='baz2').store()
CardDefCategory(name='foobar').store()
pub.role_class(name='qux').store()
NamedDataSource(name='quux').store()
@ -200,8 +201,10 @@ def test_settings_export_import(pub):
resp.form['file'] = Upload('export.wcs', zip_content.getvalue())
resp = resp.form.submit('submit')
assert 'Imported successfully' in resp.text
assert '1 forms' in resp.text
assert '1 cards' in resp.text
assert '1 form' in resp.text
assert '1 card' in resp.text
assert '2 categories' in resp.text
assert '1 card category' in resp.text
assert FormDef.count() == 1
assert FormDef.select()[0].url_name == 'foo'
assert CardDef.count() == 1

View File

@ -1165,8 +1165,10 @@ class SettingsDirectory(QommonSettingsDirectory):
r += form.render()
return r.getvalue()
else:
reason = None
try:
results = self.import_submit(form)
results['mail_templates'] = results['mail-templates']
except zipfile.BadZipfile:
results = None
reason = _('Not a valid export file')
@ -1177,38 +1179,10 @@ class SettingsDirectory(QommonSettingsDirectory):
msg += ' [%s]' % e.details
reason = _('Failed to import a workflow (%s); site import did not complete.') % (msg)
html_top('settings', title=_('Import'))
r = TemplateIO(html=True)
r += htmltext('<h2>%s</h2>') % _('Import')
if results:
r += htmltext('<p>%s</p>') % _('Imported successfully:')
r += htmltext('<ul>')
if results['formdefs']:
r += htmltext('<li>%d %s</li>') % (results['formdefs'], _('forms'))
if results['carddefs']:
r += htmltext('<li>%d %s</li>') % (results['carddefs'], _('cards'))
if results['blockdefs']:
r += htmltext('<li>%d %s</li>') % (results['blockdefs'], _('fields blocks'))
if results['workflows']:
r += htmltext('<li>%d %s</li>') % (results['workflows'], _('workflows'))
if results['roles']:
r += htmltext('<li>%d %s</li>') % (results['roles'], _('roles'))
if results['categories']:
r += htmltext('<li>%d %s</li>') % (results['categories'], _('categories'))
if results['settings']:
r += htmltext('<li>%s</li>') % _('Settings')
if results['datasources']:
r += htmltext('<li>%d %s</li>') % (results['datasources'], _('data sources'))
if results['mail-templates']:
r += htmltext('<li>%d %s</li>') % (results['mail-templates'], _('mail templates'))
if results['wscalls']:
r += htmltext('<li>%d %s</li>') % (results['wscalls'], _('webservice calls'))
if results['apiaccess']:
r += htmltext('<li>%d %s</li>') % (results['apiaccess'], _('API access'))
r += htmltext('</ul>')
else:
r += htmltext('<p>%s %s</p>') % (_('Error:'), reason)
r += htmltext('<a href=".">%s</a>') % _('Back')
return r.getvalue()
return template.QommonTemplateResponse(
templates=['wcs/backoffice/settings/import.html'],
context={'results': results, 'error': reason},
)
def import_submit(self, form):
return get_publisher().import_zip(form.get_widget('file').parse().fp)

View File

@ -168,6 +168,7 @@ class WcsPublisher(QommonPublisher):
'carddefs': 0,
'workflows': 0,
'categories': 0,
'carddef_categories': 0,
'roles': 0,
'settings': 0,
'datasources': 0,

View File

@ -0,0 +1,52 @@
{% extends "wcs/backoffice/base.html" %}
{% load i18n %}
{% block appbar-title %}{% trans "Import" %}{% endblock %}
{% block content %}
{% if error %}
<p>{% trans "Error:" %} {{ error }}</p>
{% else %}
<p>{% trans "Imported successfully:" %}</p>
<ul>
{% if results.formdefs %}
<li>{% blocktrans count counter=results.formdefs %}1 form{% plural %}{{ counter }} forms{% endblocktrans %}</li>
{% endif %}
{% if results.carddefs %}
<li>{% blocktrans count counter=results.carddefs %}1 card{% plural %}{{ counter }} cards{% endblocktrans %}</li>
{% endif %}
{% if results.blockdefs %}
<li>{% blocktrans count counter=results.blockdefs %}1 fields block{% plural %}{{ counter }} fields blocks{% endblocktrans %}</li>
{% endif %}
{% if results.workflows %}
<li>{% blocktrans count counter=results.workflows %}1 workflow{% plural %}{{ counter }} workflows{% endblocktrans %}</li>
{% endif %}
{% if results.roles %}
<li>{% blocktrans count counter=results.roles %}1 role{% plural %}{{ counter }} roles{% endblocktrans %}</li>
{% endif %}
{% if results.categories %}
<li>{% blocktrans count counter=results.categories %}1 category{% plural %}{{ counter }} categories{% endblocktrans %}</li>
{% endif %}
{% if results.carddef_categories %}
<li>{% blocktrans count counter=results.carddef_categories %}1 card category{% plural %}{{ counter }} card categories{% endblocktrans %}</li>
{% endif %}
{% if results.datasources %}
<li>{% blocktrans count counter=results.datasources %}1 data source{% plural %}{{ counter }} data sources{% endblocktrans %}</li>
{% endif %}
{% if results.mail_templates %}
<li>{% blocktrans count counter=results.mail_templates %}1 mail template{% plural %}{{ counter }} mail templates{% endblocktrans %}</li>
{% endif %}
{% if results.wscalls %}
<li>{% blocktrans count counter=results.wscalls %}1 webservice call{% plural %}{{ counter }} webservice calls{% endblocktrans %}</li>
{% endif %}
{% if results.apiaccess %}
<li>{% blocktrans count counter=results.apiaccess %}1 API access{% plural %}{{ counter }} API accesses{% endblocktrans %}</li>
{% endif %}
{% if results.settings %}
<li>{% trans "Settings" %}</li>
{% endif %}
</ul>
{% endif %}
<p><a href=".">{% trans "Back" %}</a></p>
{% endblock %}