0002-misc-split-backoffice-pages-tests.patch
tests/backoffice_pages/test_all.py | ||
---|---|---|
5 | 5 |
import re |
6 | 6 |
import time |
7 | 7 |
import random |
8 |
import xml.etree.ElementTree as ET |
|
9 | 8 |
import zipfile |
10 | 9 | |
11 | 10 |
import mock |
... | ... | |
18 | 17 | |
19 | 18 |
from django.utils.six import StringIO, BytesIO |
20 | 19 |
from django.utils.six.moves.urllib import parse as urllib |
21 |
from django.utils.six.moves.urllib import parse as urlparse |
|
22 | 20 | |
23 | 21 |
from webtest import Upload |
24 | 22 | |
25 | 23 |
from quixote import get_publisher |
26 | 24 |
from quixote.http_request import Upload as QuixoteUpload |
27 |
from wcs.qommon import ods |
|
28 | 25 |
from wcs.api_utils import sign_url |
29 | 26 |
from wcs.blocks import BlockDef |
30 | 27 |
from wcs.qommon.form import PicklableUpload |
... | ... | |
1363 | 1360 |
assert resp.text.count('>userB<') == 0 |
1364 | 1361 | |
1365 | 1362 | |
1366 |
def test_backoffice_csv(pub): |
|
1367 |
create_superuser(pub) |
|
1368 |
create_environment(pub) |
|
1369 |
app = login(get_app(pub)) |
|
1370 |
resp = app.get('/backoffice/management/form-title/') |
|
1371 |
resp = resp.click('Export as CSV File') |
|
1372 |
assert resp.headers['content-type'].startswith('text/') |
|
1373 |
assert len(resp.text.splitlines()) == 18 # 17 + header line |
|
1374 |
assert len(resp.text.splitlines()[0].split(',')) == 7 |
|
1375 | ||
1376 |
formdef = FormDef.get_by_urlname('form-title') |
|
1377 |
formdef.fields[-1].display_locations = ['validation', 'summary', 'listings'] |
|
1378 |
formdef.store() |
|
1379 |
resp = app.get('/backoffice/management/form-title/') |
|
1380 |
resp = resp.click('Export as CSV File') |
|
1381 |
assert len(resp.text.splitlines()[0].split(',')) == 9 |
|
1382 | ||
1383 |
# check item fields with datasources get two columns (id & text) |
|
1384 |
assert resp.text.splitlines()[0].split(',')[6] == '3rd field' |
|
1385 |
assert resp.text.splitlines()[0].split(',')[7] == '' # 3rd field, continue |
|
1386 |
assert resp.text.splitlines()[1].split(',')[6] == 'A' |
|
1387 |
assert resp.text.splitlines()[1].split(',')[7] == 'aa' |
|
1388 | ||
1389 |
resp = app.get('/backoffice/management/form-title/') |
|
1390 |
resp.forms['listing-settings']['filter'] = 'all' |
|
1391 |
resp = resp.forms['listing-settings'].submit() |
|
1392 |
resp_csv = resp.click('Export as CSV File') |
|
1393 |
assert len(resp_csv.text.splitlines()) == 51 |
|
1394 | ||
1395 |
# test status filter |
|
1396 |
resp.forms['listing-settings']['filter'] = 'pending' |
|
1397 |
resp.forms['listing-settings']['filter-2'].checked = True |
|
1398 |
resp = resp.forms['listing-settings'].submit() |
|
1399 |
resp.forms['listing-settings']['filter-2-value'] = 'baz' |
|
1400 |
resp = resp.forms['listing-settings'].submit() |
|
1401 |
resp_csv = resp.click('Export as CSV File') |
|
1402 |
assert len(resp_csv.text.splitlines()) == 9 |
|
1403 | ||
1404 |
# test criteria filters |
|
1405 |
resp.forms['listing-settings']['filter-start'].checked = True |
|
1406 |
resp = resp.forms['listing-settings'].submit() |
|
1407 |
resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2015, 2, 1).strftime('%Y-%m-%d') |
|
1408 |
resp = resp.forms['listing-settings'].submit() |
|
1409 |
resp_csv = resp.click('Export as CSV File') |
|
1410 |
assert len(resp_csv.text.splitlines()) == 1 |
|
1411 | ||
1412 |
resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2014, 2, 1).strftime('%Y-%m-%d') |
|
1413 |
resp = resp.forms['listing-settings'].submit() |
|
1414 |
resp.forms['listing-settings']['filter-2-value'] = 'baz' |
|
1415 |
resp = resp.forms['listing-settings'].submit() |
|
1416 |
resp_csv = resp.click('Export as CSV File') |
|
1417 |
assert len(resp_csv.text.splitlines()) == 9 |
|
1418 |
assert 'Created' in resp_csv.text.splitlines()[0] |
|
1419 | ||
1420 |
# test column selection |
|
1421 |
resp.forms['listing-settings']['time'].checked = False |
|
1422 |
resp = resp.forms['listing-settings'].submit() |
|
1423 |
resp_csv = resp.click('Export as CSV File') |
|
1424 |
assert 'Created' not in resp_csv.text.splitlines()[0] |
|
1425 | ||
1426 | ||
1427 |
def test_backoffice_export_long_listings(pub): |
|
1428 |
create_superuser(pub) |
|
1429 |
create_environment(pub) |
|
1430 |
formdef = FormDef.get_by_urlname('form-title') |
|
1431 |
for i in range(100): |
|
1432 |
formdata = formdef.data_class()() |
|
1433 |
formdata.just_created() |
|
1434 |
formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple() |
|
1435 |
formdata.data = {'1': 'BAZ BAZ %d' % i} |
|
1436 |
formdata.jump_status('new') |
|
1437 |
formdata.store() |
|
1438 | ||
1439 |
app = login(get_app(pub)) |
|
1440 |
resp = app.get('/backoffice/management/form-title/') |
|
1441 |
resp = resp.click('Export as CSV File') |
|
1442 |
assert resp.location.startswith('http://example.net/backoffice/management/form-title/export?job=') |
|
1443 |
resp = resp.follow() |
|
1444 |
assert 'completed' in resp.text |
|
1445 |
resp = resp.click('Download Export') |
|
1446 |
resp_lines = resp.text.splitlines() |
|
1447 |
assert resp_lines[0] == 'Number,Created,Last Modified,User Label,1st field,2nd field,Status' |
|
1448 |
assert len(resp_lines) == 118 |
|
1449 |
assert resp_lines[1].split(',')[1].startswith( |
|
1450 |
time.strftime('%Y-%m-%d', formdata.receipt_time)) |
|
1451 |
assert resp_lines[1].split(',')[2].startswith( |
|
1452 |
time.strftime('%Y-%m-%d', formdata.last_update_time)) |
|
1453 | ||
1454 |
resp = app.get('/backoffice/management/form-title/') |
|
1455 |
resp = resp.click('Export a Spreadsheet') |
|
1456 |
assert resp.location.startswith('http://example.net/backoffice/management/form-title/export?job=') |
|
1457 |
job_id = urlparse.parse_qs(urlparse.urlparse(resp.location).query)['job'][0] |
|
1458 |
resp = resp.follow() |
|
1459 |
assert 'completed' in resp.text |
|
1460 |
resp = resp.click('Download Export') |
|
1461 |
assert resp.content_type == 'application/vnd.oasis.opendocument.spreadsheet' |
|
1462 | ||
1463 |
# check afterjob ajax call |
|
1464 |
status_resp = app.get('/afterjobs/' + job_id) |
|
1465 |
assert status_resp.text == 'completed|completed' |
|
1466 | ||
1467 |
# check error handling |
|
1468 |
app.get('/afterjobs/whatever', status=404) |
|
1469 | ||
1470 | ||
1471 |
def test_backoffice_csv_export_channel(pub): |
|
1472 |
if not pub.site_options.has_section('variables'): |
|
1473 |
pub.site_options.add_section('variables') |
|
1474 |
pub.site_options.set('variables', 'welco_url', 'xxx') |
|
1475 |
fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') |
|
1476 |
pub.site_options.write(fd) |
|
1477 |
fd.close() |
|
1478 | ||
1479 |
create_superuser(pub) |
|
1480 |
create_environment(pub) |
|
1481 |
app = login(get_app(pub)) |
|
1482 |
resp = app.get('/backoffice/management/form-title/') |
|
1483 |
resp_csv = resp.click('Export as CSV File') |
|
1484 |
assert 'Channel' not in resp_csv.text.splitlines()[0] |
|
1485 | ||
1486 |
# add submission channel column |
|
1487 |
resp.forms['listing-settings']['submission_channel'].checked = True |
|
1488 |
resp = resp.forms['listing-settings'].submit() |
|
1489 |
resp_csv = resp.click('Export as CSV File') |
|
1490 |
assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Channel' |
|
1491 |
assert resp_csv.text.splitlines()[1].split(',')[-1] == 'Web' |
|
1492 | ||
1493 | ||
1494 |
def test_backoffice_csv_export_anonymised(pub): |
|
1495 |
fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') |
|
1496 |
pub.site_options.write(fd) |
|
1497 |
fd.close() |
|
1498 | ||
1499 |
create_superuser(pub) |
|
1500 |
create_environment(pub) |
|
1501 |
app = login(get_app(pub)) |
|
1502 |
resp = app.get('/backoffice/management/form-title/') |
|
1503 |
resp_csv = resp.click('Export as CSV File') |
|
1504 |
assert resp_csv.text.splitlines()[0].split(',')[-1] != 'Anonymised' |
|
1505 | ||
1506 |
# add anonymised column |
|
1507 |
resp.forms['listing-settings']['anonymised'].checked = True |
|
1508 |
resp = resp.forms['listing-settings'].submit() |
|
1509 |
resp_csv = resp.click('Export as CSV File') |
|
1510 |
assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Anonymised' |
|
1511 |
assert resp_csv.text.splitlines()[1].split(',')[-1] == 'No' |
|
1512 | ||
1513 | ||
1514 |
def test_backoffice_csv_export_block(pub): |
|
1515 |
create_superuser(pub) |
|
1516 |
create_environment(pub) |
|
1517 | ||
1518 |
block = BlockDef() |
|
1519 |
block.name = 'foobar' |
|
1520 |
block.fields = [ |
|
1521 |
fields.StringField(id='123', required=True, label='Test', type='string', varname='foo'), |
|
1522 |
fields.StringField(id='234', required=True, label='Test2', type='string', varname='bar'), |
|
1523 |
] |
|
1524 |
block.digest_template = 'X{{foobar_var_foo}}Y' |
|
1525 |
block.store() |
|
1526 | ||
1527 |
formdef = FormDef.get_by_urlname('form-title') |
|
1528 |
formdef.fields = [] |
|
1529 |
formdef.store() # make sure sql columns are removed |
|
1530 | ||
1531 |
formdef.fields = [ |
|
1532 |
fields.BlockField(id='1', label='test', type='block:foobar', max_items=3), |
|
1533 |
] |
|
1534 |
formdef.store() |
|
1535 | ||
1536 |
formdef.data_class().wipe() |
|
1537 |
formdata = formdef.data_class()() |
|
1538 |
formdata.data = {} |
|
1539 |
formdata.data['1'] = { |
|
1540 |
'data': [ |
|
1541 |
{'123': 'foo', '234': 'bar'}, |
|
1542 |
{'123': 'foo2', '234': 'bar2'}, |
|
1543 |
], |
|
1544 |
'schema': {'123': 'string', '234': 'string'}, |
|
1545 |
} |
|
1546 |
formdata.just_created() |
|
1547 |
formdata.jump_status('new') |
|
1548 |
formdata.store() |
|
1549 | ||
1550 |
app = login(get_app(pub)) |
|
1551 |
resp = app.get('/backoffice/management/form-title/') |
|
1552 |
resp_csv = resp.click('Export as CSV File') |
|
1553 |
resp.forms['listing-settings']['1'].checked = True |
|
1554 |
resp = resp.forms['listing-settings'].submit() |
|
1555 |
resp_csv = resp.click('Export as CSV File') |
|
1556 |
assert resp_csv.text.splitlines()[0].split(',')[-3:] == ['test - 1', 'test - 2', 'test - 3'] |
|
1557 |
assert resp_csv.text.splitlines()[1].split(',')[-3:] == ['XfooY', 'Xfoo2Y', ''] |
|
1558 | ||
1559 | ||
1560 |
def test_backoffice_ods(pub): |
|
1561 |
create_superuser(pub) |
|
1562 |
create_environment(pub) |
|
1563 |
app = login(get_app(pub)) |
|
1564 |
resp = app.get('/backoffice/management/form-title/') |
|
1565 |
resp = resp.click('Export a Spreadsheet') |
|
1566 |
assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet' |
|
1567 |
assert 'filename=form-title.ods' in resp.headers['content-disposition'] |
|
1568 |
assert resp.body[:2] == b'PK' # ods has a zip container |
|
1569 | ||
1570 |
formdef = FormDef.get_by_urlname('form-title') |
|
1571 |
formdef.fields.append(fields.FileField(id='4', label='file field', type='file', |
|
1572 |
display_locations=['validation', 'summary', 'listings'])) |
|
1573 |
formdef.fields.append(fields.DateField(id='5', label='date field', type='date', |
|
1574 |
display_locations=['validation', 'summary', 'listings'])) |
|
1575 |
formdef.fields.append(fields.StringField(id='6', label='number field', type='string', |
|
1576 |
display_locations=['validation', 'summary', 'listings'])) |
|
1577 |
formdef.fields.append(fields.StringField(id='7', label='phone field', type='string', |
|
1578 |
display_locations=['validation', 'summary', 'listings'])) |
|
1579 |
formdef.fields.append(fields.DateField(id='8', label='very old field', type='date', |
|
1580 |
display_locations=['validation', 'summary', 'listings'])) |
|
1581 |
formdef.fields.append(fields.StringField(id='9', label='string field', type='string', |
|
1582 |
display_locations=['validation', 'summary', 'listings'])) |
|
1583 |
formdef.store() |
|
1584 | ||
1585 |
formdata = formdef.data_class().select(lambda x: x.status == 'wf-new')[0] |
|
1586 |
formdata.data['4'] = PicklableUpload('/foo/bar', content_type='text/plain') |
|
1587 |
formdata.data['4'].receive([b'hello world']) |
|
1588 |
formdata.data['5'] = time.strptime('2015-05-12', '%Y-%m-%d') |
|
1589 |
formdata.data['6'] = '12345' |
|
1590 |
formdata.data['7'] = '0102030405' |
|
1591 |
formdata.data['8'] = time.strptime('1871-03-18', '%Y-%m-%d') |
|
1592 |
formdata.data['9'] = 'plop\npl\x1dop' # with control characters |
|
1593 |
formdata.store() |
|
1594 | ||
1595 |
resp = app.get('/backoffice/management/form-title/') |
|
1596 |
resp = resp.click('Export a Spreadsheet') |
|
1597 |
assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet' |
|
1598 |
assert 'filename=form-title.ods' in resp.headers['content-disposition'] |
|
1599 |
assert resp.body[:2] == b'PK' # ods has a zip container |
|
1600 | ||
1601 |
zipf = zipfile.ZipFile(BytesIO(resp.body)) |
|
1602 |
ods_sheet = ET.parse(zipf.open('content.xml')) |
|
1603 |
# check the ods contains a link to the document |
|
1604 |
elem = ods_sheet.findall('.//{%s}a' % ods.NS['text'])[0] |
|
1605 |
assert elem.attrib['{%s}href' % ods.NS['xlink']] == 'http://example.net/backoffice/management/form-title/%s/files/4/bar' % formdata.id |
|
1606 |
resp = app.get(elem.attrib['{%s}href' % ods.NS['xlink']]) |
|
1607 |
assert resp.text == 'hello world' |
|
1608 | ||
1609 |
all_texts = [x.text for x in ods_sheet.findall('.//{%s}table-row//{%s}p' % (ods.NS['table'], ods.NS['text']))] |
|
1610 |
created_column = all_texts.index('Created') |
|
1611 |
date_column = all_texts.index('date field') |
|
1612 |
number_column = all_texts.index('number field') |
|
1613 |
phone_column = all_texts.index('phone field') |
|
1614 |
old_column = all_texts.index('very old field') |
|
1615 |
string_column = all_texts.index('string field') |
|
1616 | ||
1617 |
for row in ods_sheet.findall('.//{%s}table-row' % ods.NS['table']): |
|
1618 |
if row.findall('.//{%s}table-cell/{%s}p' % ( |
|
1619 |
ods.NS['table'], ods.NS['text']))[0].text == formdata.get_display_id(): |
|
1620 |
break |
|
1621 |
else: |
|
1622 |
assert False, 'failed to find data row' |
|
1623 | ||
1624 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[created_column].attrib[ |
|
1625 |
'{%s}value-type' % ods.NS['office']] == 'date' |
|
1626 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[created_column].attrib[ |
|
1627 |
'{%s}style-name' % ods.NS['table']] == 'DateTime' |
|
1628 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[date_column].attrib[ |
|
1629 |
'{%s}value-type' % ods.NS['office']] == 'date' |
|
1630 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[date_column].attrib[ |
|
1631 |
'{%s}style-name' % ods.NS['table']] == 'Date' |
|
1632 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[number_column].attrib[ |
|
1633 |
'{%s}value-type' % ods.NS['office']] == 'float' |
|
1634 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[number_column].attrib[ |
|
1635 |
'{%s}value' % ods.NS['office']] == '12345' |
|
1636 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[phone_column].attrib[ |
|
1637 |
'{%s}value-type' % ods.NS['office']] == 'string' |
|
1638 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[old_column].attrib[ |
|
1639 |
'{%s}value-type' % ods.NS['office']] == 'date' |
|
1640 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[old_column].attrib[ |
|
1641 |
'{%s}date-value' % ods.NS['office']] == '1871-03-18' |
|
1642 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[ |
|
1643 |
string_column].find('{%s}p' % ods.NS['text']).text == 'plop\nplop' |
|
1644 | ||
1645 | ||
1646 |
@pytest.mark.skipif('xlwt is None') |
|
1647 |
def test_backoffice_xls(pub): |
|
1648 |
create_superuser(pub) |
|
1649 |
create_environment(pub) |
|
1650 |
app = login(get_app(pub)) |
|
1651 |
resp = app.get('/backoffice/management/form-title/') |
|
1652 |
assert not 'Excel Export' in resp.text |
|
1653 | ||
1654 |
if not pub.site_options.has_section('options'): |
|
1655 |
pub.site_options.add_section('options') |
|
1656 |
pub.site_options.set('options', 'legacy-excel-export', 'true') |
|
1657 |
fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') |
|
1658 |
pub.site_options.write(fd) |
|
1659 |
fd.close() |
|
1660 | ||
1661 |
resp = app.get('/backoffice/management/form-title/') |
|
1662 |
resp = resp.click('Excel Export') |
|
1663 |
assert resp.headers['content-type'].startswith('application/vnd.ms-excel') |
|
1664 | ||
1665 | ||
1666 | 1363 |
def test_backoffice_statistics(pub): |
1667 | 1364 |
create_superuser(pub) |
1668 | 1365 |
create_environment(pub) |
tests/backoffice_pages/test_export.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
import datetime |
|
3 |
import os |
|
4 |
import time |
|
5 |
import xml.etree.ElementTree as ET |
|
6 |
import zipfile |
|
7 | ||
8 |
import pytest |
|
9 | ||
10 |
try: |
|
11 |
import xlwt |
|
12 |
except ImportError: |
|
13 |
xlwt = None |
|
14 | ||
15 |
from django.utils.six import BytesIO |
|
16 |
from django.utils.six.moves.urllib import parse as urlparse |
|
17 | ||
18 |
from wcs.qommon import ods |
|
19 |
from wcs.blocks import BlockDef |
|
20 |
from wcs.qommon.form import PicklableUpload |
|
21 |
from wcs.qommon.http_request import HTTPRequest |
|
22 |
from wcs.formdef import FormDef |
|
23 |
from wcs import fields |
|
24 | ||
25 |
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub |
|
26 |
from test_all import create_superuser, create_environment |
|
27 | ||
28 | ||
29 |
def pytest_generate_tests(metafunc): |
|
30 |
if 'pub' in metafunc.fixturenames: |
|
31 |
metafunc.parametrize('pub', ['pickle', 'sql', 'pickle-templates'], indirect=True) |
|
32 | ||
33 | ||
34 |
@pytest.fixture |
|
35 |
def pub(request, emails): |
|
36 |
pub = create_temporary_pub( |
|
37 |
sql_mode=bool('sql' in request.param), |
|
38 |
templates_mode=bool('templates' in request.param) |
|
39 |
) |
|
40 | ||
41 |
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'}) |
|
42 |
pub.set_app_dir(req) |
|
43 |
pub.cfg['identification'] = {'methods': ['password']} |
|
44 |
pub.cfg['language'] = {'language': 'en'} |
|
45 |
pub.write_cfg() |
|
46 |
fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') |
|
47 |
fd.write(''' |
|
48 |
[api-secrets] |
|
49 |
coucou = 1234 |
|
50 |
''') |
|
51 |
fd.close() |
|
52 | ||
53 |
return pub |
|
54 | ||
55 | ||
56 |
def teardown_module(module): |
|
57 |
clean_temporary_pub() |
|
58 | ||
59 | ||
60 |
def test_backoffice_csv(pub): |
|
61 |
create_superuser(pub) |
|
62 |
create_environment(pub) |
|
63 |
app = login(get_app(pub)) |
|
64 |
resp = app.get('/backoffice/management/form-title/') |
|
65 |
resp = resp.click('Export as CSV File') |
|
66 |
assert resp.headers['content-type'].startswith('text/') |
|
67 |
assert len(resp.text.splitlines()) == 18 # 17 + header line |
|
68 |
assert len(resp.text.splitlines()[0].split(',')) == 7 |
|
69 | ||
70 |
formdef = FormDef.get_by_urlname('form-title') |
|
71 |
formdef.fields[-1].display_locations = ['validation', 'summary', 'listings'] |
|
72 |
formdef.store() |
|
73 |
resp = app.get('/backoffice/management/form-title/') |
|
74 |
resp = resp.click('Export as CSV File') |
|
75 |
assert len(resp.text.splitlines()[0].split(',')) == 9 |
|
76 | ||
77 |
# check item fields with datasources get two columns (id & text) |
|
78 |
assert resp.text.splitlines()[0].split(',')[6] == '3rd field' |
|
79 |
assert resp.text.splitlines()[0].split(',')[7] == '' # 3rd field, continue |
|
80 |
assert resp.text.splitlines()[1].split(',')[6] == 'A' |
|
81 |
assert resp.text.splitlines()[1].split(',')[7] == 'aa' |
|
82 | ||
83 |
resp = app.get('/backoffice/management/form-title/') |
|
84 |
resp.forms['listing-settings']['filter'] = 'all' |
|
85 |
resp = resp.forms['listing-settings'].submit() |
|
86 |
resp_csv = resp.click('Export as CSV File') |
|
87 |
assert len(resp_csv.text.splitlines()) == 51 |
|
88 | ||
89 |
# test status filter |
|
90 |
resp.forms['listing-settings']['filter'] = 'pending' |
|
91 |
resp.forms['listing-settings']['filter-2'].checked = True |
|
92 |
resp = resp.forms['listing-settings'].submit() |
|
93 |
resp.forms['listing-settings']['filter-2-value'] = 'baz' |
|
94 |
resp = resp.forms['listing-settings'].submit() |
|
95 |
resp_csv = resp.click('Export as CSV File') |
|
96 |
assert len(resp_csv.text.splitlines()) == 9 |
|
97 | ||
98 |
# test criteria filters |
|
99 |
resp.forms['listing-settings']['filter-start'].checked = True |
|
100 |
resp = resp.forms['listing-settings'].submit() |
|
101 |
resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2015, 2, 1).strftime('%Y-%m-%d') |
|
102 |
resp = resp.forms['listing-settings'].submit() |
|
103 |
resp_csv = resp.click('Export as CSV File') |
|
104 |
assert len(resp_csv.text.splitlines()) == 1 |
|
105 | ||
106 |
resp.forms['listing-settings']['filter-start-value'] = datetime.datetime(2014, 2, 1).strftime('%Y-%m-%d') |
|
107 |
resp = resp.forms['listing-settings'].submit() |
|
108 |
resp.forms['listing-settings']['filter-2-value'] = 'baz' |
|
109 |
resp = resp.forms['listing-settings'].submit() |
|
110 |
resp_csv = resp.click('Export as CSV File') |
|
111 |
assert len(resp_csv.text.splitlines()) == 9 |
|
112 |
assert 'Created' in resp_csv.text.splitlines()[0] |
|
113 | ||
114 |
# test column selection |
|
115 |
resp.forms['listing-settings']['time'].checked = False |
|
116 |
resp = resp.forms['listing-settings'].submit() |
|
117 |
resp_csv = resp.click('Export as CSV File') |
|
118 |
assert 'Created' not in resp_csv.text.splitlines()[0] |
|
119 | ||
120 | ||
121 |
def test_backoffice_export_long_listings(pub): |
|
122 |
create_superuser(pub) |
|
123 |
create_environment(pub) |
|
124 |
formdef = FormDef.get_by_urlname('form-title') |
|
125 |
for i in range(100): |
|
126 |
formdata = formdef.data_class()() |
|
127 |
formdata.just_created() |
|
128 |
formdata.receipt_time = datetime.datetime(2015, 1, 1).timetuple() |
|
129 |
formdata.data = {'1': 'BAZ BAZ %d' % i} |
|
130 |
formdata.jump_status('new') |
|
131 |
formdata.store() |
|
132 | ||
133 |
app = login(get_app(pub)) |
|
134 |
resp = app.get('/backoffice/management/form-title/') |
|
135 |
resp = resp.click('Export as CSV File') |
|
136 |
assert resp.location.startswith('http://example.net/backoffice/management/form-title/export?job=') |
|
137 |
resp = resp.follow() |
|
138 |
assert 'completed' in resp.text |
|
139 |
resp = resp.click('Download Export') |
|
140 |
resp_lines = resp.text.splitlines() |
|
141 |
assert resp_lines[0] == 'Number,Created,Last Modified,User Label,1st field,2nd field,Status' |
|
142 |
assert len(resp_lines) == 118 |
|
143 |
assert resp_lines[1].split(',')[1].startswith( |
|
144 |
time.strftime('%Y-%m-%d', formdata.receipt_time)) |
|
145 |
assert resp_lines[1].split(',')[2].startswith( |
|
146 |
time.strftime('%Y-%m-%d', formdata.last_update_time)) |
|
147 | ||
148 |
resp = app.get('/backoffice/management/form-title/') |
|
149 |
resp = resp.click('Export a Spreadsheet') |
|
150 |
assert resp.location.startswith('http://example.net/backoffice/management/form-title/export?job=') |
|
151 |
job_id = urlparse.parse_qs(urlparse.urlparse(resp.location).query)['job'][0] |
|
152 |
resp = resp.follow() |
|
153 |
assert 'completed' in resp.text |
|
154 |
resp = resp.click('Download Export') |
|
155 |
assert resp.content_type == 'application/vnd.oasis.opendocument.spreadsheet' |
|
156 | ||
157 |
# check afterjob ajax call |
|
158 |
status_resp = app.get('/afterjobs/' + job_id) |
|
159 |
assert status_resp.text == 'completed|completed' |
|
160 | ||
161 |
# check error handling |
|
162 |
app.get('/afterjobs/whatever', status=404) |
|
163 | ||
164 | ||
165 |
def test_backoffice_csv_export_channel(pub): |
|
166 |
if not pub.site_options.has_section('variables'): |
|
167 |
pub.site_options.add_section('variables') |
|
168 |
pub.site_options.set('variables', 'welco_url', 'xxx') |
|
169 |
fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') |
|
170 |
pub.site_options.write(fd) |
|
171 |
fd.close() |
|
172 | ||
173 |
create_superuser(pub) |
|
174 |
create_environment(pub) |
|
175 |
app = login(get_app(pub)) |
|
176 |
resp = app.get('/backoffice/management/form-title/') |
|
177 |
resp_csv = resp.click('Export as CSV File') |
|
178 |
assert 'Channel' not in resp_csv.text.splitlines()[0] |
|
179 | ||
180 |
# add submission channel column |
|
181 |
resp.forms['listing-settings']['submission_channel'].checked = True |
|
182 |
resp = resp.forms['listing-settings'].submit() |
|
183 |
resp_csv = resp.click('Export as CSV File') |
|
184 |
assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Channel' |
|
185 |
assert resp_csv.text.splitlines()[1].split(',')[-1] == 'Web' |
|
186 | ||
187 | ||
188 |
def test_backoffice_csv_export_anonymised(pub): |
|
189 |
fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') |
|
190 |
pub.site_options.write(fd) |
|
191 |
fd.close() |
|
192 | ||
193 |
create_superuser(pub) |
|
194 |
create_environment(pub) |
|
195 |
app = login(get_app(pub)) |
|
196 |
resp = app.get('/backoffice/management/form-title/') |
|
197 |
resp_csv = resp.click('Export as CSV File') |
|
198 |
assert resp_csv.text.splitlines()[0].split(',')[-1] != 'Anonymised' |
|
199 | ||
200 |
# add anonymised column |
|
201 |
resp.forms['listing-settings']['anonymised'].checked = True |
|
202 |
resp = resp.forms['listing-settings'].submit() |
|
203 |
resp_csv = resp.click('Export as CSV File') |
|
204 |
assert resp_csv.text.splitlines()[0].split(',')[-1] == 'Anonymised' |
|
205 |
assert resp_csv.text.splitlines()[1].split(',')[-1] == 'No' |
|
206 | ||
207 | ||
208 |
def test_backoffice_csv_export_block(pub): |
|
209 |
create_superuser(pub) |
|
210 |
create_environment(pub) |
|
211 | ||
212 |
block = BlockDef() |
|
213 |
block.name = 'foobar' |
|
214 |
block.fields = [ |
|
215 |
fields.StringField(id='123', required=True, label='Test', type='string', varname='foo'), |
|
216 |
fields.StringField(id='234', required=True, label='Test2', type='string', varname='bar'), |
|
217 |
] |
|
218 |
block.digest_template = 'X{{foobar_var_foo}}Y' |
|
219 |
block.store() |
|
220 | ||
221 |
formdef = FormDef.get_by_urlname('form-title') |
|
222 |
formdef.fields = [] |
|
223 |
formdef.store() # make sure sql columns are removed |
|
224 | ||
225 |
formdef.fields = [ |
|
226 |
fields.BlockField(id='1', label='test', type='block:foobar', max_items=3), |
|
227 |
] |
|
228 |
formdef.store() |
|
229 | ||
230 |
formdef.data_class().wipe() |
|
231 |
formdata = formdef.data_class()() |
|
232 |
formdata.data = {} |
|
233 |
formdata.data['1'] = { |
|
234 |
'data': [ |
|
235 |
{'123': 'foo', '234': 'bar'}, |
|
236 |
{'123': 'foo2', '234': 'bar2'}, |
|
237 |
], |
|
238 |
'schema': {'123': 'string', '234': 'string'}, |
|
239 |
} |
|
240 |
formdata.just_created() |
|
241 |
formdata.jump_status('new') |
|
242 |
formdata.store() |
|
243 | ||
244 |
app = login(get_app(pub)) |
|
245 |
resp = app.get('/backoffice/management/form-title/') |
|
246 |
resp_csv = resp.click('Export as CSV File') |
|
247 |
resp.forms['listing-settings']['1'].checked = True |
|
248 |
resp = resp.forms['listing-settings'].submit() |
|
249 |
resp_csv = resp.click('Export as CSV File') |
|
250 |
assert resp_csv.text.splitlines()[0].split(',')[-3:] == ['test - 1', 'test - 2', 'test - 3'] |
|
251 |
assert resp_csv.text.splitlines()[1].split(',')[-3:] == ['XfooY', 'Xfoo2Y', ''] |
|
252 | ||
253 | ||
254 |
def test_backoffice_ods(pub): |
|
255 |
create_superuser(pub) |
|
256 |
create_environment(pub) |
|
257 |
app = login(get_app(pub)) |
|
258 |
resp = app.get('/backoffice/management/form-title/') |
|
259 |
resp = resp.click('Export a Spreadsheet') |
|
260 |
assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet' |
|
261 |
assert 'filename=form-title.ods' in resp.headers['content-disposition'] |
|
262 |
assert resp.body[:2] == b'PK' # ods has a zip container |
|
263 | ||
264 |
formdef = FormDef.get_by_urlname('form-title') |
|
265 |
formdef.fields.append(fields.FileField( |
|
266 |
id='4', label='file field', type='file', |
|
267 |
display_locations=['validation', 'summary', 'listings'])) |
|
268 |
formdef.fields.append(fields.DateField( |
|
269 |
id='5', label='date field', type='date', |
|
270 |
display_locations=['validation', 'summary', 'listings'])) |
|
271 |
formdef.fields.append(fields.StringField( |
|
272 |
id='6', label='number field', type='string', |
|
273 |
display_locations=['validation', 'summary', 'listings'])) |
|
274 |
formdef.fields.append(fields.StringField( |
|
275 |
id='7', label='phone field', type='string', |
|
276 |
display_locations=['validation', 'summary', 'listings'])) |
|
277 |
formdef.fields.append(fields.DateField( |
|
278 |
id='8', label='very old field', type='date', |
|
279 |
display_locations=['validation', 'summary', 'listings'])) |
|
280 |
formdef.fields.append(fields.StringField( |
|
281 |
id='9', label='string field', type='string', |
|
282 |
display_locations=['validation', 'summary', 'listings'])) |
|
283 |
formdef.store() |
|
284 | ||
285 |
formdata = formdef.data_class().select(lambda x: x.status == 'wf-new')[0] |
|
286 |
formdata.data['4'] = PicklableUpload('/foo/bar', content_type='text/plain') |
|
287 |
formdata.data['4'].receive([b'hello world']) |
|
288 |
formdata.data['5'] = time.strptime('2015-05-12', '%Y-%m-%d') |
|
289 |
formdata.data['6'] = '12345' |
|
290 |
formdata.data['7'] = '0102030405' |
|
291 |
formdata.data['8'] = time.strptime('1871-03-18', '%Y-%m-%d') |
|
292 |
formdata.data['9'] = 'plop\npl\x1dop' # with control characters |
|
293 |
formdata.store() |
|
294 | ||
295 |
resp = app.get('/backoffice/management/form-title/') |
|
296 |
resp = resp.click('Export a Spreadsheet') |
|
297 |
assert resp.headers['content-type'] == 'application/vnd.oasis.opendocument.spreadsheet' |
|
298 |
assert 'filename=form-title.ods' in resp.headers['content-disposition'] |
|
299 |
assert resp.body[:2] == b'PK' # ods has a zip container |
|
300 | ||
301 |
zipf = zipfile.ZipFile(BytesIO(resp.body)) |
|
302 |
ods_sheet = ET.parse(zipf.open('content.xml')) |
|
303 |
# check the ods contains a link to the document |
|
304 |
elem = ods_sheet.findall('.//{%s}a' % ods.NS['text'])[0] |
|
305 |
assert elem.attrib['{%s}href' % ods.NS['xlink']] == 'http://example.net/backoffice/management/form-title/%s/files/4/bar' % formdata.id |
|
306 |
resp = app.get(elem.attrib['{%s}href' % ods.NS['xlink']]) |
|
307 |
assert resp.text == 'hello world' |
|
308 | ||
309 |
all_texts = [x.text for x in ods_sheet.findall('.//{%s}table-row//{%s}p' % (ods.NS['table'], ods.NS['text']))] |
|
310 |
created_column = all_texts.index('Created') |
|
311 |
date_column = all_texts.index('date field') |
|
312 |
number_column = all_texts.index('number field') |
|
313 |
phone_column = all_texts.index('phone field') |
|
314 |
old_column = all_texts.index('very old field') |
|
315 |
string_column = all_texts.index('string field') |
|
316 | ||
317 |
for row in ods_sheet.findall('.//{%s}table-row' % ods.NS['table']): |
|
318 |
if row.findall('.//{%s}table-cell/{%s}p' % ( |
|
319 |
ods.NS['table'], ods.NS['text']))[0].text == formdata.get_display_id(): |
|
320 |
break |
|
321 |
else: |
|
322 |
assert False, 'failed to find data row' |
|
323 | ||
324 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[created_column].attrib[ |
|
325 |
'{%s}value-type' % ods.NS['office']] == 'date' |
|
326 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[created_column].attrib[ |
|
327 |
'{%s}style-name' % ods.NS['table']] == 'DateTime' |
|
328 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[date_column].attrib[ |
|
329 |
'{%s}value-type' % ods.NS['office']] == 'date' |
|
330 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[date_column].attrib[ |
|
331 |
'{%s}style-name' % ods.NS['table']] == 'Date' |
|
332 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[number_column].attrib[ |
|
333 |
'{%s}value-type' % ods.NS['office']] == 'float' |
|
334 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[number_column].attrib[ |
|
335 |
'{%s}value' % ods.NS['office']] == '12345' |
|
336 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[phone_column].attrib[ |
|
337 |
'{%s}value-type' % ods.NS['office']] == 'string' |
|
338 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[old_column].attrib[ |
|
339 |
'{%s}value-type' % ods.NS['office']] == 'date' |
|
340 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[old_column].attrib[ |
|
341 |
'{%s}date-value' % ods.NS['office']] == '1871-03-18' |
|
342 |
assert row.findall('.//{%s}table-cell' % ods.NS['table'])[ |
|
343 |
string_column].find('{%s}p' % ods.NS['text']).text == 'plop\nplop' |
|
344 | ||
345 | ||
346 |
@pytest.mark.skipif('xlwt is None') |
|
347 |
def test_backoffice_xls(pub): |
|
348 |
create_superuser(pub) |
|
349 |
create_environment(pub) |
|
350 |
app = login(get_app(pub)) |
|
351 |
resp = app.get('/backoffice/management/form-title/') |
|
352 |
assert 'Excel Export' not in resp.text |
|
353 | ||
354 |
if not pub.site_options.has_section('options'): |
|
355 |
pub.site_options.add_section('options') |
|
356 |
pub.site_options.set('options', 'legacy-excel-export', 'true') |
|
357 |
fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') |
|
358 |
pub.site_options.write(fd) |
|
359 |
fd.close() |
|
360 | ||
361 |
resp = app.get('/backoffice/management/form-title/') |
|
362 |
resp = resp.click('Excel Export') |
|
363 |
assert resp.headers['content-type'].startswith('application/vnd.ms-excel') |
|
0 |
- |