Reviewers: ,


Please review this at http://codereview.tryton.org/256001/

Affected files:
  M trytond/model/__init__.py
  A trytond/model/workflow.py


Index: trytond/model/__init__.py
===================================================================

--- a/trytond/model/__init__.py
+++ b/trytond/model/__init__.py
@@ -6,3 +6,4 @@
 from .modelsingleton import *
 from .modelsql import *
 from .modelworkflow import *
+from .workflow import Workflow

Index: trytond/model/workflow.py
===================================================================
new file mode 100644

--- /dev/null
+++ b/trytond/model/workflow.py
@@ -0,0 +1,63 @@
+#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 functools import wraps
+from collections import defaultdict
+from ..transaction import Transaction
+from ..pool import Pool
+
+
+class Workflow(object):
+    """
+    Mix-in class to handle transition check.
+    """
+    _transition_state = 'state'
+
+    def __init__(self):
+        super(Workflow, self).__init__()
+        self._transitions = defaultdict(set)
+
+    @staticmethod
+    def transition(state):
+        def check_transition(func):
+            @wraps(func)
+            def wrapper(self, ids, *args, **kwargs):
+                pool = Pool()
+                user_obj = pool.get('res.user')
+                model_data_obj = pool.get('ir.model.data')
+
+                records = self.browse(ids)
+                filtered = []
+                to_update = []
+
+                transitions = dict((key,
+                        set(model_data_obj.get_id(*group.split('.'))
+                            for group in groups))
+                    for key, groups in self._transitions.iteritems())
+
+                if Transaction().user != 0:
+                    groups = set(user_obj.get_groups())
+                else:
+                    groups = None
+
+                for record in records:
+                    current_state = getattr(record, self._transition_state)
+                    transition = (current_state, state)
+                    if transition in transitions:
+                        if groups and transitions[transition]:
+                            if groups & transitions[transition]:
+                                filtered.append(record.id)
+                                if current_state != state:
+                                    to_update.append(record.id)
+                        else:
+                            filtered.append(record.id)
+                            if current_state != state:
+                                to_update.append(record.id)
+
+                result = func(self, filtered, *args, **kwargs)
+                if to_update:
+                    self.write(to_update, {
+                            self._transition_state: state,
+                            })
+                return result
+            return wrapper
+        return check_transition



--
[email protected] mailing list

Reply via email to