wcs/tests/backoffice_pages/test_custom_view.py

563 lines
20 KiB
Python

# -*- coding: utf-8 -*-
import os
import pytest
try:
import xlwt
except ImportError:
xlwt = None
from quixote import get_publisher
from wcs.qommon.ident.password_accounts import PasswordAccount
from wcs.qommon.http_request import HTTPRequest
from wcs.carddef import CardDef
from wcs.formdef import FormDef
from wcs.roles import Role
from wcs import fields
from utilities import (get_app, login, create_temporary_pub, clean_temporary_pub)
from test_all import create_user, create_superuser
def pytest_generate_tests(metafunc):
if 'pub' in metafunc.fixturenames:
metafunc.parametrize('pub', ['pickle', 'sql', 'pickle-templates'], indirect=True)
@pytest.fixture
def pub(request, emails):
pub = create_temporary_pub(
sql_mode=bool('sql' in request.param),
templates_mode=bool('templates' in request.param)
)
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
pub.set_app_dir(req)
pub.cfg['identification'] = {'methods': ['password']}
pub.cfg['language'] = {'language': 'en'}
pub.write_cfg()
fd = open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w')
fd.write('''
[api-secrets]
coucou = 1234
''')
fd.close()
return pub
def teardown_module(module):
clean_temporary_pub()
def test_backoffice_custom_view(pub):
create_superuser(pub)
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = [
fields.ItemField(
id='1', label='field', type='item',
items=['foo', 'bar', 'baz'],
display_locations=['validation', 'summary', 'listings']),
]
formdef.workflow_roles = {'_receiver': 1}
formdef.store()
formdef.data_class().wipe()
for i in range(3):
formdata = formdef.data_class()()
formdata.data = {}
if i == 0:
formdata.data['1'] = 'foo'
formdata.data['1_display'] = 'foo'
else:
formdata.data['1'] = 'baz'
formdata.data['1_display'] = 'baz'
formdata.jump_status('new')
formdata.store()
formdef = FormDef()
formdef.workflow_roles = {'_receiver': 1}
formdef.name = 'other form'
formdef.fields = []
formdef.store()
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
assert resp.text.count('<span>User Label</span>') == 1
assert resp.text.count('<tr') == 4
# columns
resp.forms['listing-settings']['user-label'].checked = False
resp = resp.forms['listing-settings'].submit()
# filters
resp.forms['listing-settings']['filter-1'].checked = True
resp = resp.forms['listing-settings'].submit()
resp.forms['listing-settings']['filter-1-value'] = 'baz'
resp = resp.forms['listing-settings'].submit()
assert resp.text.count('<span>User Label</span>') == 0
assert resp.text.count('<tr') == 3
resp.forms['save-custom-view']['title'] = 'custom test view'
resp = resp.forms['save-custom-view'].submit()
assert resp.location.endswith('/user-custom-test-view/')
resp = resp.follow()
assert resp.text.count('<span>User Label</span>') == 0
assert resp.text.count('<tr') == 3
resp.forms['listing-settings']['filter-1-value'] = 'foo'
resp = resp.forms['listing-settings'].submit()
assert resp.text.count('<tr') == 2
assert resp.forms['save-custom-view']['update'].checked is True
resp = resp.forms['save-custom-view'].submit()
assert resp.location.endswith('/user-custom-test-view/')
resp = resp.follow()
assert resp.text.count('<tr') == 2
resp = app.get('/backoffice/management/other-form/')
assert 'custom test view' not in resp
# check it's not possible to create a view without any columns
for field_key in resp.forms['listing-settings'].fields:
if not field_key:
continue
if field_key.startswith('filter'):
continue
if resp.forms['listing-settings'][field_key].attrs.get('type') != 'checkbox':
continue
resp.forms['listing-settings'][field_key].checked = False
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'custom test view'
resp = resp.forms['save-custom-view'].submit().follow()
assert 'Views must have at least one column.' in resp.text
def test_backoffice_custom_view_delete(pub):
create_superuser(pub)
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = []
formdef.store()
custom_view = pub.custom_view_class()
custom_view.title = 'custom test view'
custom_view.formdef = formdef
custom_view.visibility = 'any'
custom_view.columns = {'list': [{'id': 'id'}]}
custom_view.filters = {}
custom_view.store()
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/custom-test-view/')
resp = resp.click('Delete')
resp = resp.form.submit()
assert resp.location.endswith('/management/form-title/')
resp = resp.follow()
assert 'custom test view' not in resp.text
def test_backoffice_custom_map_view(pub):
user = create_superuser(pub)
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = [
fields.StringField(
id='1', label='1st field', type='string',
display_locations=['validation', 'summary', 'listings']),
]
formdef.geolocations = {'base': 'Geolocafoobar'}
formdef.store()
custom_view = pub.custom_view_class()
custom_view.title = 'custom test view'
custom_view.formdef = formdef
custom_view.visibility = 'any'
custom_view.columns = {'list': [{'id': '1'}]}
custom_view.filters = {'filter-1': True, 'filter-1-value': 'baz'}
custom_view.user = user
custom_view.store()
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/custom-test-view/')
resp = resp.click('Plot on a Map')
assert resp.forms['listing-settings']['filter-1-value'].value == 'baz'
def test_backoffice_custom_view_reserved_slug(pub):
create_superuser(pub)
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = []
formdef.store()
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
resp.forms['listing-settings']['user-label'].checked = False
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'user custom test view'
resp = resp.forms['save-custom-view'].submit()
# check slug not created with "user" as prefix
assert resp.location.endswith('/user-userx-custom-test-view/')
resp = resp.follow()
def test_backoffice_custom_view_visibility(pub):
create_superuser(pub)
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = []
formdef.workflow_roles = {'_receiver': 1}
formdef.store()
agent = pub.user_class(name='agent')
agent.roles = [formdef.workflow_roles['_receiver']]
agent.store()
account = PasswordAccount(id='agent')
account.set_password('agent')
account.user_id = agent.id
account.store()
app = login(get_app(pub), username='agent', password='agent')
resp = app.get('/backoffice/management/form-title/')
# columns
resp.forms['listing-settings']['user-label'].checked = False
resp = resp.forms['listing-settings'].submit()
assert resp.text.count('<span>User Label</span>') == 0
resp.forms['save-custom-view']['title'] = 'custom test view'
assert 'visibility' not in resp.forms['save-custom-view'].fields
resp = resp.forms['save-custom-view'].submit()
assert resp.location.endswith('/user-custom-test-view/')
resp = resp.follow()
assert resp.text.count('<span>User Label</span>') == 0
# second agent
agent2 = pub.user_class(name='agent2')
agent2.roles = [formdef.workflow_roles['_receiver']]
agent2.store()
account = PasswordAccount(id='agent2')
account.set_password('agent2')
account.user_id = agent2.id
account.store()
app = login(get_app(pub), username='agent2', password='agent2')
resp = app.get('/backoffice/management/form-title/')
assert 'custom test view' not in resp
# shared custom view
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'shared view'
resp.forms['save-custom-view']['visibility'] = 'any'
resp = resp.forms['save-custom-view'].submit()
app = login(get_app(pub), username='agent2', password='agent2')
resp = app.get('/backoffice/management/form-title/')
resp = resp.click('shared view')
# don't allow a second "any" view with same slug
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'shared view'
resp.forms['save-custom-view']['visibility'] = 'any'
resp = resp.forms['save-custom-view'].submit()
assert set([(x.slug, x.visibility) for x in get_publisher().custom_view_class.select()]) == set(
[('custom-test-view', 'owner'), ('shared-view', 'any'), ('shared-view-2', 'any')])
def test_backoffice_custom_view_is_default(pub):
create_superuser(pub)
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = []
formdef.workflow_roles = {'_receiver': 1}
formdef.store()
# private custom view
agent = pub.user_class(name='agent')
agent.roles = [formdef.workflow_roles['_receiver']]
agent.store()
account = PasswordAccount(id='agent')
account.set_password('agent')
account.user_id = agent.id
account.store()
app = login(get_app(pub), username='agent', password='agent')
resp = app.get('/backoffice/management/form-title/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'view 1'
resp.forms['save-custom-view']['is_default'] = True
resp = resp.forms['save-custom-view'].submit()
# other private custom view
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'view 2'
resp.forms['save-custom-view']['visibility'] = 'owner'
resp.forms['save-custom-view']['is_default'] = True
resp = resp.forms['save-custom-view'].submit()
# shared custom view
resp = app.get('/backoffice/management/form-title/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'view 3'
resp.forms['save-custom-view']['visibility'] = 'any'
resp.forms['save-custom-view']['is_default'] = True
resp = resp.forms['save-custom-view'].submit()
assert pub.custom_view_class.count() == 3
assert pub.custom_view_class.get(1).is_default is True # simple user - private
assert pub.custom_view_class.get(2).is_default is True # super user - private
assert pub.custom_view_class.get(3).is_default is True # super user - shared
# not possible to define more than one default private view
resp = app.get('/backoffice/management/form-title/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'view 4'
resp.forms['save-custom-view']['visibility'] = 'owner'
resp.forms['save-custom-view']['is_default'] = True
resp = resp.forms['save-custom-view'].submit()
assert pub.custom_view_class.count() == 4
assert pub.custom_view_class.get(1).is_default is True # simple user - private
assert pub.custom_view_class.get(2).is_default is False # super user - private
assert pub.custom_view_class.get(3).is_default is True # super user - shared
assert pub.custom_view_class.get(4).is_default is True # super user - private 2
# not possible to define more than one default shared view
resp = app.get('/backoffice/management/form-title/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'view 5'
resp.forms['save-custom-view']['visibility'] = 'any'
resp.forms['save-custom-view']['is_default'] = True
resp = resp.forms['save-custom-view'].submit()
assert pub.custom_view_class.count() == 5
assert pub.custom_view_class.get(1).is_default is True # simple user - private
assert pub.custom_view_class.get(2).is_default is False # super user - private
assert pub.custom_view_class.get(3).is_default is False # super user - shared
assert pub.custom_view_class.get(4).is_default is True # super user - private 2
assert pub.custom_view_class.get(5).is_default is True # super user - shared 2
def test_backoffice_default_custom_view(pub):
user = create_superuser(pub)
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = [
fields.ItemField(
id='1', label='field 1', type='item',
items=['foo', 'bar', 'baz'],
display_locations=['validation', 'summary', 'listings']),
]
formdef.workflow_roles = {'_receiver': 1}
formdef.store()
formdef.data_class().wipe()
for i in range(3):
formdata = formdef.data_class()()
formdata.data = {}
if i == 0:
formdata.data['1'] = 'foo'
formdata.data['1_display'] = 'foo'
else:
formdata.data['1'] = 'baz'
formdata.data['1_display'] = 'baz'
if i < 2:
formdata.jump_status('new')
formdata.store()
app = login(get_app(pub))
# define a shared defautl view
custom_view = pub.custom_view_class()
custom_view.title = 'shared custom test view'
custom_view.formdef = formdef
custom_view.visibility = 'any'
custom_view.columns = {'list': [{'id': 'id'}]}
custom_view.filters = {'filter-1-value': 'baz', 'filter': 'all', 'filter-1': 'on', 'filter-status': 'on'}
custom_view.user = user
custom_view.is_default = True
custom_view.store()
resp = app.get('/backoffice/management/form-title/shared-custom-test-view/')
assert resp.text.count('<span>User Label</span>') == 0
assert resp.text.count('<span>field 1</span>') == 0
assert resp.text.count('<tr') == 3
# check that default view is applied
resp = app.get('/backoffice/management/form-title/')
assert resp.text.count('<span>User Label</span>') == 0
assert resp.text.count('<span>field 1</span>') == 0
assert resp.text.count('<tr') == 3
# define a user default view
custom_view = pub.custom_view_class()
custom_view.title = 'private custom test view'
custom_view.formdef = formdef
custom_view.visibility = 'owner'
custom_view.columns = {'list': [{'id': '1'}]}
custom_view.filters = {'filter-1-value': 'foo', 'filter': 'all', 'filter-1': 'on', 'filter-status': 'on'}
custom_view.user = user
custom_view.is_default = True
custom_view.store()
resp = app.get('/backoffice/management/form-title/user-private-custom-test-view/')
assert resp.text.count('<span>User Label</span>') == 0
assert resp.text.count('<span>field 1</span>') == 1
assert resp.text.count('<tr') == 2
# check that private default view is applied, and not shared default view
resp = app.get('/backoffice/management/form-title/')
assert resp.text.count('<span>User Label</span>') == 0
assert resp.text.count('<span>field 1</span>') == 1
assert resp.text.count('<tr') == 2
def test_backoffice_missing_custom_view(pub):
create_superuser(pub)
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = []
formdef.store()
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/user-plop/')
assert resp.location == 'http://example.net/backoffice/management/form-title/'
resp = resp.follow()
assert 'A missing or invalid custom view was referenced' in resp
resp = app.get('/backoffice/management/form-title/user-plop/1/')
assert resp.location == 'http://example.net/backoffice/management/form-title/1/'
resp = app.get('/backoffice/management/form-title/user-plop/1/?plop')
assert resp.location == 'http://example.net/backoffice/management/form-title/1/?plop'
def test_carddata_custom_view(pub, studio):
user = create_user(pub)
CardDef.wipe()
pub.custom_view_class.wipe()
carddef = CardDef()
carddef.name = 'foo'
carddef.fields = [
fields.StringField(id='1', label='Test', type='string', varname='foo'),
]
carddef.backoffice_submission_roles = user.roles
carddef.workflow_roles = {'_editor': user.roles[0]}
carddef.store()
app = login(get_app(pub))
resp = app.get('/backoffice/data/foo/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'card view'
resp = resp.forms['save-custom-view'].submit()
assert resp.location.endswith('/user-card-view/')
resp = resp.follow()
def test_carddata_custom_view_is_default(pub):
user = create_superuser(pub)
Role.wipe()
role = Role(name='foobar')
role.store()
user.roles = [role.id]
user.store()
CardDef.wipe()
pub.custom_view_class.wipe()
carddef = CardDef()
carddef.name = 'foo'
carddef.fields = [
fields.StringField(id='1', label='Test', type='string', varname='foo'),
]
carddef.backoffice_submission_roles = user.roles
carddef.digest_template = '{{ form_var_foo }}'
carddef.store()
carddef.data_class().wipe()
# datasource custom view
app = login(get_app(pub))
resp = app.get('/backoffice/data/foo/')
resp = resp.forms['listing-settings'].submit()
resp.forms['save-custom-view']['title'] = 'view 3'
resp.forms['save-custom-view']['visibility'] = 'datasource'
resp.forms['save-custom-view']['is_default'] = True
resp = resp.forms['save-custom-view'].submit()
assert pub.custom_view_class.get(1).is_default is False # not for datasource
def test_backoffice_custom_view_keep_filters(pub):
user = create_superuser(pub)
datasource = {
'type': 'formula',
'value': repr([('A', 'aa'), ('B', 'bb'), ('C', 'cc')])
}
FormDef.wipe()
pub.custom_view_class.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.fields = [
fields.StringField(
id='1', label='1st field', type='string',
display_locations=['validation', 'summary', 'listings']),
fields.ItemField(
id='2', label='2nd field', type='item',
data_source=datasource, varname='foo'),
]
formdef.store()
custom_view = pub.custom_view_class()
custom_view.title = 'custom test view'
custom_view.formdef = formdef
custom_view.visibility = 'owner'
custom_view.columns = {'list': [{'id': '1'}]}
custom_view.filters = {'filter-1': True, 'filter-1-value': 'baz'}
custom_view.user = user
custom_view.store()
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/user-custom-test-view/')
assert 'filter-1-value' in resp.forms['listing-settings'].fields
assert 'filter-2-value' not in resp.forms['listing-settings'].fields
resp = app.get('/backoffice/management/form-title/user-custom-test-view/?filter-foo=A')
assert 'filter-1-value' not in resp.forms['listing-settings'].fields
assert 'filter-2-value' in resp.forms['listing-settings'].fields
resp = app.get('/backoffice/management/form-title/user-custom-test-view/?filter-foo=A&keep-view-filters=on')
assert 'filter-1-value' in resp.forms['listing-settings'].fields
assert 'filter-2-value' in resp.forms['listing-settings'].fields