Commit 201caf82 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki Committed by Jérome Perrin

ERP5Type: set Setter guarantees the order with just removing the duplicates.

parent 00311942
...@@ -740,7 +740,7 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor): ...@@ -740,7 +740,7 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
# the list. # the list.
self.assertEqual(person.getDefaultRegion(), 'beta') self.assertEqual(person.getDefaultRegion(), 'beta')
person.setRegionSet(['alpha', 'beta', 'alpha']) person.setRegionSet(['alpha', 'beta', 'alpha'])
self.assertEqual(person.getRegionList(), ['beta', 'alpha']) self.assertEqual(sorted(person.getRegionList()), ['alpha', 'beta'])
# calling a set setter did not change the default region # calling a set setter did not change the default region
self.assertEqual(person.getDefaultRegion(), 'beta') self.assertEqual(person.getDefaultRegion(), 'beta')
...@@ -2185,6 +2185,10 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor): ...@@ -2185,6 +2185,10 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
checked_permission=checked_permission) checked_permission=checked_permission)
self.assertSameSet([beta_path, gamma_path], foo.getRegionList()) self.assertSameSet([beta_path, gamma_path], foo.getRegionList())
foo.setRegionList([beta_path])
foo.setRegionSet([gamma_path, beta_path])
self.assertEqual(foo.getRegionList(), [beta_path, gamma_path])
foo.setRegionValue(None) foo.setRegionValue(None)
self.assertEqual(None, foo.getRegion()) self.assertEqual(None, foo.getRegion())
# Check setCategoryValueSet accessor # Check setCategoryValueSet accessor
...@@ -2199,6 +2203,10 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor): ...@@ -2199,6 +2203,10 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
checked_permission=checked_permission) checked_permission=checked_permission)
self.assertSameSet([beta_path, gamma_path], foo.getRegionList()) self.assertSameSet([beta_path, gamma_path], foo.getRegionList())
foo.setRegionValueList([beta])
foo.setRegionValueSet([gamma, beta])
self.assertEqual(foo.getRegionValueList(), [beta, gamma])
# check hasCategory accessors # check hasCategory accessors
foo.setRegionValue(None) foo.setRegionValue(None)
self.assertEqual(None, foo.getRegion()) self.assertEqual(None, foo.getRegion())
...@@ -2406,12 +2414,23 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor): ...@@ -2406,12 +2414,23 @@ class TestERP5Type(PropertySheetTestCase, LogInterceptor):
person.setDummyList(['a', 'b']) person.setDummyList(['a', 'b'])
self.assertEqual(person.getDummy(), 'a') self.assertEqual(person.getDummy(), 'a')
self.assertEqual(person.getDummyList(), ['a', 'b']) self.assertEqual(person.getDummyList(), ['a', 'b'])
self.assertEqual(person.getDummySet(), ['a', 'b']) self.assertEqual(sorted(person.getDummySet()), ['a', 'b'])
person.setDummySet(['b', 'a', 'c'])
self.assertEqual(person.getDummy(), 'a')
self.assertEqual(sorted(person.getDummyList()), ['a', 'b', 'c'])
person.setDummySet(['b', 'c'])
self.assertEqual(sorted(person.getDummyList()), ['b', 'c'])
person.setDummyList(['a', 'b', 'b'])
self.assertEqual(person.getDummy(), 'a')
self.assertEqual(person.getDummyList(), ['a', 'b', 'b'])
self.assertEqual(sorted(person.getDummySet()), ['a', 'b'])
person.setDummy('value') person.setDummy('value')
self.assertEqual(person.getDummy(), 'value') self.assertEqual(person.getDummy(), 'value')
self.assertEqual(person.getDummyList(), ['value']) self.assertEqual(person.getDummyList(), ['value'])
self.assertEqual(person.getDummySet(), ['value']) self.assertEqual(sorted(person.getDummySet()), ['value'])
def test_translated_accessors(self): def test_translated_accessors(self):
self._addProperty('Person', self._addProperty('Person',
......
...@@ -28,6 +28,7 @@ from __future__ import absolute_import ...@@ -28,6 +28,7 @@ from __future__ import absolute_import
############################################################################## ##############################################################################
from collections import OrderedDict
from .Base import func_code, type_definition, list_types,\ from .Base import func_code, type_definition, list_types,\
ATTRIBUTE_PREFIX, Method, evaluateTales ATTRIBUTE_PREFIX, Method, evaluateTales
from .TypeDefinition import asList, identity from .TypeDefinition import asList, identity
...@@ -80,8 +81,7 @@ class DefaultSetter(Base.Setter): ...@@ -80,8 +81,7 @@ class DefaultSetter(Base.Setter):
else: else:
if self._item_cast is not identity: if self._item_cast is not identity:
value = self._item_cast(value) value = self._item_cast(value)
list_value = set(getattr(instance, self._storage_id, ())) list_value = list(OrderedDict.fromkeys(e for e in getattr(instance, self._storage_id, ()) if e != value))
list_value.discard(value)
setattr(instance, self._storage_id, (value,) + tuple(list_value)) setattr(instance, self._storage_id, (value,) + tuple(list_value))
class ListSetter(DefaultSetter): class ListSetter(DefaultSetter):
...@@ -145,7 +145,7 @@ class SetSetter(Base.Setter): ...@@ -145,7 +145,7 @@ class SetSetter(Base.Setter):
if self._item_cast is not identity: if self._item_cast is not identity:
value = [self._item_cast(v) for v in value] value = [self._item_cast(v) for v in value]
if value: if value:
value = set(value) value = list(OrderedDict.fromkeys(value))
list_value = getattr(instance, self._storage_id, None) list_value = getattr(instance, self._storage_id, None)
if list_value: if list_value:
default_value = list_value[0] default_value = list_value[0]
......
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