Commit b2f7ee06 authored by Yoshinori Okuji's avatar Yoshinori Okuji

default can be passed with a keyword.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@15743 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 00539b6e
...@@ -40,6 +40,7 @@ from Products.ERP5Type.Accessor.TypeDefinition import list_types ...@@ -40,6 +40,7 @@ from Products.ERP5Type.Accessor.TypeDefinition import list_types
from Products.ERP5Form import _dtmldir from Products.ERP5Form import _dtmldir
from Products.ERP5Form.Document.Preference import Priority from Products.ERP5Form.Document.Preference import Priority
_marker = []
def updatePreferenceClassPropertySheetList(): def updatePreferenceClassPropertySheetList():
# The Preference class should be imported from the common location # The Preference class should be imported from the common location
...@@ -124,11 +125,10 @@ class PreferenceMethod(Method): ...@@ -124,11 +125,10 @@ class PreferenceMethod(Method):
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
def _getPreference(user_name=None): def _getPreference(user_name=None):
MARKER = []
value = None value = None
for pref in instance._getSortedPreferenceList(): for pref in instance._getSortedPreferenceList():
value = getattr(pref, self._preference_name, MARKER) value = getattr(pref, self._preference_name, _marker)
if value is not MARKER: if value is not _marker:
# If callable, store the return value. # If callable, store the return value.
if callable(value): if callable(value):
value = value() value = value()
...@@ -144,8 +144,13 @@ class PreferenceMethod(Method): ...@@ -144,8 +144,13 @@ class PreferenceMethod(Method):
# all values are null values, one of them must be returned. # all values are null values, one of them must be returned.
# Therefore, return a default value, only if explicitly specified, # Therefore, return a default value, only if explicitly specified,
# instead of returning None. # instead of returning None.
if value in self._null and args: default = _marker
return args[0] if 'default' in kw:
default = kw['default']
elif args:
default = args[0]
if value in self._null and default is not _marker:
return default
return value return value
class PreferenceTool(BaseTool): class PreferenceTool(BaseTool):
...@@ -176,11 +181,15 @@ class PreferenceTool(BaseTool): ...@@ -176,11 +181,15 @@ class PreferenceTool(BaseTool):
BaseTool.inheritedAttribute('manage_afterAdd')(self, item, container) BaseTool.inheritedAttribute('manage_afterAdd')(self, item, container)
security.declareProtected(Permissions.View, "getPreference") security.declareProtected(Permissions.View, "getPreference")
def getPreference(self, pref_name, default=None) : def getPreference(self, pref_name, default=_marker) :
""" get the preference on the most appopriate Preference object. """ """ get the preference on the most appopriate Preference object. """
method = getattr(self, 'get%s' % convertToUpperCase(pref_name), None) method = getattr(self, 'get%s' % convertToUpperCase(pref_name), None)
if method is not None: if method is not None:
return method(default=default) if default is not _marker:
kw = {'default': default}
else:
kw = {}
return method(**kw)
return default return default
security.declareProtected(Permissions.ModifyPortalContent, "setPreference") security.declareProtected(Permissions.ModifyPortalContent, "setPreference")
......
...@@ -376,13 +376,24 @@ class TestPreferences(ERP5TypeTestCase): ...@@ -376,13 +376,24 @@ class TestPreferences(ERP5TypeTestCase):
state = method('default') state = method('default')
self.assertEquals(state, 'default') self.assertEquals(state, 'default')
method = lambda *args: pref_tool.getPreference('preferred_accounting_transaction_simulation_state', *args)
state = method()
self.assertEquals(state, None)
state = method('default')
self.assertEquals(state, 'default')
method = pref_tool.getPreferredAccountingTransactionSimulationStateList method = pref_tool.getPreferredAccountingTransactionSimulationStateList
state_list = method() state_list = method()
self.assertEquals(state_list, None) self.assertEquals(state_list, None)
state_list = method(('default',)) state_list = method(('default',))
self.assertEquals(state_list, ('default',)) self.assertEquals(state_list, ('default',))
method = lambda *args: pref_tool.getPreference('preferred_accounting_transaction_simulation_state_list', *args)
state_list = method()
self.assertEquals(state_list, None)
state_list = method(('default',))
self.assertEquals(state_list, ('default',))
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestPreferences)) suite.addTest(unittest.makeSuite(TestPreferences))
......
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