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> ...@@ -86,7 +86,7 @@ to set categories</td></tr>
<tr> <tr>
<td>verifyValue</td> <td>verifyValue</td>
<td>field_my_bar_category_title_list</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> <td></td>
</tr> </tr>
<tr> <tr>
...@@ -104,7 +104,7 @@ to set categories</td></tr> ...@@ -104,7 +104,7 @@ to set categories</td></tr>
<tr> <tr>
<td>verifyValue</td> <td>verifyValue</td>
<td>field_my_bar_category_title_list</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> <td></td>
</tr> </tr>
<tr> <tr>
...@@ -122,7 +122,7 @@ to set categories</td></tr> ...@@ -122,7 +122,7 @@ to set categories</td></tr>
<tr> <tr>
<td>verifyValue</td> <td>verifyValue</td>
<td>field_my_bar_category_title_list</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> <td></td>
</tr> </tr>
<!-- XXX don't test jump for now, because we don't have good locators anyway. <!-- XXX don't test jump for now, because we don't have good locators anyway.
......
...@@ -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):
......
...@@ -639,12 +639,12 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor): ...@@ -639,12 +639,12 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
person.setRegionValue(alpha) person.setRegionValue(alpha)
self.assertEqual(person.getRegion(), 'alpha') self.assertEqual(person.getRegion(), 'alpha')
person.setRegionValueList([alpha, alpha]) person.setRegionValueList([alpha, alpha])
self.assertEqual(person.getRegionList(), ['alpha', 'alpha']) self.assertEqual(person.getRegionList(), ['alpha'])
person.setRegionValueSet([alpha, alpha]) person.setRegionValueSet([alpha, alpha])
self.assertEqual(person.getRegionList(), ['alpha']) self.assertEqual(person.getRegionList(), ['alpha'])
self.assertEqual(person.getRegionSet(), ['alpha']) self.assertEqual(person.getRegionSet(), ['alpha'])
person.setRegionValueList([alpha, beta, alpha]) person.setRegionValueList([alpha, beta, alpha])
self.assertEqual(person.getRegionList(), ['alpha', 'beta', 'alpha']) self.assertEqual(person.getRegionList(), ['alpha', 'beta'])
person.setRegionValueSet([alpha, beta, alpha]) person.setRegionValueSet([alpha, beta, alpha])
result = person.getRegionSet() result = person.getRegionSet()
result.sort() result.sort()
...@@ -677,12 +677,12 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor): ...@@ -677,12 +677,12 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
person.setRegion('alpha') person.setRegion('alpha')
self.assertEqual(person.getRegion(), 'alpha') self.assertEqual(person.getRegion(), 'alpha')
person.setRegionList(['alpha', 'alpha']) person.setRegionList(['alpha', 'alpha'])
self.assertEqual(person.getRegionList(), ['alpha', 'alpha']) self.assertEqual(person.getRegionList(), ['alpha'])
self.assertEqual(person.getRegionSet(), ['alpha']) self.assertEqual(person.getRegionSet(), ['alpha'])
person.setRegionSet(['beta', 'alpha', 'alpha']) person.setRegionSet(['beta', 'alpha', 'alpha'])
self.assertEqual(person.getRegionList(), ['alpha', 'beta']) self.assertEqual(person.getRegionList(), ['alpha', 'beta'])
person.setRegionList(['beta', 'alpha', 'alpha']) 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 # at this point the person have a default region set to the first item in
# the list. # the list.
self.assertEqual(person.getDefaultRegion(), 'beta') self.assertEqual(person.getDefaultRegion(), 'beta')
...@@ -717,11 +717,11 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor): ...@@ -717,11 +717,11 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
person.setRegionUid(alpha.getUid()) person.setRegionUid(alpha.getUid())
self.assertEqual(person.getRegion(), 'alpha') self.assertEqual(person.getRegion(), 'alpha')
person.setRegionUidList([beta.getUid(), beta.getUid()]) person.setRegionUidList([beta.getUid(), beta.getUid()])
self.assertEqual(person.getRegionList(), ['beta', 'beta']) self.assertEqual(person.getRegionList(), ['beta'])
person.setRegionUidSet([alpha.getUid(), alpha.getUid()]) person.setRegionUidSet([alpha.getUid(), alpha.getUid()])
self.assertEqual(person.getRegionList(), ['alpha']) self.assertEqual(person.getRegionList(), ['alpha'])
person.setRegionUidList([alpha.getUid(), beta.getUid(), alpha.getUid()]) 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()]) person.setRegionUidSet([alpha.getUid(), beta.getUid(), alpha.getUid()])
self.assertEqual(sorted(person.getRegionSet()), ['alpha', 'beta']) self.assertEqual(sorted(person.getRegionSet()), ['alpha', 'beta'])
person.setDefaultRegionUid(beta.getUid()) 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