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