14 |
14 |
# You should have received a copy of the GNU General Public License
|
15 |
15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
16 |
16 |
|
|
17 |
import collections
|
|
18 |
|
17 |
19 |
from qommon.form import *
|
18 |
20 |
from wcs.roles import get_user_roles
|
19 |
21 |
from wcs.workflows import WorkflowStatusItem, register_item_class
|
20 |
22 |
|
|
23 |
|
|
24 |
class AutomaticDispatchRowWidget(CompositeWidget):
|
|
25 |
def __init__(self, name, value=None, **kwargs):
|
|
26 |
CompositeWidget.__init__(self, name, value, **kwargs)
|
|
27 |
if not value:
|
|
28 |
value = {}
|
|
29 |
self.add(StringWidget, name='value', title=_('Value'),
|
|
30 |
value=value.get('value'), **kwargs)
|
|
31 |
self.add(SingleSelectWidget, name='role_id', title=_('Role'),
|
|
32 |
value=value.get('role_id'),
|
|
33 |
options=[(None, '----', None)] + get_user_roles())
|
|
34 |
|
|
35 |
def _parse(self, request):
|
|
36 |
if self.get('value') or self.get('role_id'):
|
|
37 |
self.value = {
|
|
38 |
'value': self.get('value'),
|
|
39 |
'role_id': self.get('role_id')
|
|
40 |
}
|
|
41 |
else:
|
|
42 |
self.value = None
|
|
43 |
|
|
44 |
|
|
45 |
class AutomaticDispatchTableWidget(WidgetListAsTable):
|
|
46 |
readonly = False
|
|
47 |
def __init__(self, name, **kwargs):
|
|
48 |
super(AutomaticDispatchTableWidget, self).__init__(name,
|
|
49 |
element_type=AutomaticDispatchRowWidget, **kwargs)
|
|
50 |
|
|
51 |
|
21 |
52 |
class DispatchWorkflowStatusItem(WorkflowStatusItem):
|
22 |
53 |
description = N_('Assign a Function')
|
23 |
54 |
key = 'dispatch'
|
24 |
55 |
|
25 |
56 |
role_id = None
|
26 |
57 |
role_key = None
|
|
58 |
dispatch_type = 'manual'
|
|
59 |
variable = None
|
|
60 |
rules = None
|
27 |
61 |
|
28 |
62 |
def get_parameters(self):
|
29 |
|
return ('role_key', 'role_id')
|
|
63 |
return ('role_key', 'role_id', 'dispatch_type', 'variable', 'rules')
|
30 |
64 |
|
31 |
65 |
def role_id_export_to_xml(self, item, charset, include_id=False):
|
32 |
66 |
self._role_export_to_xml('role_id', item, charset,
|
... | ... | |
49 |
83 |
form.add(SingleSelectWidget, '%srole_key' % prefix,
|
50 |
84 |
title=_('Function to Set'), value=self.role_key,
|
51 |
85 |
options=[(None, '----')] + self.parent.parent.roles.items())
|
|
86 |
dispatch_types = collections.OrderedDict(
|
|
87 |
[('manual', _('Manual')), ('automatic', _('Automatic'))])
|
|
88 |
if 'dispatch_type' in parameters:
|
|
89 |
form.add(RadiobuttonsWidget, '%sdispatch_type' % prefix,
|
|
90 |
title=_('Type'),
|
|
91 |
options=dispatch_types.items(),
|
|
92 |
value=self.dispatch_type,
|
|
93 |
required=True,
|
|
94 |
attrs={'data-dynamic-display-parent': 'true'})
|
52 |
95 |
if 'role_id' in parameters:
|
53 |
96 |
form.add(SingleSelectWidget, '%srole_id' % prefix,
|
54 |
97 |
title=_('Role'), value=str(self.role_id),
|
55 |
|
options=[(None, '----', None)] + get_user_roles())
|
|
98 |
options=[(None, '----', None)] + get_user_roles(),
|
|
99 |
attrs={
|
|
100 |
'data-dynamic-display-child-of': '%sdispatch_type' % prefix,
|
|
101 |
'data-dynamic-display-value': dispatch_types.get('manual'),
|
|
102 |
}
|
|
103 |
)
|
|
104 |
if 'variable' in parameters:
|
|
105 |
form.add(StringWidget, '%svariable' % prefix,
|
|
106 |
title=_('Variable'),
|
|
107 |
value=self.variable,
|
|
108 |
attrs={
|
|
109 |
'data-dynamic-display-child-of': '%sdispatch_type' % prefix,
|
|
110 |
'data-dynamic-display-value': dispatch_types.get('automatic'),
|
|
111 |
}
|
|
112 |
)
|
|
113 |
if 'rules' in parameters:
|
|
114 |
form.add(AutomaticDispatchTableWidget, '%srules' % prefix,
|
|
115 |
title=_('Rules'),
|
|
116 |
value=self.rules,
|
|
117 |
attrs={
|
|
118 |
'data-dynamic-display-child-of': '%sdispatch_type' % prefix,
|
|
119 |
'data-dynamic-display-value': dispatch_types.get('automatic'),
|
|
120 |
}
|
|
121 |
)
|
56 |
122 |
|
57 |
123 |
def perform(self, formdata):
|
58 |
|
if not (self.role_id and self.role_key):
|
59 |
|
return
|
60 |
124 |
if not formdata.workflow_roles:
|
61 |
125 |
formdata.workflow_roles = {}
|
62 |
|
formdata.workflow_roles[self.role_key] = str(self.role_id)
|
63 |
|
formdata.store()
|
64 |
126 |
|
65 |
|
register_item_class(DispatchWorkflowStatusItem)
|
|
127 |
new_role_id = None
|
|
128 |
|
|
129 |
if self.dispatch_type == 'manual' or not self.dispatch_type:
|
|
130 |
if not (self.role_id and self.role_key):
|
|
131 |
return
|
|
132 |
new_role_id = self.role_id
|
|
133 |
elif self.dispatch_type == 'automatic':
|
|
134 |
if not (self.role_key and self.variable and self.rules):
|
|
135 |
return
|
|
136 |
variables = get_publisher().substitutions.get_context_variables()
|
|
137 |
# convert the given value to a few different types, to allow more
|
|
138 |
# diversity in matching.
|
|
139 |
variable_values = [variables.get(self.variable)]
|
|
140 |
if not variable_values[0]:
|
|
141 |
variable_values.append(None)
|
|
142 |
try:
|
|
143 |
variable_values.append(int(variable_values[0]))
|
|
144 |
except (ValueError, TypeError):
|
|
145 |
pass
|
66 |
146 |
|
|
147 |
for rule in self.rules:
|
|
148 |
if rule.get('value') in variable_values:
|
|
149 |
new_role_id = rule.get('role_id')
|
|
150 |
break
|
|
151 |
|
|
152 |
if new_role_id:
|
|
153 |
formdata.workflow_roles[self.role_key] = str(new_role_id)
|
|
154 |
formdata.store()
|
|
155 |
|
|
156 |
register_item_class(DispatchWorkflowStatusItem)
|
67 |
|
-
|