2014-12-02 12:54:01 +01:00
|
|
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
|
|
|
|
# this repository contains the full copyright notices and license terms.
|
2016-03-19 11:59:52 +01:00
|
|
|
from collections import OrderedDict
|
2012-03-25 00:15:17 +01:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Workflow(object):
|
|
|
|
|
"""
|
|
|
|
|
Mix-in class to handle transition check.
|
|
|
|
|
"""
|
2019-09-07 23:45:08 +02:00
|
|
|
__slots__ = ()
|
2012-03-25 00:15:17 +01:00
|
|
|
_transition_state = 'state'
|
|
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
@classmethod
|
|
|
|
|
def __setup__(cls):
|
|
|
|
|
super(Workflow, cls).__setup__()
|
|
|
|
|
cls._transitions = set()
|
2012-03-25 00:15:17 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def transition(state):
|
|
|
|
|
def check_transition(func):
|
|
|
|
|
@wraps(func)
|
2012-09-24 22:16:53 +02:00
|
|
|
def wrapper(cls, records, *args, **kwargs):
|
2012-03-25 00:15:17 +01:00
|
|
|
filtered = []
|
2016-03-19 11:59:52 +01:00
|
|
|
to_update = OrderedDict()
|
2012-03-25 00:15:17 +01:00
|
|
|
|
2017-07-16 23:49:15 +02:00
|
|
|
assert len(records) == len(set(records)), "Duplicate records"
|
|
|
|
|
|
2012-03-25 00:15:17 +01:00
|
|
|
for record in records:
|
2012-09-24 22:16:53 +02:00
|
|
|
current_state = getattr(record, cls._transition_state)
|
2012-03-25 00:15:17 +01:00
|
|
|
transition = (current_state, state)
|
2012-09-24 22:16:53 +02:00
|
|
|
if transition in cls._transitions:
|
|
|
|
|
filtered.append(record)
|
2012-03-25 00:15:17 +01:00
|
|
|
if current_state != state:
|
2012-09-24 22:16:53 +02:00
|
|
|
to_update[record] = current_state
|
2012-03-25 00:15:17 +01:00
|
|
|
|
2012-09-24 22:16:53 +02:00
|
|
|
result = func(cls, filtered, *args, **kwargs)
|
2012-03-25 00:15:17 +01:00
|
|
|
if to_update:
|
2018-06-26 19:11:19 +02:00
|
|
|
for record in list(to_update.keys()):
|
2012-09-24 22:16:53 +02:00
|
|
|
current_state = getattr(record, cls._transition_state)
|
|
|
|
|
if current_state != to_update[record]:
|
|
|
|
|
del to_update[record]
|
2016-03-19 11:59:52 +01:00
|
|
|
cls.write(list(to_update), {
|
2012-09-24 22:16:53 +02:00
|
|
|
cls._transition_state: state,
|
2012-03-25 00:15:17 +01:00
|
|
|
})
|
|
|
|
|
return result
|
|
|
|
|
return wrapper
|
|
|
|
|
return check_transition
|
2018-09-01 01:02:04 +02:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def copy(cls, records, default=None):
|
|
|
|
|
if default is None:
|
|
|
|
|
default = {}
|
|
|
|
|
else:
|
|
|
|
|
default = default.copy()
|
|
|
|
|
default.setdefault(
|
|
|
|
|
cls._transition_state, cls._defaults[cls._transition_state]())
|
|
|
|
|
return super().copy(records, default=default)
|