Commit 17f5aec9 authored by Romain Courteaud's avatar Romain Courteaud

[erp5_ods_style] Allow to export module into a spreadsheet in ERP5JS

parent 80bb093d
......@@ -16,6 +16,8 @@ import json
import re
import urllib
from zope.globalrequest import setRequest
from Acquisition import aq_base
from Products.ERP5Form.Selection import Selection, DomainSelection
......@@ -129,7 +131,29 @@ def do_fake_request(request_method, headers=None, data=()):
body_stream.write('{}={!s}&'.format(
urllib.quote_plus(key), urllib.quote(value)))
return HTTPRequest(body_stream, env, HTTPResponse())
request = HTTPRequest(body_stream, env, HTTPResponse())
if data and request_method.upper() == 'POST':
for key, value in data:
request.form[key] = value
return request
def replace_request(new_request, context):
base_chain = [aq_base(x) for x in context.aq_chain]
# Grab existig request (last chain item) and create a copy.
request_container = base_chain.pop()
# request = request_container.REQUEST
setRequest(new_request)
new_request_container = request_container.__class__(REQUEST=new_request)
# Recreate acquisition chain.
my_self = new_request_container
base_chain.reverse()
for item in base_chain:
my_self = item.__of__(my_self)
return my_self
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
......@@ -2605,4 +2629,141 @@ class TestERP5Action_getHateoas(ERP5HALJSONStyleSkinsMixin):
'your_custom_workflow_variable', response))
self.assertIn('error_text', response['your_custom_workflow_variable'], "Invalid field must contain error message")
self.assertGreater(len(response['your_custom_workflow_variable']['error_text']), 0, "Error message must not be empty")
\ No newline at end of file
class TestERP5ODS(ERP5HALJSONStyleSkinsMixin):
def afterSetUp(self):
ERP5HALJSONStyleSkinsMixin.afterSetUp(self)
document = self._makeDocument()
document.edit(title='foook1')
document = self._makeDocument()
document.edit(title='foook2')
document = self._makeDocument()
document.edit(title='foonotok')
self.tic()
@changeSkin('Hal')
def test_getHateoas_exportCSV(self, **kw):
"""Check that ODS export returns lines calculated from latest search
"""
self.portal.foo_module.FooModule_viewFooList.listbox.ListBox_setPropertyList(
field_columns = '\n'.join((
'id | ID',
'title | Title',
'creation_date | Creation Date',
)))
# Create the listbox selection
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.ERP5Document_getHateoas(
REQUEST=fake_request,
mode="search",
# local_roles=["Manager"],
query='title:"foook%"',
list_method='searchFolder',
select_list=['title', 'creation_date', 'uid'],
relative_url='foo_module',
form_relative_url='portal_skins/erp5_ui_test/FooModule_viewFooList/listbox',
sort_on=json.dumps(["title","descending"])
)
self.assertEquals(fake_request.RESPONSE.status, 200)
self.assertEquals(fake_request.RESPONSE.getHeader('Content-Type'),
"application/hal+json"
)
result_dict = json.loads(result)
self.assertEqual(len(result_dict['_embedded']['contents']), 2)
# Export as CSV
fake_request = do_fake_request("POST", data=(
('dialog_method', 'Base_viewAsODS'),
('dialog_id', 'Base_viewAsODSDialog'),
('form_id', 'FooModule_viewFooList'),
('selection_name', 'foo_selection'),
('field_your_print_mode', 'list'),
('default_field_your_print_mode:int', '0'),
('field_your_format', 'csv'),
('default_field_your_format:int', '0'),
('field_your_target_language', ''),
('default_field_your_target_language:int', '0'),
('extra_param_json', '{}'),
))
fake_portal = replace_request(fake_request, self.portal)
result = fake_portal.web_site_module.hateoas.foo_module.Base_callDialogMethod(
dialog_method='Base_viewAsODS',
dialog_id='Base_viewAsODSDialog',
form_id='FooModule_viewFooList',
)
self.assertEqual(fake_request.get('portal_skin'), 'ODS')
self.assertEqual(fake_request.RESPONSE.status, 200)
self.assertEqual(fake_request.RESPONSE.getHeader('Content-Type'), 'application/csv')
expected_csv = 'Title,Creation Date\nfoook2,XX/XX/XXXX XX:XX:XX\nfoook1,XX/XX/XXXX XX:XX:XX\n'
self.assertEqual(len(result), len(expected_csv), result)
prefix_length = len('Title,Creation Date\nfoook2,')
self.assertEqual(result[:prefix_length], expected_csv[:prefix_length])
@changeSkin('Hal')
def test_getHateoas_exportAllViewCSV(self, **kw):
"""Check that ODS export returns views calculated from latest search
"""
self.portal.foo_module.FooModule_viewFooList.listbox.ListBox_setPropertyList(
field_columns = '\n'.join((
'id | ID',
'title | Title',
'creation_date | Creation Date',
)))
# Create the listbox selection
fake_request = do_fake_request("GET")
result = self.portal.web_site_module.hateoas.ERP5Document_getHateoas(
REQUEST=fake_request,
mode="search",
# local_roles=["Manager"],
query='title:"foook%"',
list_method='searchFolder',
select_list=['title', 'creation_date', 'uid'],
relative_url='foo_module',
form_relative_url='portal_skins/erp5_ui_test/FooModule_viewFooList/listbox',
sort_on=json.dumps(["title","descending"])
)
self.assertEquals(fake_request.RESPONSE.status, 200)
self.assertEquals(fake_request.RESPONSE.getHeader('Content-Type'),
"application/hal+json"
)
result_dict = json.loads(result)
self.assertEqual(len(result_dict['_embedded']['contents']), 2)
# Export as CSV
fake_request = do_fake_request("POST", data=(
('dialog_method', 'Base_viewAsODS'),
('dialog_id', 'Base_viewAsODSDialog'),
('form_id', 'FooModule_viewFooList'),
('selection_name', 'foo_selection'),
('field_your_print_mode', 'list_view'),
('default_field_your_print_mode:int', '0'),
('field_your_format', 'csv'),
('default_field_your_format:int', '0'),
('field_your_target_language', ''),
('default_field_your_target_language:int', '0'),
('extra_param_json', '{}'),
))
fake_portal = replace_request(fake_request, self.portal)
result = fake_portal.web_site_module.hateoas.foo_module.Base_callDialogMethod(
dialog_method='Base_viewAsODS',
dialog_id='Base_viewAsODSDialog',
form_id='FooModule_viewFooList',
)
self.assertEqual(fake_request.get('portal_skin'), 'ODS')
self.assertEqual(fake_request.RESPONSE.status, 200)
self.assertEqual(fake_request.RESPONSE.getHeader('Content-Type'), 'application/csv')
self.assertTrue('foook1' in result, result)
self.assertTrue('foook2' in result, result)
self.assertTrue('foonotok' not in result, result)
# Check one of the field name
self.assertTrue('Read-Only Quantity' in result, result)
# Ensure it is not the list mode rendering
self.assertTrue(len(result.split('\n')) > 50, result)
......@@ -2,4 +2,5 @@ erp5_full_text_mroonga_catalog
erp5_ui_test_core
erp5_ui_test
erp5_dummy_movement
erp5_configurator_standard_trade_template
\ No newline at end of file
erp5_configurator_standard_trade_template
erp5_ods_style
\ No newline at end of file
......@@ -14,7 +14,7 @@
</item>
<item>
<key> <string>category</string> </key>
<value> <string>object_exchange</string> </value>
<value> <string>object_jio_exchange</string> </value>
</item>
<item>
<key> <string>condition</string> </key>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment