Files
tryton/trytond/model/workflow.py
T
Cédric Krier 510931a8ab Use OrderedDict in Workflow.transition for cache alignment
This allows to loop over the key in the same order as the list of records.
Also avoid to use .keys() when iterating on dict because it generate a useless
list on Python2.
Use list on dict instead of .keys() to avoid one extra call on Python3 because
.key() return an iterator.

issue5396
review20061002
2016-03-19 11:59:52 +01:00

46 lines
1.6 KiB
Python

# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from collections import OrderedDict
from functools import wraps
class Workflow(object):
"""
Mix-in class to handle transition check.
"""
_transition_state = 'state'
@classmethod
def __setup__(cls):
super(Workflow, cls).__setup__()
cls._transitions = set()
@staticmethod
def transition(state):
def check_transition(func):
@wraps(func)
def wrapper(cls, records, *args, **kwargs):
filtered = []
to_update = OrderedDict()
for record in records:
current_state = getattr(record, cls._transition_state)
transition = (current_state, state)
if transition in cls._transitions:
filtered.append(record)
if current_state != state:
to_update[record] = current_state
result = func(cls, filtered, *args, **kwargs)
if to_update:
for record in to_update:
current_state = getattr(record, cls._transition_state)
if current_state != to_update[record]:
del to_update[record]
cls.write(list(to_update), {
cls._transition_state: state,
})
return result
return wrapper
return check_transition