Files
tryton/trytond/model/workflow.py
T

45 lines
1.6 KiB
Python
Raw Normal View History

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.
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.
"""
_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 = []
to_update = OrderedDict()
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:
2016-03-19 15:25:49 +01:00
for record in 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]
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