Project

General

Profile

wcs.workflow-jumps.diff

Frédéric Péters, 27 January 2012 06:41 PM

Download (12.6 KB)

View differences:


  

wcs/formdata.py (copie de travail)
174 174
                status_id = status.split('-')[1]
175 175
            except IndexError:
176 176
                return _('Unknown')
177
            wf_status = [x for x in self.formdef.workflow.possible_status if x.id == status_id][0]
177
            try:
178
                wf_status = [x for x in self.formdef.workflow.possible_status if x.id == status_id][0]
179
            except IndexError:
180
                return _('Unknown')
178 181
            return wf_status.name
179 182
        else:
180 183
            # COMPAT (behaviour when no workflow)
......
207 210
        if not self.status.startswith('wf-'):
208 211
            raise 'Error, this codepath is only for workflows (current status: %s)' % self.status
209 212
        status_id = self.status.split('-')[1]
210
        wf_status = [x for x in self.formdef.workflow.possible_status if x.id == status_id][0]
213
        try:
214
            wf_status = [x for x in self.formdef.workflow.possible_status if x.id == status_id][0]
215
        except IndexError:
216
            return None
211 217
        return wf_status
212 218

  
213 219
    def get_field_value(self, field):
wcs/workflows.py (copie de travail)
376 376
        for f in self.get_parameters():
377 377
            widget = form.get_widget(f)
378 378
            if widget:
379
                setattr(self, f, widget.parse())
379
                value = widget.parse()
380
                if hasattr(self, '%s_parse' % f):
381
                    value = getattr(self, '%s_parse' % f)(value)
382
                setattr(self, f, value)
380 383

  
381 384
    def compute(self, var):
382 385
        if not isinstance(var, basestring):
......
451 454
        return odict
452 455

  
453 456

  
457
class WorkflowStatusJumpItem(WorkflowStatusItem):
458
    status = None
459

  
460
    def get_status(self):
461
        if not self.status:
462
            return None
463
        try:
464
            return [x for x in self.parent.parent.possible_status if x.id == self.status][0]
465
        except IndexError:
466
            get_publisher().get_app_logger().error(
467
                            'reference to invalid status in workflow %s, status %s, item %s' % (
468
                                    self.parent.parent.name,
469
                                    self.parent.name,
470
                                    self.description))
471
            return None
472

  
473
    def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
474
        if 'status' in parameters:
475
            form.add(SingleSelectWidget, '%sstatus' % prefix, title = _('Status'), value = self.status,
476
                options = [(None, '---')] + [(x.id, x.name) for x in self.parent.parent.possible_status])
477

  
478
    def get_parameters(self):
479
        return ('status',)
480

  
481

  
454 482
def get_role_translation(formdef, role_name):
455 483
    if role_name == '_receiver':
456 484
        return formdef.receiver_id
......
561 589
register_item_class(CommentableWorkflowStatusItem)
562 590

  
563 591

  
564
class ChoiceWorkflowStatusItem(WorkflowStatusItem):
592
class ChoiceWorkflowStatusItem(WorkflowStatusJumpItem):
565 593
    description = N_('Change Status')
566 594
    key = 'choice'
567 595
    endpoint = False
568 596

  
569 597
    label = None
570
    status = None
571 598
    by = []
572 599

  
573 600
    def render_as_line(self):
574 601
        if self.label:
575 602
            if self.by:
576 603
                return _('Change Status "%(label)s" by %(by)s') % \
