Commit 56db74f4 authored by Jérome Perrin's avatar Jérome Perrin

Fix translation in deferred style

This fixes the problem that deferred style does not honor Localizer cookie and just translate according to user agent's `Accept-Language` [#20180319-1D1E0A3](https://nexedi.erp5.net/bug_module/20180319-1D1E0A3/) 

Filtering of request arguments have been reworked to stop code duplication and fix [#20180220-1DBF516](https://nexedi.erp5.net/bug_module/20180220-1DBF516)

/cc @gabriel  @luke @aurel 

/reviewed-on nexedi/erp5!611
parents 93ac620a 9c478ef4
......@@ -20,22 +20,9 @@ if person_value.getDefaultEmailText('') in ('', None):
return context.Base_redirect('view', keep_items=dict(
portal_status_message=translateString(
"You haven't defined your email address")))
# save request parameters
# XXX we exclude some reserved names in a very ad hoc way
request_form = {}
for k, v in request.form.items():
if k not in ('TraversalRequestNameStack', 'AUTHENTICATED_USER', 'URL',
'SERVER_URL', 'AUTHENTICATION_PATH', 'USER_PREF_LANGUAGES', 'PARENTS',
'PUBLISHED', 'AcceptLanguage', 'AcceptCharset', 'RESPONSE',
'ACTUAL_URL'):
# XXX proxy fields stores a cache in request.other that cannot be pickled
if str(k).startswith('field__proxyfield'):
continue
# Remove FileUpload parameters
elif getattr(v, 'headers', ''):
continue
request_form[k] = v
request_form = portal.ERP5Site_filterRequestForDeferredStyle(request)
localizer_language = portal.Localizer.get_selected_language()
......
......@@ -22,22 +22,10 @@ format = request.get('format', '')
skin_name = request['deferred_portal_skin']
# save request parameters (after calling the report_method which may tweak the
# request). XXX we exclude some reserved names in a very ad hoc way
request_other = {}
for k, v in request.items():
if k not in ('TraversalRequestNameStack', 'AUTHENTICATED_USER', 'URL',
'SERVER_URL', 'AUTHENTICATION_PATH', 'USER_PREF_LANGUAGES', 'PARENTS',
'PUBLISHED', 'AcceptLanguage', 'AcceptCharset', 'RESPONSE', 'SESSION',
'ACTUAL_URL'):
# XXX proxy fields stores a cache in request.other that cannot be pickled
if same_type(k, '') and k.startswith('field__proxyfield'):
continue
# Remove FileUpload parameters
elif getattr(v, 'headers', ''):
continue
request_other[k] = v
# request).
request_other = portal.ERP5Site_filterRequestForDeferredStyle(request)
localizer_language = portal.Localizer.get_selected_language()
context.activate(activity="SQLQueue", tag=tag, after_tag=after_tag,
priority=priority).Base_computeReportSection(
......@@ -47,7 +35,8 @@ context.activate(activity="SQLQueue", tag=tag, after_tag=after_tag,
tag=tag,
skin_name=skin_name,
format=format,
priority=priority,
priority=priority,
localizer_language=localizer_language,
**kw)
context.activate(activity='SQLQueue', after_tag=tag).getTitle()
......
portal = context.getPortalObject()
N_ = portal.Base_translateString
form = context.restrictedTraverse(form)
request = container.REQUEST
request.other.update(request_other)
if form.meta_type == 'ERP5 Report':
report_section_list = getattr(context, form.report_method)()
elif form.meta_type == 'ERP5 Form':
report_section_list = []
for field in form.get_fields():
if field.getRecursiveTemplateField().meta_type == 'ReportBox':
report_section_list.extend(field.render())
else:
raise ValueError, 'form meta_type (%r) unknown' %(form.meta_type,)
with portal.Localizer.translationContext(localizer_language):
if form.meta_type == 'ERP5 Report':
report_section_list = getattr(context, form.report_method)()
elif form.meta_type == 'ERP5 Form':
report_section_list = []
for field in form.get_fields():
if field.getRecursiveTemplateField().meta_type == 'ReportBox':
report_section_list.extend(field.render())
else:
raise ValueError, 'form meta_type (%r) unknown' %(form.meta_type,)
report_title = portal.Base_translateString((form.getProperty('title')))
# Rebuild request_other as report section can have modify request content
request_other = {}
for k, v in request.items():
if k not in ('TraversalRequestNameStack', 'AUTHENTICATED_USER', 'URL',
'SERVER_URL', 'AUTHENTICATION_PATH', 'USER_PREF_LANGUAGES', 'PARENTS',
'PUBLISHED', 'AcceptLanguage', 'AcceptCharset', 'RESPONSE', 'SESSION',
'ACTUAL_URL'):
# XXX proxy fields stores a cache in request.other that cannot be pickled
if same_type(k, '') and str(k).startswith('field__proxyfield'):
continue
# Remove FileUpload parameters
elif getattr(v, 'headers', ''):
continue
request_other[k] = v
request_other = portal.ERP5Site_filterRequestForDeferredStyle(request)
localizer_language = portal.Localizer.get_selected_language()
active_process = portal.portal_activities.newActiveProcess()
for idx, report_section in enumerate(report_section_list):
......@@ -57,7 +46,7 @@ activity_context.activate(activity='SQLQueue', after_tag=tag, priority=priority)
active_process_url=active_process.getRelativeUrl(),
skin_name=skin_name,
localizer_language=localizer_language,
title=N_(form.getProperty('title')),
title=report_title,
request_other=request_other,
form_path=form.getPhysicalPath(),
user_name=user_name,
......
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>form, request_other, user_name, tag, skin_name, format, priority, **kw</string> </value>
<value> <string>form, request_other, user_name, tag, skin_name, format, priority, localizer_language, **kw</string> </value>
</item>
<item>
<key> <string>_proxy_roles</string> </key>
......
request_other = {}
for k, v in request.other.items() + request.form.items():
if k not in ('TraversalRequestNameStack', 'AUTHENTICATED_USER', 'URL',
'SERVER_URL', 'AUTHENTICATION_PATH', 'USER_PREF_LANGUAGES', 'PARENTS',
'PUBLISHED', 'AcceptLanguage', 'AcceptCharset', 'RESPONSE', 'SESSION',
'ACTUAL_URL', 'HTTP_COOKIE'):
# XXX proxy fields stores a cache in request.other that cannot be pickled
if same_type(k, '') and k.startswith('field__proxyfield'):
continue
# Remove FileUpload parameters
elif getattr(v, 'headers', ''):
continue
request_other[k] = v
return request_other
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>request</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ERP5Site_filterRequestForDeferredStyle</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -51,8 +51,11 @@ class TestDeferredStyle(ERP5TypeTestCase, ZopeTestCase.Functional):
def getBusinessTemplateList(self):
return ('erp5_core_proxy_field_legacy',
'erp5_base', 'erp5_ods_style',
'erp5_odt_style', 'erp5_deferred_style',)
'erp5_base',
'erp5_ods_style',
'erp5_odt_style',
'erp5_deferred_style',
'erp5_l10n_fr',)
def afterSetUp(self):
self.login()
......@@ -148,6 +151,63 @@ class TestDeferredStyle(ERP5TypeTestCase, ZopeTestCase.Functional):
else:
self.fail('Attachment not found in email\n%s' % message_text)
def test_lang_negociation(self):
# User's Accept-Language header is honored in reports.
self.loginAsUser('bob')
self.portal.changeSkin('Deferred')
response = self.publish(
'/%s/person_module/pers/Base_viewHistory?deferred_portal_skin=%s'
% (self.portal.getId(), self.skin),
'%s:%s' % (self.username, self.password),
extra={
'HTTP_ACCEPT_LANGUAGE': 'fr;q=0.9,en;q=0.8',
})
self.tic()
mail_message = email.message_from_string(self.portal.MailHost._last_message[2])
# mail subject is translated
self.assertEqual('Historique', mail_message['subject'])
# content is translated
part, = [x for x in mail_message.walk() if x.get_content_type() == self.content_type]
self.assertIn(
'Historique',
self.portal.portal_transforms.convertTo(
'text/plain',
part.get_payload(decode=True),
context=self.portal,
mimetype=self.content_type).getData())
def test_lang_negociation_cookie(self):
# User's LOCALIZER_LANGUAGE cookie is honored in reports and have priority over Accept-Language
self.loginAsUser('bob')
self.portal.changeSkin('Deferred')
response = self.publish(
'/%s/person_module/pers/Base_viewHistory?deferred_portal_skin=%s'
% (self.portal.getId(), self.skin),
'%s:%s' % (self.username, self.password),
# user has configured preferred language to english
extra={
'HTTP_ACCEPT_LANGUAGE': 'en;q=0.9,fr;q=0.8',
},
# but has forced to french in a Localizer cookie
env={
'HTTP_COOKIE': 'LOCALIZER_LANGUAGE="fr"',
})
self.tic()
mail_message = email.message_from_string(self.portal.MailHost._last_message[2])
# mail subject is translated
self.assertEqual('Historique', mail_message['subject'])
# content is translated
mail_message = email.message_from_string(self.portal.MailHost._last_message[2])
part, = [x for x in mail_message.walk() if x.get_content_type() == self.content_type]
self.assertIn(
'Historique',
self.portal.portal_transforms.convertTo(
'text/plain',
part.get_payload(decode=True),
context=self.portal,
mimetype=self.content_type).getData())
class TestODSDeferredStyle(TestDeferredStyle):
skin = 'ODS'
......
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