backoffice: use a popup for csv/ods export (#52969)
This commit is contained in:
parent
6819a292c7
commit
71fb98d24a
|
@ -642,7 +642,9 @@ def test_backoffice_submission_agent_column(pub):
|
|||
assert resp.text.count('</th>') == 9 # seven columns
|
||||
assert resp.text.count('data-link') == 17 # 17 rows
|
||||
assert '>agent<' not in resp.text
|
||||
resp = resp.click('Export as CSV File')
|
||||
resp = resp.click('Export a Spreadsheet')
|
||||
resp.form['format'] = 'csv'
|
||||
resp = resp.form.submit('submit')
|
||||
assert len(resp.text.splitlines()) == 18 # 17 + header line
|
||||
assert ',agent,' not in resp.text
|
||||
|
||||
|
@ -655,7 +657,9 @@ def test_backoffice_submission_agent_column(pub):
|
|||
resp = resp.forms['listing-settings'].submit()
|
||||
assert resp.text.count('>agent<') == 17
|
||||
|
||||
resp = resp.click('Export as CSV File')
|
||||
resp = resp.click('Export a Spreadsheet')
|
||||
resp.form['format'] = 'csv'
|
||||
resp = resp.form.submit('submit')
|
||||
assert len(resp.text.splitlines()) == 18 # 17 + header line
|
||||
assert resp.text.count(',agent') == 17
|
||||
|
||||
|
@ -813,7 +817,9 @@ def test_backoffice_card_field_columns(pub):
|
|||
assert resp.text.count('<td>Yes</td>') == 17
|
||||
assert resp.text.count('<td>aa</td>') == 17
|
||||
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert resp_csv.text.splitlines()[1].endswith(',plop,2020-04-24,Yes,aa')
|
||||
|
||||
resp_map = resp.click('Plot on a Map')
|
||||
|
|
|
@ -97,7 +97,9 @@ def test_backoffice_csv(pub):
|
|||
|
||||
app = login(get_app(pub))
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp = resp.click('Export as CSV File')
|
||||
resp = resp.click('Export a Spreadsheet')
|
||||
resp.form['format'] = 'csv'
|
||||
resp = resp.form.submit('submit')
|
||||
assert resp.headers['content-type'].startswith('text/')
|
||||
assert len(resp.text.splitlines()) == 3 # 3 + header line
|
||||
assert len(resp.text.splitlines()[0].split(',')) == 7
|
||||
|
@ -106,7 +108,9 @@ def test_backoffice_csv(pub):
|
|||
formdef.fields[-1].display_locations = ['validation', 'summary', 'listings']
|
||||
formdef.store()
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp = resp.click('Export as CSV File')
|
||||
resp = resp.click('Export a Spreadsheet')
|
||||
resp.form['format'] = 'csv'
|
||||
resp = resp.form.submit('submit')
|
||||
assert len(resp.text.splitlines()[0].split(',')) == 9
|
||||
|
||||
# check item fields with datasources get two columns (id & text)
|
||||
|
@ -118,7 +122,9 @@ def test_backoffice_csv(pub):
|
|||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp.forms['listing-settings']['filter'] = 'all'
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert len(resp_csv.text.splitlines()) == 4
|
||||
|
||||
# test status filter
|
||||
|
@ -127,7 +133,9 @@ def test_backoffice_csv(pub):
|
|||
resp = resp.forms['listing-settings'].submit()
|
||||
resp.forms['listing-settings']['filter-2-value'] = 'baz'
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert len(resp_csv.text.splitlines()) == 2
|
||||
|
||||
# test criteria filters
|
||||
|
@ -135,21 +143,27 @@ def test_backoffice_csv(pub):
|
|||
resp = resp.forms['listing-settings'].submit()
|
||||
resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2015, 2, 1).strftime('%Y-%m-%d')
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert len(resp_csv.text.splitlines()) == 1
|
||||
|
||||
resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2014, 2, 1).strftime('%Y-%m-%d')
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp.forms['listing-settings']['filter-2-value'] = 'baz'
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert len(resp_csv.text.splitlines()) == 2
|
||||
assert 'Created' in resp_csv.text.splitlines()[0]
|
||||
|
||||
# test column selection
|
||||
resp.forms['listing-settings']['time'].checked = False
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert 'Created' not in resp_csv.text.splitlines()[0]
|
||||
|
||||
|
||||
|
@ -186,7 +200,9 @@ def test_backoffice_export_long_listings(pub, threshold):
|
|||
|
||||
app = login(get_app(pub))
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp = resp.click('Export as CSV File')
|
||||
resp = resp.click('Export a Spreadsheet')
|
||||
resp.form['format'] = 'csv'
|
||||
resp = resp.form.submit('submit')
|
||||
assert resp.location.startswith('http://example.net/backoffice/processing?job=')
|
||||
resp = resp.follow()
|
||||
assert 'completed' in resp.text
|
||||
|
@ -199,6 +215,8 @@ def test_backoffice_export_long_listings(pub, threshold):
|
|||
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp = resp.click('Export a Spreadsheet')
|
||||
resp.form['format'] = 'ods'
|
||||
resp = resp.form.submit('submit')
|
||||
assert resp.location.startswith('http://example.net/backoffice/processing?job=')
|
||||
job_id = urllib.parse.parse_qs(urllib.parse.urlparse(resp.location).query)['job'][0]
|
||||
resp = resp.follow()
|
||||
|
@ -238,13 +256,17 @@ def test_backoffice_csv_export_channel(pub):
|
|||
|
||||
app = login(get_app(pub))
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert 'Channel' not in resp_csv.text.splitlines()[0]
|
||||
|
||||
# add submission channel column
|
||||
resp.forms['listing-settings']['submission_channel'].checked = True
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Channel'
|
||||
assert resp_csv.text.splitlines()[1].split(',')[-1] == 'Web'
|
||||
|
||||
|
@ -270,13 +292,17 @@ def test_backoffice_csv_export_anonymised(pub):
|
|||
|
||||
app = login(get_app(pub))
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert resp_csv.text.splitlines()[0].split(',')[-1] != 'Anonymised'
|
||||
|
||||
# add anonymised column
|
||||
resp.forms['listing-settings']['anonymised'].checked = True
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Anonymised'
|
||||
assert resp_csv.text.splitlines()[1].split(',')[-1] == 'No'
|
||||
|
||||
|
@ -319,10 +345,14 @@ def test_backoffice_csv_export_block(pub):
|
|||
|
||||
app = login(get_app(pub))
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
resp.forms['listing-settings']['1'].checked = True
|
||||
resp = resp.forms['listing-settings'].submit()
|
||||
resp_csv = resp.click('Export as CSV File')
|
||||
resp_csv = resp.click('Export a Spreadsheet')
|
||||
resp_csv.form['format'] = 'csv'
|
||||
resp_csv = resp_csv.form.submit('submit')
|
||||
assert resp_csv.text.splitlines()[0].split(',')[-3:] == ['test - 1', 'test - 2', 'test - 3']
|
||||
assert resp_csv.text.splitlines()[1].split(',')[-3:] == ['XfooY', 'Xfoo2Y', '']
|
||||
|
||||
|
@ -424,6 +454,7 @@ def test_backoffice_ods(pub):
|
|||
app = login(get_app(pub))
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp = resp.click('Export a Spreadsheet')
|
||||
resp = resp.form.submit('submit')
|
||||
assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet'
|
||||
assert 'filename=form-title.ods' in resp.headers['content-disposition']
|
||||
assert resp.body[:2] == b'PK' # ods has a zip container
|
||||
|
@ -447,6 +478,7 @@ def test_backoffice_ods(pub):
|
|||
|
||||
resp = app.get('/backoffice/management/form-title/')
|
||||
resp = resp.click('Export a Spreadsheet')
|
||||
resp = resp.form.submit('submit')
|
||||
assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet'
|
||||
assert 'filename=form-title.ods' in resp.headers['content-disposition']
|
||||
assert resp.body[:2] == b'PK' # ods has a zip container
|
||||
|
|
|
@ -90,6 +90,7 @@ class CardPage(FormPage):
|
|||
'map',
|
||||
'geojson',
|
||||
'add',
|
||||
('export-spreadsheet', 'export_spreadsheet'),
|
||||
('save-view', 'save_view'),
|
||||
('delete-view', 'delete_view'),
|
||||
('import-csv', 'import_csv'),
|
||||
|
|
|
@ -29,6 +29,7 @@ from django.utils.encoding import force_text
|
|||
from quixote import get_publisher, get_request, get_response, get_session, redirect
|
||||
from quixote.directory import Directory
|
||||
from quixote.html import TemplateIO, htmlescape, htmltext
|
||||
from quixote.http_request import parse_query
|
||||
|
||||
from wcs.admin.settings import UserFieldsFormDef
|
||||
from wcs.api_utils import get_user_from_api_query_string
|
||||
|
@ -1075,6 +1076,7 @@ class FormPage(Directory):
|
|||
'export',
|
||||
'map',
|
||||
'geojson',
|
||||
('export-spreadsheet', 'export_spreadsheet'),
|
||||
('filter-options', 'filter_options'),
|
||||
('save-view', 'save_view'),
|
||||
('delete-view', 'delete_view'),
|
||||
|
@ -1153,11 +1155,13 @@ class FormPage(Directory):
|
|||
|
||||
def get_formdata_sidebar_actions(self, qs=''):
|
||||
r = TemplateIO(html=True)
|
||||
r += htmltext(' <li><a data-base-href="ods" href="ods%s">%s</a></li>') % (
|
||||
r += htmltext(
|
||||
' <li><a rel="popup" data-base-href="export-spreadsheet" data-autoclose-dialog="true" '
|
||||
'href="export-spreadsheet%s">%s</a></li>'
|
||||
) % (
|
||||
qs,
|
||||
_('Export a Spreadsheet'),
|
||||
)
|
||||
r += htmltext(' <li><a data-base-href="csv" href="csv%s">%s</a></li>') % (qs, _('Export as CSV File'))
|
||||
if self.formdef.geolocations:
|
||||
r += htmltext(' <li><a data-base-href="map" href="map%s">%s</a></li>') % (qs, _('Plot on a Map'))
|
||||
if 'stats' in self._q_exports:
|
||||
|
@ -2094,6 +2098,34 @@ class FormPage(Directory):
|
|||
job.store()
|
||||
return redirect(job.get_processing_url())
|
||||
|
||||
def export_spreadsheet(self):
|
||||
self.check_access()
|
||||
form = Form()
|
||||
form.add_hidden('query_string', get_request().get_query())
|
||||
form.add(
|
||||
RadiobuttonsWidget,
|
||||
'format',
|
||||
options=[('ods', _('OpenDocument (.ods)'), 'ods'), ('csv', _('Text (.csv)'), 'csv')],
|
||||
value='ods',
|
||||
required=True,
|
||||
extra_css_class='widget-inline-radio',
|
||||
)
|
||||
form.add_submit('submit', _('Export'))
|
||||
form.add_submit('cancel', _('Cancel'))
|
||||
|
||||
if not form.is_submitted() or form.has_errors():
|
||||
r = TemplateIO(html=True)
|
||||
r += htmltext('<h2>%s</h2>') % _('Spreadsheet Options')
|
||||
r += form.render()
|
||||
return r.getvalue()
|
||||
|
||||
get_request().form = parse_query(form.get_widget('query_string').parse() or '', 'utf-8')
|
||||
file_format = form.get_widget('format').parse()
|
||||
if file_format == 'csv':
|
||||
return self.csv()
|
||||
else:
|
||||
return self.ods()
|
||||
|
||||
def csv(self):
|
||||
self.check_access()
|
||||
fields = self.get_fields_from_query()
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
function displayPopup(event)
|
||||
{
|
||||
var $anchor = $(this);
|
||||
var autoclose_dialog = $anchor.data('autoclose-dialog');
|
||||
$.ajax({
|
||||
url: $(this).attr('href'),
|
||||
beforeSend: function(xhr) {xhr.setRequestHeader('X-Popup', 'true'); },
|
||||
|
@ -26,7 +28,11 @@ function displayPopup(event)
|
|||
if (name === 'cancel') {
|
||||
button.click = function() { $(this).dialog('close'); return false; };
|
||||
} else {
|
||||
button.click = function() { $(form).find('[name='+name+']').click(); return false; };
|
||||
button.click = function() {
|
||||
if (autoclose_dialog) $(this).dialog('close');
|
||||
$(form).find('[name='+name+']').click();
|
||||
return false;
|
||||
};
|
||||
}
|
||||
if (name === 'submit') {
|
||||
button['class'] = 'submit-button';
|
||||
|
|
Loading…
Reference in New Issue