summaryrefslogtreecommitdiff
path: root/odoo/tools/misc.py
diff options
context:
space:
mode:
authorRaphael Collet <rco@odoo.com>2019-01-29 16:32:14 +0000
committerMartin Trigaux <mat@odoo.com>2019-08-21 13:47:17 +0000
commit4cd093a0013752ddb140fe0f0f8928bc001543f6 (patch)
tree0b53d4ddc5071123add7001619777c421437f233 /odoo/tools/misc.py
parentebaa132d5d1e93e122a645bcf9a8eeff1b478b0a (diff)
[IMP] fields: extend selection with a given order
When extended with the selection_add parameter, a singleton (value,) where `value` appears in the overriden selection, can be used. The new values are inserted in an order that is consistent with the overridden selection and this list. selection = [('a', 'A'), ('b', 'B')] selection_add = [('c', 'C'), ('b',)] > result = [('a', 'A'), ('c', 'C'), ('b', 'B')]
Diffstat (limited to 'odoo/tools/misc.py')
-rw-r--r--odoo/tools/misc.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/odoo/tools/misc.py b/odoo/tools/misc.py
index 14e207d7db1..3139cc1b526 100644
--- a/odoo/tools/misc.py
+++ b/odoo/tools/misc.py
@@ -349,6 +349,35 @@ def topological_sort(elems):
return result
+def merge_sequences(*iterables):
+ """ Merge several iterables into a list. The result is the union of the
+ iterables, ordered following the partial order given by the iterables,
+ with a bias towards the end for the last iterable::
+
+ seq = merge_sequences(['A', 'B', 'C'])
+ assert seq == ['A', 'B', 'C']
+
+ seq = merge_sequences(
+ ['A', 'B', 'C'],
+ ['Z'], # 'Z' can be anywhere
+ ['Y', 'C'], # 'Y' must precede 'C';
+ ['A', 'X', 'Y'], # 'X' must follow 'A' and precede 'Y'
+ )
+ assert seq == ['A', 'B', 'X', 'Y', 'C', 'Z']
+ """
+ # we use an OrderedDict to keep elements in order by default
+ deps = OrderedDict() # {item: elems_before_item}
+ for iterable in iterables:
+ prev = None
+ for index, item in enumerate(iterable):
+ if not index:
+ deps.setdefault(item, [])
+ else:
+ deps.setdefault(item, []).append(prev)
+ prev = item
+ return topological_sort(deps)
+
+
try:
import xlwt