Commit 79d406cb authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Change Preference accessor to use category property. Build accessor from...

Change Preference accessor to use category property. Build accessor from accessor holder accessors and not from properties.

Conflicts:
	product/ERP5Form/tests/testPreferences.py
parent 6ee39f47
......@@ -58,28 +58,15 @@ def _generatePreferenceToolAccessorHolder(portal_type_name,
preference_tool_accessor_holder = AccessorHolderType('PreferenceTool')
for accessor_holder in accessor_holder_list:
for prop in accessor_holder._properties:
if not prop.get('preference'):
continue
# XXX read_permission and write_permissions defined at
# property sheet are not respected by this.
# only properties marked as preference are used
preferred_accessor = ("isPreferred", "hasPreferred", "getPreferred",
"getDefaultPreferred", "hasDefaultPreferred")
# properties have already been 'converted' and _list is appended
# to list_types properties
attribute = prop['id']
if attribute.endswith('_list'):
attribute = prop['base_id']
attr_list = [ 'get%s' % convertToUpperCase(attribute)]
if prop['type'] == 'boolean':
attr_list.append('is%s' % convertToUpperCase(attribute))
if prop['type'] in list_types :
attr_list.append('get%sList' % convertToUpperCase(attribute))
read_permission = prop.get('read_permission')
for attribute_name in attr_list:
method = PreferenceMethod(attribute_name, prop.get('default'))
preference_tool_accessor_holder.registerAccessor(method, read_permission)
for accessor_holder in accessor_holder_list:
for accessor in accessor_holder.__dict__:
if accessor.startswith(preferred_accessor) and not accessor.endswith("__roles__"):
method = PreferenceMethod(accessor)
preference_tool_accessor_holder.registerAccessor(method,
'Access contents information')
accessor_holder_module.registerAccessorHolder(preference_tool_accessor_holder)
......
......@@ -30,6 +30,8 @@
from AccessControl import ClassSecurityInfo
from AccessControl.SecurityManagement import getSecurityManager,\
setSecurityManager, newSecurityManager
from AccessControl.ZopeGuards import guarded_getattr
from MethodObject import Method
from Products.ERP5Type.Globals import InitializeClass, DTMLFile
from zLOG import LOG, PROBLEM
......@@ -60,9 +62,8 @@ class PreferenceMethod(Method):
func_code.co_argcount = 1
func_defaults = ()
def __init__(self, attribute, default):
def __init__(self, attribute):
self.__name__ = self._preference_getter = attribute
self._preference_default = default
self._preference_cache_id = 'PreferenceTool.CachingMethod.%s' % attribute
def __call__(self, instance, default=_marker, *args, **kw):
......@@ -72,18 +73,23 @@ class PreferenceMethod(Method):
# there is a design problem in current archive API.
sql_catalog_id = kw.pop('sql_catalog_id', None)
for pref in instance._getSortedPreferenceList(sql_catalog_id=sql_catalog_id):
value = getattr(pref, self._preference_getter)(_marker, *args, **kw)
value = guarded_getattr(pref, self._preference_getter)(_marker, *args, **kw)
# XXX Due to UI limitation, null value is treated as if the property
# was not defined. The drawback is that it is not possible for a
# user to mask a non-null global value with a null value.
if value not in (_marker, None, '', (), []):
return value
if default is _marker:
return self._preference_default
from Products.ERP5Type.Document import newTempPreference
return CachingMethod(getattr(newTempPreference(instance, ''),
self._preference_getter),
id=id+"_default",
cache_factory='erp5_ui_short')()
return default
id = '%s.%s' % (self._preference_cache_id,
getSecurityManager().getUser().getId())
_getPreference = CachingMethod(_getPreference,
id='%s.%s' % (self._preference_cache_id,
getSecurityManager().getUser().getId()),
id=id,
cache_factory='erp5_ui_short')
return _getPreference(default, *args, **kw)
......
......@@ -426,13 +426,13 @@ class TestPreferences(PropertySheetTestCase):
method = pref_tool.getPreferredAccountingTransactionSimulationState
state = method()
self.assertEqual(state, [])
self.assertEquals(state, None)
state = method('default')
self.assertEqual(state, 'default')
self.assertEquals(state, 'default')
method = lambda *args: pref_tool.getPreference('preferred_accounting_transaction_simulation_state', *args)
state = method()
self.assertEqual(state, [])
self.assertEquals(state, None)
state = method('default')
self.assertEqual(state, 'default')
......@@ -559,20 +559,20 @@ class TestPreferences(PropertySheetTestCase):
self._addProperty('Preference',
'test_boolean_accessor Preference',
portal_type='Standard Property',
property_id='dummy',
property_id='preferred_dummy',
preference=True,
elementary_type='boolean')
portal_preferences = self.portal.portal_preferences
self.assertFalse(portal_preferences.getDummy())
self.assertFalse(portal_preferences.isDummy())
self.assertFalse(portal_preferences.getPreferredDummy())
self.assertFalse(portal_preferences.isPreferredDummy())
preference = portal_preferences.newContent(portal_type='Preference',
dummy=True)
preferred_dummy=True)
preference.enable()
self.tic()
self.assertTrue(portal_preferences.getDummy())
self.assertTrue(portal_preferences.isDummy())
self.assertTrue(portal_preferences.getPreferredDummy())
self.assertTrue(portal_preferences.isPreferredDummy())
def test_property_sheet_security_on_permission(self):
""" Added a test to make sure permissions are used into portal
......@@ -595,7 +595,7 @@ class TestPreferences(PropertySheetTestCase):
self.assertTrue(guarded_hasattr(obj, 'setPreferredToto'))
obj.setPreferredToto("A TEST")
self.assertTrue(guarded_hasattr(obj, 'getPreferredToto'))
self.assertEquals("A TEST", obj.getPreferredToto())
obj.manage_permission(write_permission, [], 0)
self.assertFalse(guarded_hasattr(obj, 'setPreferredToto'))
......@@ -620,8 +620,12 @@ class TestPreferences(PropertySheetTestCase):
preference_tool.manage_permission(write_permission, ['Manager'], 1)
preference_tool.manage_permission(read_permission, [], 0)
obj.manage_permission(read_permission, [], 0)
self.assertFalse(guarded_hasattr(preference_tool, 'getPreferredToto'))
self.logout()
self.assertEqual(None, preference_tool.getPreferredToto())
self.login('manager')
self.assertEquals("A TEST", preference_tool.getPreferredToto())
preference_tool.manage_permission(read_permission, ['Manager'], 1)
def test_system_preference_value_prefererred(self):
......@@ -631,30 +635,30 @@ class TestPreferences(PropertySheetTestCase):
self._addProperty('Preference',
'test_system_preference_value_prefererred Preference',
portal_type='Standard Property',
property_id='dummystring',
property_id='preferred_dummy_string',
property_default='python: "%s"' % default_preference_string,
preference=True,
elementary_type='string')
portal_preferences = self.portal.portal_preferences
self.assertEqual(default_preference_string,
portal_preferences.getDummystring())
portal_preferences.getPreferredDummyString())
preference = portal_preferences.newContent(portal_type='Preference',
dummystring=normal_preference_string,
preferred_dummy_string=normal_preference_string,
priority=Priority.SITE)
preference.enable()
self.tic()
self.assertEqual(normal_preference_string,
portal_preferences.getDummystring())
portal_preferences.getPreferredDummyString())
system_preference = portal_preferences.newContent(portal_type='System Preference',
dummystring=system_preference_string)
preferred_dummy_string=system_preference_string)
system_preference.enable()
self.tic()
self.assertEqual(system_preference_string,
portal_preferences.getDummystring())
portal_preferences.getPreferredDummyString())
@expectedFailure
def test_system_preference_value_prefererred_clear_cache_disabled(self):
......
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