577
                        { 'label' : self.label, 
604
                        { 'label' : self.label,
578 605
                          'by' : render_list_of_roles(self.by) }
579 606
            else:
580 607
                return _('Change Status "%s"') % self.label
......
586 613

  
587 614
    def submit_form(self, form, formdata, user, evo):
588 615
        if form.get_submit() == self.id:
589
            wf_status = [x for x in self.parent.parent.possible_status \
590
                         if str(x.id) == str(self.status)][0]
591
            evo.status = 'wf-%s' % wf_status.id
592
            form.clear_errors()
593
            return True # get out of processing loop
616
            wf_status = self.get_status()
617
            if wf_status:
618
                evo.status = 'wf-%s' % wf_status.id
619
                form.clear_errors()
620
                return True # get out of processing loop
594 621

  
595 622
    def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
596 623
        if 'label' in parameters:
597 624
            form.add(StringWidget, '%slabel' % prefix, title = _('Label'), value = self.label)
598
        if 'status' in parameters:
599
            form.add(SingleSelectWidget, '%sstatus' % prefix, title = _('Status'), value = self.status,
600
                options = [(None, '---')] + [(x.id, x.name) for x in self.parent.parent.possible_status])
625
        WorkflowStatusJumpItem.add_parameters_widgets(self, form, parameters, prefix, formdef)
601 626
        if 'by' in parameters:
602 627
            form.add(WidgetList, '%sby' % prefix, title = _('By'), element_type = SingleSelectWidget,
603 628
                value = self.by,
......
615 640
register_item_class(ChoiceWorkflowStatusItem)
616 641

  
617 642

  
618
class JumpOnSubmitWorkflowStatusItem(WorkflowStatusItem):
643
class JumpOnSubmitWorkflowStatusItem(WorkflowStatusJumpItem):
619 644
    description = N_('Change Status on Submit')
620 645
    key = 'jumponsubmit'
621 646

  
622
    status = None
623

  
624 647
    def render_as_line(self):
625 648
        if self.status:
626
            wf_status = [x for x in self.parent.parent.possible_status if x.id == self.status][0]
627
            return _('Change Status on Submit (to %s)') % wf_status.name
649
            if self.get_status():
650
                return _('Change Status on Submit (to %s)') % self.get_status().name
651
            else:
652
                return _('Change Status on Submit (broken)')
628 653
        else:
629 654
            return _('Change Status on Submit (not completed)')
630 655

  
631 656
    def submit_form(self, form, formdata, user, evo):
632 657
        if form.is_submitted() and not form.has_errors():
633
            wf_status = [x for x in self.parent.parent.possible_status if x.id == self.status][0]
634
            evo.status = 'wf-%s' % wf_status.id
635

  
636
    def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
637
        if 'status' in parameters:
638
            form.add(SingleSelectWidget, '%sstatus' % prefix, title = _('Status'), value = self.status,
639
                options = [(None, '---')] + [(x.id, x.name) for x in self.parent.parent.possible_status])
640

  
641
    def get_parameters(self):
642
        return ('status',)
658
            wf_status = self.get_status()
659
            if wf_status:
660
                evo.status = 'wf-%s' % wf_status.id
643 661
register_item_class(JumpOnSubmitWorkflowStatusItem)
644 662

  
645 663

  
wcs/wf/jump.py (copie de travail)
16 16

  
17 17
from qommon.form import *
18 18

  
19
from wcs.workflows import WorkflowStatusItem, register_item_class
19
from wcs.workflows import WorkflowStatusJumpItem, register_item_class
20 20

  
21 21

  
22
class JumpWorkflowStatusItem(WorkflowStatusItem):
22
class JumpWorkflowStatusItem(WorkflowStatusJumpItem):
23 23
    description = N_('Change Status Automatically')
24 24
    key = 'jump'
25 25

  
26
    status = None
27 26
    condition = None
28 27

  
29 28
    def render_as_line(self):
30 29
        if self.status:
31
            wf_status = [x for x in self.parent.parent.possible_status if x.id == self.status][0]
30
            wf_status = self.get_status()
31
            if not wf_status:
32
                return _('Change Status Automatically (broken)')
32 33
            if self.condition:
33 34
                return _('Change Status Automatically (to %(name)s, if %(condition)s)') % {
34 35
                            'name': wf_status.name,
......
43 44
        return ('status', 'condition')
44 45

  
45 46
    def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
46
        if 'status' in parameters:
47
            form.add(SingleSelectWidget, '%sstatus' % prefix, title = _('Status'), value = self.status,
48
                options = [(None, '---')] + [(x.id, x.name) for x in self.parent.parent.possible_status])
47
        WorkflowStatusJumpItem.add_parameters_widgets(self, form, parameters, prefix, formdef)
49 48
        if 'condition' in parameters:
50 49
            form.add(StringWidget, '%scondition' % prefix, title=_('Condition (formula)'),
51 50
                     value=self.condition, size=40)
......
61 60
            except:
62 61
                condition = False
63 62
        if condition:
64
            wf_status = [x for x in self.parent.parent.possible_status if x.id == self.status][0]
65
            formdata.status = 'wf-%s' % wf_status.id
63
            wf_status = self.get_status()
64
            if wf_status:
65
                formdata.status = 'wf-%s' % wf_status.id
66 66

  
67 67
register_item_class(JumpWorkflowStatusItem)
wcs/wf/timeout_jump.py (copie de travail)
16 16

  
17 17
from wcs.qommon.humantime import *
18 18

  
19
from wcs.workflows import Workflow, WorkflowStatusItem, register_item_class, \
19
from wcs.workflows import Workflow, WorkflowStatusJumpItem, register_item_class, \
20 20
        render_list_of_roles, get_role_translation
21 21

  
22 22
from wcs.formdata import Evolution
......
24 24
from qommon.publisher import get_publisher_class
25 25
from qommon.cron import CronJob
26 26

  
27
class TimeoutWorkflowStatusItem(WorkflowStatusItem):
27
class TimeoutWorkflowStatusItem(WorkflowStatusJumpItem):
28 28
    description = N_('Change Status on Timeout')
29 29
    key = 'timeout'
30 30
    endpoint = False
31 31

  
32
    status = None
33 32
    timeout = None
34 33

  
35 34
    _granularity = 20 * 60 # default: 20 minutes, see bottom of file
36 35

  
37 36
    def render_as_line(self):
38
        status = [ x for x in self.parent.parent.possible_status \
39
                   if str(x.id) == str(self.status)]
40
        if status and self.timeout:
37
        if self.status and self.get_status() and self.timeout:
41 38
            return _('Change Status on Timeout to "%(status)s" after %(time)s') % \
42
                {'status': status[0].name, 'time': seconds2humanduration(self.timeout)}
39
                {'status': self.get_status().name, 'time': seconds2humanduration(self.timeout)}
43 40
        else:
44 41
            return _('Change Status on Timeout (not completed)')
45 42

  
46
    def fill_admin_form(self, form):
47
        form.add(SingleSelectWidget, 'status', title = _('Status'), value = self.status,
48
                options = [(None, '---')] + [(x.id, x.name) for x in self.parent.parent.possible_status])
49
        _hint = htmltext(_("ex.: 7 days 1 minute<br/> Usable units of time: %(variables)s. " \
43
    def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
44
        WorkflowStatusJumpItem.add_parameters_widgets(self, form, parameters, prefix, formdef)
45
        if 'timeout' in parameters:
46
            _hint = htmltext(_("ex.: 7 days 1 minute<br/> Usable units of time: %(variables)s. " \
50 47
                  '<br/><span class="warning">Minimal duration is %(granularity)s</span>')) % {
51 48
                        'variables': ','.join(timewords()),
52 49
                        'granularity': seconds2humanduration(self._granularity)}
53
        form.add(StringWidget, 'timeout', title=_('Timeout'),
50
            form.add(StringWidget, 'timeout', title=_('Timeout'),
54 51
                        value=seconds2humanduration(self.timeout), hint=_hint)
55 52

  
56
    def submit_admin_form(self, form):
57
        widget = form.get_widget('timeout')
58
        if widget and widget.parse():
59
            try:
60
                self.timeout = int(widget.parse())
61
            except ValueError:
62
                try:
63
                    self.timeout = humanduration2seconds(widget.parse())
64
                except ValueError:
65
                    self.timeout = None
66
        for f in ('status',):
67
            widget = form.get_widget(f)
68
            if widget:
69
                setattr(self, f, widget.parse())
53
    def timeout_parse(self, value):
54
        try:
55
            return humanduration2seconds(value)
56
        except ValueError:
57
            return None
70 58

  
59
    def get_parameters(self):
60
        return ('status', 'timeout')
71 61

  
62

  
72 63
def workflows_with_timeout():
73 64
    '''Return a list of workflow objects with at least a status with a timeout object
74 65
    '''
......
86 77
                    status = 'wf-%s' % status.id
87 78
                    if not wfs_status[id].get(status):
88 79
                        wfs_status[id][status] = []
80
                    if not item.get_status():
81
                        # this will catch status being a removed status
82
                        continue
89 83
                    wfs_status[id][status].append(item)
90 84

  
91 85
    return wfs_status