Commit 31badf8f authored by Jérome Perrin's avatar Jérome Perrin

CMFCategory: new disable_{node,leave} API

This allow filtering category item lists so that user can only select nodes /
leaves in a way similar to filter_{node,leave} but instead of removing the
items, keep them with None as relative URL. This is something Formulator
understand, categories are displayed with disable attribute set on the select
option, which allows to visualize the category hierarchical structure, while
not allowing user to select {nodes,leaves}
parent e07696bf
...@@ -64,32 +64,26 @@ class Filter(Implicit): ...@@ -64,32 +64,26 @@ class Filter(Implicit):
self.filter_node = filter_node self.filter_node = filter_node
self.filter_leave = filter_leave self.filter_leave = filter_leave
def _isNode(self, context):
return bool(context.contentIds(filter={'portal_type' : 'Category'}))
def test(self, context): def test(self, context):
""" """
Test filter on a context Test filter on a context
""" """
#LOG('Filter test', 0, 'context = %s' % repr(context)) if self.filter_node and self._isNode(context):
is_node = None return False
if self.filter_node: if self.filter_leave and not self._isNode(context):
is_node = len(context.contentIds(filter={'portal_type' : 'Category'})) return False
if is_node:
return 0
if self.filter_leave:
if is_node is None:
# Only recalculate is_node if not already done
is_node = len(context.contentIds(filter={'portal_type' : 'Category'}))
if not is_node:
return 0
for k, v in self.filter_dict.items(): for k, v in self.filter_dict.items():
#LOG('Filter test', 0, "k = %s, v = %s" % (repr(k), repr(v)))
if type(v) in (type([]), type(())): if type(v) in (type([]), type(())):
if context.getProperty(k) not in v: if context.getProperty(k) not in v:
return 0 return False
elif context.getProperty(k) != v: elif context.getProperty(k) != v:
return 0 return False
if self.filter_method is not None: if self.filter_method is not None:
return self.filter_method(context) return self.filter_method(context)
return 1 return True
def asDict(self): def asDict(self):
......
...@@ -43,7 +43,8 @@ class Renderer(Filter): ...@@ -43,7 +43,8 @@ class Renderer(Filter):
def __init__(self, spec = None, filter = None, portal_type = None, def __init__(self, spec = None, filter = None, portal_type = None,
display_id = None, sort_id = None, display_id = None, sort_id = None,
display_method = None, sort_method = None, filter_method = None, display_method = None, sort_method = None, filter_method = None,
filter_node=0, filter_leave=0, filter_node=0, disable_node=0,
filter_leave=0, disable_leave=0,
is_right_display = 0, translate_display = 0, is_right_display = 0, translate_display = 0,
translatation_domain = None, display_base_category = 0, translatation_domain = None, display_base_category = 0,
base_category = None, base = 1, base_category = None, base = 1,
...@@ -58,8 +59,12 @@ class Renderer(Filter): ...@@ -58,8 +59,12 @@ class Renderer(Filter):
- *filter_node*: do not keep node categories - *filter_node*: do not keep node categories
- *disable_node*: return node categories as disabled (ie. None instead of their relative URL)
- *filter_leave*: do not keep leave categories - *filter_leave*: do not keep leave categories
- *disable_leave*: return leave categories as disabled (ie. None instead of their relative URL)
- *sort_id*: the id of the attribute to "call" to calculate the value used for sorting. - *sort_id*: the id of the attribute to "call" to calculate the value used for sorting.
Sorting is only applied to default ItemList items. Sorting is only applied to default ItemList items.
...@@ -99,7 +104,8 @@ class Renderer(Filter): ...@@ -99,7 +104,8 @@ class Renderer(Filter):
""" """
Filter.__init__(self, spec=spec, filter=filter, Filter.__init__(self, spec=spec, filter=filter,
portal_type=portal_type, filter_method=filter_method, portal_type=portal_type, filter_method=filter_method,
filter_node=filter_node, filter_leave=filter_leave) filter_node=filter_node and not disable_node,
filter_leave=filter_leave and not disable_leave)
self.display_id = display_id self.display_id = display_id
self.sort_id = sort_id self.sort_id = sort_id
self.display_method = display_method self.display_method = display_method
...@@ -111,6 +117,8 @@ class Renderer(Filter): ...@@ -111,6 +117,8 @@ class Renderer(Filter):
self.base_category = base_category self.base_category = base_category
self.base = base self.base = base
self.display_none_category = display_none_category self.display_none_category = display_none_category
self.disable_node = disable_node
self.disable_leave = disable_leave
def getObjectList(self, value_list): def getObjectList(self, value_list):
new_value_list = [] new_value_list = []
...@@ -222,6 +230,11 @@ class Renderer(Filter): ...@@ -222,6 +230,11 @@ class Renderer(Filter):
bc_title = getattr(bc, base_category_display_method_id)() bc_title = getattr(bc, base_category_display_method_id)()
label = '%s/%s' % (bc_title, label) label = '%s/%s' % (bc_title, label)
if self.disable_node and self._isNode(value):
url = None
if self.disable_leave and not self._isNode(value):
url = None
if self.is_right_display: if self.is_right_display:
item = [url, label] item = [url, label]
else: else:
......
...@@ -755,6 +755,58 @@ class TestCMFCategory(ERP5TypeTestCase): ...@@ -755,6 +755,58 @@ class TestCMFCategory(ERP5TypeTestCase):
whitespace_number = self.portal.portal_preferences.getPreferredWhitespaceNumberForChildItemIndentation() whitespace_number = self.portal.portal_preferences.getPreferredWhitespaceNumberForChildItemIndentation()
self.assertEqual(NBSP_UTF8 * whitespace_number + 'The Sub Title', sub_cat.getIndentedTitle()) self.assertEqual(NBSP_UTF8 * whitespace_number + 'The Sub Title', sub_cat.getIndentedTitle())
def test_CategoryChildTitleItemListFilterNodeFilterLeave(self):
base_cat = self.getCategoryTool().newContent(portal_type='Base Category')
base_cat.newContent(
portal_type='Category',
id='the_id',
title='The Title'
).newContent(
portal_type='Category',
id='the_sub_id',
title='The Sub Title')
self.assertEqual(
base_cat.getCategoryChildTitleItemList(filter_node=True),
[
['', ''],
['The Sub Title', 'the_id/the_sub_id'],
]
)
self.assertEqual(
base_cat.getCategoryChildTitleItemList(filter_leave=True),
[
['', ''],
['The Title', 'the_id'],
]
)
def test_CategoryChildTitleItemListDisableNodeDisableLeave(self):
base_cat = self.getCategoryTool().newContent(portal_type='Base Category')
base_cat.newContent(
portal_type='Category',
id='the_id',
title='The Title'
).newContent(
portal_type='Category',
id='the_sub_id',
title='The Sub Title')
self.assertEqual(
base_cat.getCategoryChildTitleItemList(disable_node=True),
[
['', ''],
['The Title', None],
['The Sub Title', 'the_id/the_sub_id'],
]
)
self.assertEqual(
base_cat.getCategoryChildTitleItemList(disable_leave=True),
[
['', ''],
['The Title', 'the_id'],
['The Sub Title', None],
]
)
def test_20_CategoryChildTitleAndIdItemList(self): def test_20_CategoryChildTitleAndIdItemList(self):
"""Tests getCategoryChildTitleAndIdItemList.""" """Tests getCategoryChildTitleAndIdItemList."""
base_cat = self.getCategoryTool().newContent(portal_type='Base Category') base_cat = self.getCategoryTool().newContent(portal_type='Base Category')
......
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