Commit 3e2507c7 authored by Sebastien Robin's avatar Sebastien Robin

CMFCategory: review usage of current_category and rename it current_category_list

Since the beginning, there was in getCategoryChildItemList Renderer
API the possibility to pass a parameter 'current_category'. The idea
was to allow displaying a category which is not part of what the item
list will return (typically, if you expire a category, and if you check
permission, the category will not be in item list any more).

But the way it was implemented was too inconvenient and it looks never
used. So improve several things:
- possibility to pass multiple categories for multi value cases
- reuse renderer to render current categories instead of just reinjecting
  the url (which is not user friendly)
- implement handling of current_category_list in getCategoryChildItemList
  to allow caching the result of the Renderer. The only part which is not
  cached is the rendering of current categories.
parent bdcdc229
......@@ -557,7 +557,8 @@ class Category(Folder):
security.declareProtected(Permissions.AccessContentsInformation,
'getCategoryChildItemList')
def getCategoryChildItemList(self, recursive=1, base=0,
cache=DEFAULT_CACHE_FACTORY, **kw):
cache=DEFAULT_CACHE_FACTORY,
current_category_list=None, **kw):
"""
Returns a list of tuples by parsing recursively all categories in a
given list of base categories. Each tuple contains::
......@@ -575,6 +576,10 @@ class Category(Folder):
recursive -- if set to 0 do not apply recursively
current_category_list -- allows to provide of categories which is not part of the
default ItemList. Very useful for displaying values in a popup
menu which can no longer be selected.
All parameters supported by getCategoryChildValueList and Render are
supported here.
"""
......@@ -583,25 +588,40 @@ class Category(Folder):
return Renderer(base=base, **kw).render(value_list)
if not cache:
return _renderCategoryChildItemList(
item_list = _renderCategoryChildItemList(
recursive=recursive, base=base, **kw)
else:
# If checked_permission is specified, we include the username in the
# cache key
username = None
if 'checked_permission' in kw:
username = str(getSecurityManager().getUser())
# Some methods are language dependent so we include the language in the
# key
localizer = getToolByName(self, 'Localizer')
language = localizer.get_selected_language()
m = CachingMethod(_renderCategoryChildItemList,
('Category_getCategoryChildItemList', language,
self.getPath(), username),
cache_factory=cache)
return m(recursive=recursive, base=base, **kw)
# If checked_permission is specified, we include the username in the
# cache key
username = None
if 'checked_permission' in kw:
username = str(getSecurityManager().getUser())
# Some methods are language dependent so we include the language in the
# key
localizer = getToolByName(self, 'Localizer')
language = localizer.get_selected_language()
m = CachingMethod(_renderCategoryChildItemList,
('Category_getCategoryChildItemList', language,
self.getPath(), username),
cache_factory=cache)
item_list = m(recursive=recursive, base=base, **kw)
if current_category_list:
current_kw = kw.copy()
current_kw['display_none_category'] = False
current_category_item_list = Renderer(base=base, **current_kw).render(
[self.unrestrictedTraverse(x) for x in current_category_list])
item_set = set([tuple(x) for x in item_list])
additional_item_list = []
for current_category_item in current_category_item_list:
if not(tuple(current_category_item) in item_set):
additional_item_list.append(current_category_item)
if additional_item_list:
item_list = item_list + additional_item_list
return item_list
# Alias for compatibility
security.declareProtected(Permissions.View, 'getFormItemList')
......
......@@ -47,7 +47,7 @@ class Renderer(Filter):
is_right_display = 0, translate_display = 0,
translatation_domain = None, display_base_category = 0,
base_category = None, base = 1,
display_none_category = 1, current_category = None,**kw):
display_none_category = 1, **kw):
"""
- *display_id*: the id of attribute to "call" to calculate the value to display
(getProperty(display_id) -> getDisplayId)
......@@ -91,16 +91,9 @@ class Renderer(Filter):
- *is_self_excluded*: allows to exclude this category from the displayed list
- *current_category*: allows to provide a category which is not part of the
default ItemList. Very useful for displaying
values in a popup menu which can no longer
be selected.
- *display_none_category*: allows to include an empty value. Very useful
to define None values or empty lists through
popup widgets. If both has_empty_item and
current_category are provided, current_category
is displayed first.
popup widgets.
"""
......@@ -118,7 +111,6 @@ class Renderer(Filter):
self.base_category = base_category
self.base = base
self.display_none_category = display_none_category
self.current_category = current_category
def getObjectList(self, value_list):
new_value_list = []
......@@ -161,12 +153,6 @@ class Renderer(Filter):
# Initialize the list of items.
item_list = []
if self.current_category:
if self.is_right_display:
item = [None, self.current_category]
else:
item = [self.current_category, None]
item_list.append(item)
if self.display_none_category:
item = ['', '']
item_list.append(item)
......
......@@ -882,6 +882,11 @@ class TestCMFCategory(ERP5TypeTestCase):
[['', ''], ['A', '1'], ['B', '2'], ['B1', '2/21']],
bc.getCategoryChildTitleItemList(checked_permission=checked_permission,
cache=0))
self.assertEqual(
[['', ''], ['A', '1'], ['B', '2'], ['B1', '2/21']],
bc.getCategoryChildTitleItemList(checked_permission=checked_permission,
current_category_list=['2/21', '1'],
cache=0))
b.manage_permission(checked_permission, roles=[], acquire=0)
......@@ -906,6 +911,29 @@ class TestCMFCategory(ERP5TypeTestCase):
[['', ''], ['A', '1']],
bc.getCategoryChildTitleItemList(checked_permission=checked_permission,
cache=0))
# Verify that current_category_list parameter allows to display again
# hidden values
self.assertEqual(
[['', ''], ['A', '1'], ['B1', '2/21']],
bc.getCategoryChildTitleItemList(checked_permission=checked_permission,
current_category_list=['2/21'],
cache=0))
a.manage_permission(checked_permission, roles=[], acquire=0)
self.assertEqual(
[['', '']],
bc.getCategoryChildTitleItemList(checked_permission=checked_permission,
cache=0))
self.assertEqual(
[['', ''], ['B1', '2/21']],
bc.getCategoryChildTitleItemList(checked_permission=checked_permission,
current_category_list=['2/21'],
cache=0))
self.assertEqual(
[['', ''], ['A', '1'], ['B1', '2/21']],
bc.getCategoryChildTitleItemList(checked_permission=checked_permission,
current_category_list=['1', '2/21'],
cache=0))
def test_28b_getCategoryChildItemList_checked_permission_cache(self):
# getCategoryChildTitleItemList take into account user
......
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