Commit 10673188 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

cleanup selection tool implementation.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38247 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 406fbe4e
...@@ -167,9 +167,6 @@ class SelectionTool( BaseTool, SimpleItem ): ...@@ -167,9 +167,6 @@ class SelectionTool( BaseTool, SimpleItem ):
storage = 'selection_data' storage = 'selection_data'
return storage return storage
def isMemcachedUsed(self):
return 'portal_memcached' in self.getStorage()
def _redirectToOriginalForm(self, REQUEST=None, form_id=None, dialog_id=None, def _redirectToOriginalForm(self, REQUEST=None, form_id=None, dialog_id=None,
query_string=None, query_string=None,
no_reset=False, no_report_depth=False): no_reset=False, no_report_depth=False):
...@@ -207,8 +204,6 @@ class SelectionTool( BaseTool, SimpleItem ): ...@@ -207,8 +204,6 @@ class SelectionTool( BaseTool, SimpleItem ):
""" """
Returns the selection names of the current user. Returns the selection names of the current user.
""" """
if self.isMemcachedUsed():
return []
return sorted(self._getSelectionNameListFromContainer()) return sorted(self._getSelectionNameListFromContainer())
# backward compatibility # backward compatibility
...@@ -1402,12 +1397,7 @@ class SelectionTool( BaseTool, SimpleItem ): ...@@ -1402,12 +1397,7 @@ class SelectionTool( BaseTool, SimpleItem ):
# focus the temporary selection in the most narrow scope. # focus the temporary selection in the most narrow scope.
return temporary_selection_dict[selection_name][-1] return temporary_selection_dict[selection_name][-1]
if self.isMemcachedUsed(): return self._getContainer().getSelection(user_id, selection_name)
return self._getMemcachedContainer().get('%s-%s' %
(user_id, selection_name))
else:
return self._getPersistentContainer(user_id).get(selection_name,
None)
def _setSelectionToContainer(self, selection_name, selection): def _setSelectionToContainer(self, selection_name, selection):
user_id = self._getUserId() user_id = self._getUserId()
...@@ -1420,59 +1410,104 @@ class SelectionTool( BaseTool, SimpleItem ): ...@@ -1420,59 +1410,104 @@ class SelectionTool( BaseTool, SimpleItem ):
temporary_selection_dict[selection_name][-1] = selection temporary_selection_dict[selection_name][-1] = selection
return return
if self.isMemcachedUsed(): self._getContainer().setSelection(user_id, selection_name, selection)
self._getMemcachedContainer().set('%s-%s' % (user_id, selection_name), aq_base(selection))
else:
self._getPersistentContainer(user_id)[selection_name] = aq_base(selection)
def _deleteSelectionForUserFromContainer(self, selection_name, user_id): def _deleteSelectionForUserFromContainer(self, selection_name, user_id):
if user_id is None: return None if user_id is None: return None
if self.isMemcachedUsed(): self._getContainer().deleteSelection(user_id, selection_name)
del(self._getMemcachedContainer()['%s-%s' % (user_id, selection_name)])
else:
del(self._getPersistentContainer(user_id)[selection_name])
def _deleteSelectionFromContainer(self, selection_name): def _deleteSelectionFromContainer(self, selection_name):
user_id = self._getUserId() user_id = self._getUserId()
self._deleteSelectionForUserFromContainer(selection_name, user_id) self._deleteSelectionForUserFromContainer(selection_name, user_id)
def _deleteGlobalSelectionFromContainer(self, selection_name): def _deleteGlobalSelectionFromContainer(self, selection_name):
if not self.isMemcachedUsed(): self._getContainer().deleteGlobalSelection(self, selection_name)
if getattr(aq_base(self), 'selection_data', None) is not None:
for user_id in self.selection_data.keys():
mapping = self._getPersistentContainer(user_id)
if mapping.has_key(selection_name):
del(mapping[selection_name])
def _getSelectionNameListFromContainer(self): def _getSelectionNameListFromContainer(self):
if self.isMemcachedUsed(): user_id = self._getUserId()
return [] return list(set(self._getContainer().getSelectionNameList(user_id) + \
else: self.getTemporarySelectionDict().keys()))
user_id = self._getUserId()
if user_id is None: return [] def _getContainer(self):
container_id = '_v_selection_container'
tv = getTransactionalVariable(self) storage = self.getStorage()
return list(set(self._getPersistentContainer(user_id).keys() + self.getTemporarySelectionDict().keys())) container = getattr(aq_base(self), container_id, None)
if container is None:
def _getMemcachedContainer(self): if storage.startswith('portal_memcached/'):
value = getattr(aq_base(self), '_v_selection_data', None) plugin_path = storage
if value is None: value = self.getPortalObject().\
plugin_path = self.getStorage() portal_memcached.getMemcachedDict(key_prefix='selection_tool',
value = self.getPortalObject().\ plugin_path=plugin_path)
portal_memcached.getMemcachedDict(key_prefix='selection_tool', container = MemcachedContainer(value)
plugin_path=plugin_path) else:
setattr(self, '_v_selection_data', value) if getattr(aq_base(self), 'selection_data', None) is None:
return value self.selection_data = PersistentMapping()
value = self.selection_data
def _getPersistentContainer(self, user_id): container = PersistentMappingContainer(value)
if getattr(aq_base(self), 'selection_data', None) is None: setattr(self, container_id, container)
self.selection_data = PersistentMapping() return container
if not self.selection_data.has_key(user_id):
self.selection_data[user_id] = SelectionPersistentMapping()
return self.selection_data[user_id]
InitializeClass( SelectionTool ) InitializeClass( SelectionTool )
class MemcachedContainer(object):
def __init__(self, container):
self._container = container
def getSelectionNameList(self, user_id):
return []
def getSelection(self, user_id, selection_name):
try:
return self._container.get('%s-%s' % (user_id, selection_name))
except KeyError:
return None
def setSelection(self, user_id, selection_name, selection):
self._container.set('%s-%s' % (user_id, selection_name), aq_base(selection))
def deleteSelection(self, user_id, selection_name):
del(self._container['%s-%s' % (user_id, selection_name)])
def deleteGlobalSelection(self, user_id, selection_name):
pass
class PersistentMappingContainer(object):
def __init__(self, container):
self._container = container
def getSelectionNameList(self, user_id):
try:
return self._container[user_id].keys()
except KeyError:
return []
def getSelection(self, user_id, selection_name):
try:
return self._container[user_id][selection_name]
except KeyError:
return None
def setSelection(self, user_id, selection_name, selection):
try:
user_container = self._container[user_id]
except KeyError:
user_container = SelectionPersistentMapping()
self._container[user_id] = user_container
user_container[selection_name] = aq_base(selection)
def deleteSelection(self, user_id, selection_name):
try:
user_container = self._container[user_id]
del(user_container[selection_name])
except KeyError:
pass
def deleteGlobalSelection(self, user_id, selection_name):
for user_container in self._container.itervalues():
try:
del(user_container[selection_name])
except KeyError:
pass
class SelectionPersistentMapping(PersistentMapping): class SelectionPersistentMapping(PersistentMapping):
"""A conflict-free PersistentMapping. """A conflict-free PersistentMapping.
......
...@@ -61,10 +61,11 @@ class TestSelectionTool(ERP5TypeTestCase): ...@@ -61,10 +61,11 @@ class TestSelectionTool(ERP5TypeTestCase):
self.portal_selections.getSelectionNameList()) self.portal_selections.getSelectionNameList())
self.assertEquals(['test_selection'], self.assertEquals(['test_selection'],
self.portal_selections.getSelectionNames()) self.portal_selections.getSelectionNames())
self.assert_(self.portal_selections._getPersistentContainer('manager') self.assert_(self.portal_selections._getContainer() is not None)
is not None)
self.assert_(getattr(self.portal_selections, 'selection_data', None) self.assert_(getattr(self.portal_selections, 'selection_data', None)
is not None) is not None)
self.assert_(getattr(self.portal_selections, '_v_selection_container', None)
is not None)
def testGetSelectionFor(self): def testGetSelectionFor(self):
selection = self.portal_selections.getSelectionFor('test_selection') selection = self.portal_selections.getSelectionFor('test_selection')
...@@ -374,8 +375,8 @@ class TestSelectionToolMemcachedStorage(TestSelectionTool): ...@@ -374,8 +375,8 @@ class TestSelectionToolMemcachedStorage(TestSelectionTool):
self.portal_selections.getSelectionNameList()) self.portal_selections.getSelectionNameList())
self.assertEquals([], self.assertEquals([],
self.portal_selections.getSelectionNames()) self.portal_selections.getSelectionNames())
self.assert_(self.portal_selections._getMemcachedContainer() is not None) self.assert_(self.portal_selections._getContainer() is not None)
self.assert_(getattr(self.portal_selections, '_v_selection_data', None) self.assert_(getattr(self.portal_selections, '_v_selection_container', None)
is not None) is not None)
@skip('To be decided if implementation is required') @skip('To be decided if implementation is required')
......
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