Commit 3985f158 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

CMFCategory: keep only the first occurrence of each category while preserving order.

parent c3f1131f
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
"""\ """\
ERP portal_categories tool. ERP portal_categories tool.
""" """
from collections import deque from collections import deque, OrderedDict
import re import re
from BTrees.OOBTree import OOTreeSet from BTrees.OOBTree import OOTreeSet
from Products.ERP5Type.Globals import InitializeClass, DTMLFile from Products.ERP5Type.Globals import InitializeClass, DTMLFile
...@@ -1189,7 +1189,8 @@ class CategoryTool(BaseTool): ...@@ -1189,7 +1189,8 @@ class CategoryTool(BaseTool):
# note that we don't want to cast as set at this point to keep ordering (and duplicates). # note that we don't want to cast as set at this point to keep ordering (and duplicates).
value = [x for x in tuple(value) if x != relative_url] value = [x for x in tuple(value) if x != relative_url]
context.categories = value = tuple(value) # Keep only the first occurrence of each category while preserving order.
context.categories = value = tuple(OrderedDict.fromkeys(value))
if context.isTempDocument(): if context.isTempDocument():
return return
......
...@@ -221,6 +221,14 @@ class TestCMFCategory(ERP5TypeTestCase): ...@@ -221,6 +221,14 @@ class TestCMFCategory(ERP5TypeTestCase):
p1.setRegionList(region_list) p1.setRegionList(region_list)
self.assertEqual(p1.getRegion(), None) self.assertEqual(p1.getRegion(), None)
region_list = [self.region1, self.region2, self.region1]
p1.setRegionList(region_list)
self.assertEqual(p1.getRegionList(), self.region_list)
p1.setRegionValueList([region_value_list[0],
region_value_list[1],
region_value_list[0]])
self.assertEqual(p1.getRegionList(), self.region_list)
def test_03_CategoryValue(self): def test_03_CategoryValue(self):
# Test if we can get categories values # Test if we can get categories values
region_value = self.portal.portal_categories.resolveCategory('region/%s' % self.region1) region_value = self.portal.portal_categories.resolveCategory('region/%s' % self.region1)
...@@ -1251,13 +1259,13 @@ class TestCMFCategory(ERP5TypeTestCase): ...@@ -1251,13 +1259,13 @@ class TestCMFCategory(ERP5TypeTestCase):
def _set(*args, **kw): def _set(*args, **kw):
return category_tool._setCategoryMembership(person, *args, **kw) return category_tool._setCategoryMembership(person, *args, **kw)
_set(bc.id, list('aa')) _set(bc.id, list('aa'))
self.assertEqual(get(bc.id), list('aa')) self.assertEqual(get(bc.id), list('a'))
_set(bc.id, list('baa')) _set(bc.id, list('baa'))
self.assertEqual(get(bc.id), list('aba')) self.assertEqual(get(bc.id), list('ab'))
_set(bc.id, map(base, 'bb'), 1) _set(bc.id, map(base, 'bb'), 1)
self.assertEqual(get(bc.id), list('bb')) self.assertEqual(get(bc.id), list('b'))
_set(bc.id, map(base, 'abb'), 1) _set(bc.id, map(base, 'abb'), 1)
self.assertEqual(get(bc.id), list('bab')) self.assertEqual(get(bc.id), list('ba'))
_set(bc.id, ()) _set(bc.id, ())
def test_relatedIndex(self): def test_relatedIndex(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