Commit e38f80f1 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki Committed by Julien Muchembled

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

parent 54b2d7a6
......@@ -86,7 +86,7 @@ to set categories</td></tr>
<tr>
<td>verifyValue</td>
<td>field_my_bar_category_title_list</td>
<td>Title 0<br />Title 1<br />Title 1</td>
<td>Title 0<br />Title 1</td>
<td></td>
</tr>
<tr>
......@@ -104,7 +104,7 @@ to set categories</td></tr>
<tr>
<td>verifyValue</td>
<td>field_my_bar_category_title_list</td>
<td>Title 0<br />Title 0<br />Title 1<br />Title 1</td>
<td>Title 0<br />Title 0<br />Title 1</td>
<td></td>
</tr>
<tr>
......@@ -122,7 +122,7 @@ to set categories</td></tr>
<tr>
<td>verifyValue</td>
<td>field_my_bar_category_title_list</td>
<td>Title 1<br />Title 0<br />Title 1</td>
<td>Title 1<br />Title 0</td>
<td></td>
</tr>
<!-- XXX don't test jump for now, because we don't have good locators anyway.
......
......@@ -30,7 +30,7 @@
"""\
ERP portal_categories tool.
"""
from collections import deque
from collections import deque, OrderedDict
import re
from BTrees.OOBTree import OOTreeSet
from Products.ERP5Type.Globals import InitializeClass, DTMLFile
......@@ -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).
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():
return
......
......@@ -221,6 +221,14 @@ class TestCMFCategory(ERP5TypeTestCase):
p1.setRegionList(region_list)
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):
# Test if we can get categories values
region_value = self.portal.portal_categories.resolveCategory('region/%s' % self.region1)
......@@ -1251,13 +1259,13 @@ class TestCMFCategory(ERP5TypeTestCase):
def _set(*args, **kw):
return category_tool._setCategoryMembership(person, *args, **kw)
_set(bc.id, list('aa'))
self.assertEqual(get(bc.id), list('aa'))
self.assertEqual(get(bc.id), list('a'))
_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)
self.assertEqual(get(bc.id), list('bb'))
self.assertEqual(get(bc.id), list('b'))
_set(bc.id, map(base, 'abb'), 1)
self.assertEqual(get(bc.id), list('bab'))
self.assertEqual(get(bc.id), list('ba'))
_set(bc.id, ())
def test_relatedIndex(self):
......
......@@ -639,12 +639,12 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
person.setRegionValue(alpha)
self.assertEqual(person.getRegion(), 'alpha')
person.setRegionValueList([alpha, alpha])
self.assertEqual(person.getRegionList(), ['alpha', 'alpha'])
self.assertEqual(person.getRegionList(), ['alpha'])
person.setRegionValueSet([alpha, alpha])
self.assertEqual(person.getRegionList(), ['alpha'])
self.assertEqual(person.getRegionSet(), ['alpha'])
person.setRegionValueList([alpha, beta, alpha])
self.assertEqual(person.getRegionList(), ['alpha', 'beta', 'alpha'])
self.assertEqual(person.getRegionList(), ['alpha', 'beta'])
person.setRegionValueSet([alpha, beta, alpha])
result = person.getRegionSet()
result.sort()
......@@ -677,12 +677,12 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
person.setRegion('alpha')
self.assertEqual(person.getRegion(), 'alpha')
person.setRegionList(['alpha', 'alpha'])
self.assertEqual(person.getRegionList(), ['alpha', 'alpha'])
self.assertEqual(person.getRegionList(), ['alpha'])
self.assertEqual(person.getRegionSet(), ['alpha'])
person.setRegionSet(['beta', 'alpha', 'alpha'])
self.assertEqual(person.getRegionList(), ['alpha', 'beta'])
person.setRegionList(['beta', 'alpha', 'alpha'])
self.assertEqual(person.getRegionList(), ['beta', 'alpha', 'alpha'])
self.assertEqual(person.getRegionList(), ['beta', 'alpha'])
# at this point the person have a default region set to the first item in
# the list.
self.assertEqual(person.getDefaultRegion(), 'beta')
......@@ -717,11 +717,11 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
person.setRegionUid(alpha.getUid())
self.assertEqual(person.getRegion(), 'alpha')
person.setRegionUidList([beta.getUid(), beta.getUid()])
self.assertEqual(person.getRegionList(), ['beta', 'beta'])
self.assertEqual(person.getRegionList(), ['beta'])
person.setRegionUidSet([alpha.getUid(), alpha.getUid()])
self.assertEqual(person.getRegionList(), ['alpha'])
person.setRegionUidList([alpha.getUid(), beta.getUid(), alpha.getUid()])
self.assertEqual(person.getRegionList(), ['alpha', 'beta', 'alpha'])
self.assertEqual(person.getRegionList(), ['alpha', 'beta'])
person.setRegionUidSet([alpha.getUid(), beta.getUid(), alpha.getUid()])
self.assertEqual(sorted(person.getRegionSet()), ['alpha', 'beta'])
person.setDefaultRegionUid(beta.getUid())
......
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