web/views/workflow.py
author sylvain.thenault@logilab.fr
Tue, 28 Apr 2009 19:09:37 +0200
branchtls-sprint
changeset 1519 5cfc5cc1dd20
parent 1511 514e4e53a3c7
child 1533 bcd4bfff658b
permissions -rw-r--r--
fix retreival of previously submitted error for entity forms
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
     1
"""workflow views:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
     2
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
     3
* IWorkflowable views and forms
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
     4
* workflow entities views (State, Transition, TrInfo)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
:organization: Logilab
795
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
     7
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
822
e3d8db01f3f5 test_views fixes
sylvain.thenault@logilab.fr
parents: 795
diff changeset
    12
from logilab.mtconverter import html_escape
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    13
from logilab.common.graph import escape, GraphGenerator, DotBackend
822
e3d8db01f3f5 test_views fixes
sylvain.thenault@logilab.fr
parents: 795
diff changeset
    14
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    15
from cubicweb import Unauthorized, view
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    16
from cubicweb.selectors import (implements, has_related_entities,
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    17
                                relation_possible, match_form_params)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    18
from cubicweb.interfaces import IWorkflowable
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    19
from cubicweb.web import stdmsgs, action, component, form
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1305
diff changeset
    20
from cubicweb.web.form import FormViewMixIn
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    21
from cubicweb.web.formfields import StringField,  RichTextField
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
    22
from cubicweb.web.formwidgets import HiddenInput, SubmitButton, Button
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    23
from cubicweb.web.views import TmpFileViewMixin
1151
b20677336ee6 turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    24
from cubicweb.web.views.boxes import EditBox
b20677336ee6 turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    25
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    26
_ = unicode
1151
b20677336ee6 turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    27
b20677336ee6 turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    28
EditBox.rmode.set_rtag('create', 'destination_state', 'subject', 'Transition')
1511
514e4e53a3c7 do not set visible property by default
sylvain.thenault@logilab.fr
parents: 1504
diff changeset
    29
EditBox.rmode.set_rtag('create', 'allowed_transition', 'object', otype='Transition')
514e4e53a3c7 do not set visible property by default
sylvain.thenault@logilab.fr
parents: 1504
diff changeset
    30
EditBox.rmode.set_rtag('create', 'destination_state', 'object', otype='State')
1151
b20677336ee6 turn old __rtags__ the new way
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    31
EditBox.rmode.set_rtag('create', 'allowed_transition', 'subject', 'State')
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    32
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    33
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    34
# IWorkflowable views #########################################################
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    35
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    36
class ChangeStateForm(form.EntityFieldsForm):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    37
    id = 'changestate'
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    38
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
    39
    __method = StringField(name='__method', initial='set_state',
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
    40
                           widget=HiddenInput)
1511
514e4e53a3c7 do not set visible property by default
sylvain.thenault@logilab.fr
parents: 1504
diff changeset
    41
    state = StringField(eidparam=True, widget=HiddenInput)
514e4e53a3c7 do not set visible property by default
sylvain.thenault@logilab.fr
parents: 1504
diff changeset
    42
    trcomment = RichTextField(label=_('comment:'), eidparam=True)
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
    43
    form_buttons = [SubmitButton(stdmsgs.YES),
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1151
diff changeset
    44
                     Button(stdmsgs.NO, cwaction='cancel')]
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    45
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    46
1318
50e1a778c5ee new FormViewMixIn class, cleanup FormMixIn (to remove once cubes doesn't use it anymore)
sylvain.thenault@logilab.fr
parents: 1305
diff changeset
    47
class ChangeStateFormView(FormViewMixIn, view.EntityView):
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    48
    id = 'statuschange'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    49
    title = _('status change')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    50
    __select__ = implements(IWorkflowable) & match_form_params('treid')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    52
    def cell_call(self, row, col):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    53
        entity = self.entity(row, col)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    54
        state = entity.in_state[0]
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    55
        transition = self.req.eid_rset(self.req.form['treid']).get_entity(0, 0)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    56
        dest = transition.destination()
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    57
        _ = self.req._
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
    58
        form = self.vreg.select_object('forms', 'changestate', self.req,
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
    59
                                       self.rset, row=row, col=col,
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
    60
                                       entity=entity,
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
    61
                                       redirect_path=self.redirectpath(entity))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    62
        self.w(form.error_message())
1305
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
    63
        self.w(u'<h4>%s %s</h4>\n' % (_(transition.name),
395ef7f2b95b cleanup, remove some unnecessary (sometime buggy) stuff
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
    64
                                      entity.view('oneline')))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    65
        msg = _('status will change from %(st1)s to %(st2)s') % {
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    66
            'st1': _(state.name),
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    67
            'st2': _(dest.name)}
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    68
        self.w(u'<p>%s</p>\n' % msg)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    69
        self.w(form.form_render(state=dest.eid, trcomment=u''))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    70
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    71
    def redirectpath(self, entity):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    72
        return entity.rest_path()
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    73
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    74
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    75
class WFHistoryVComponent(component.EntityVComponent):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    76
    """display the workflow history for entities supporting it"""
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    77
    id = 'wfhistory'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    78
    __select__ = (component.EntityVComponent.__select__
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    79
                  & relation_possible('wf_info_for', role='object'))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    80
    context = 'navcontentbottom'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    81
    title = _('Workflow history')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    82
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    83
    def cell_call(self, row, col, view=None):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    84
        _ = self.req._
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    85
        eid = self.rset[row][col]
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    86
        sel = 'Any FS,TS,WF,D'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    87
        rql = ' ORDERBY D DESC WHERE WF wf_info_for X,'\
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    88
              'WF from_state FS, WF to_state TS, WF comment C,'\
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    89
              'WF creation_date D'
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
    90
        if self.vreg.schema.eschema('CWUser').has_perm(self.req, 'read'):
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    91
            sel += ',U,C'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    92
            rql += ', WF owned_by U?'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    93
            displaycols = range(5)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    94
            headers = (_('from_state'), _('to_state'), _('comment'), _('date'),
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    95
                       _('CWUser'))
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    96
        else:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    97
            sel += ',C'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    98
            displaycols = range(4)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
    99
            headers = (_('from_state'), _('to_state'), _('comment'), _('date'))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   100
        rql = '%s %s, X eid %%(x)s' % (sel, rql)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   101
        try:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   102
            rset = self.req.execute(rql, {'x': eid}, 'x')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   103
        except Unauthorized:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   104
            return
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   105
        if rset:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   106
            self.wview('table', rset, title=_(self.title), displayactions=False,
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   107
                       displaycols=displaycols, headers=headers)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   108
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   109
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   110
# workflow entity types views #################################################
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   111
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   112
class CellView(view.EntityView):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
    id = 'cell'
728
a95b284150d1 first pass to use __select__ instead of __selectors__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 688
diff changeset
   114
    __select__ = implements('TrInfo')
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   115
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    def cell_call(self, row, col, cellvid=None):
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   117
        self.w(self.entity(row, col).printable_value('comment'))
795
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   118
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   119
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   120
class StateInContextView(view.EntityView):
795
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   121
    """convenience trick, State's incontext view should not be clickable"""
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   122
    id = 'incontext'
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   123
    __select__ = implements('State')
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   124
795
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   125
    def cell_call(self, row, col):
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   126
        self.w(html_escape(self.view('textincontext', self.rset,
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   127
                                     row=row, col=col)))
63f4f81e19f1 restore appropriate in State's incontext view, moved it into wfentities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 728
diff changeset
   128
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   129
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   130
# workflow images #############################################################
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   131
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   132
class ViewWorkflowAction(action.Action):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   133
    id = 'workflow'
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
   134
    __select__ = implements('CWEType') & has_related_entities('state_of', 'object')
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   135
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   136
    category = 'mainactions'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   137
    title = _('view workflow')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   138
    def url(self):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   139
        entity = self.rset.get_entity(self.row or 0, self.col or 0)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   140
        return entity.absolute_url(vid='workflow')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   141
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   142
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
   143
class CWETypeWorkflowView(view.EntityView):
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   144
    id = 'workflow'
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
   145
    __select__ = implements('CWEType')
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   146
    cache_max_age = 60*60*2 # stay in http cache for 2 hours by default
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   147
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   148
    def cell_call(self, row, col, **kwargs):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   149
        entity = self.entity(row, col)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   150
        self.w(u'<h1>%s</h1>' % (self.req._('workflow for %s')
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   151
                                 % display_name(self.req, entity.name)))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   152
        self.w(u'<img src="%s" alt="%s"/>' % (
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   153
            html_escape(entity.absolute_url(vid='ewfgraph')),
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   154
            html_escape(self.req._('graphical workflow for %s') % entity.name)))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   155
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   156
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   157
class WorkflowDotPropsHandler(object):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   158
    def __init__(self, req):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   159
        self._ = req._
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   160
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   161
    def node_properties(self, stateortransition):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   162
        """return default DOT drawing options for a state or transition"""
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   163
        props = {'label': stateortransition.name,
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   164
                 'fontname': 'Courier'}
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   165
        if hasattr(stateortransition, 'state_of'):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   166
            props['shape'] = 'box'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   167
            props['style'] = 'filled'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   168
            if stateortransition.reverse_initial_state:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   169
                props['color'] = '#88CC88'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   170
        else:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   171
            props['shape'] = 'ellipse'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   172
            descr = []
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   173
            tr = stateortransition
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   174
            if tr.require_group:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   175
                descr.append('%s %s'% (
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   176
                    self._('groups:'),
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   177
                    ','.join(g.name for g in tr.require_group)))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   178
            if tr.condition:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   179
                descr.append('%s %s'% (self._('condition:'), tr.condition))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   180
            if descr:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   181
                props['label'] += escape('\n'.join(descr))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   182
        return props
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   183
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   184
    def edge_properties(self, transition, fromstate, tostate):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   185
        return {'label': '', 'dir': 'forward',
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   186
                'color': 'black', 'style': 'filled'}
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   187
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   188
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   189
class WorkflowVisitor:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   190
    def __init__(self, entity):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   191
        self.entity = entity
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   192
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   193
    def nodes(self):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   194
        for state in self.entity.reverse_state_of:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   195
            state.complete()
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   196
            yield state.eid, state
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   197
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   198
        for transition in self.entity.reverse_transition_of:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   199
            transition.complete()
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   200
            yield transition.eid, transition
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   201
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   202
    def edges(self):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   203
        for transition in self.entity.reverse_transition_of:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   204
            for incomingstate in transition.reverse_allowed_transition:
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   205
                yield incomingstate.eid, transition.eid, transition
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   206
            yield transition.eid, transition.destination().eid, transition
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   207
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   208
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
   209
class CWETypeWorkflowImageView(TmpFileViewMixin, view.EntityView):
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   210
    id = 'ewfgraph'
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   211
    content_type = 'image/png'
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1318
diff changeset
   212
    __select__ = implements('CWEType')
1504
c6051f141807 register to catalog, delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   213
1091
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   214
    def _generate(self, tmpfile):
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   215
        """display schema information for an entity"""
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   216
        entity = self.entity(self.row, self.col)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   217
        visitor = WorkflowVisitor(entity)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   218
        prophdlr = WorkflowDotPropsHandler(self.req)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   219
        generator = GraphGenerator(DotBackend('workflow', 'LR',
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   220
                                              ratio='compress', size='30,12'))
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   221
        return generator.generate(visitor, prophdlr, tmpfile)
b5e253c0dd13 a bit of reorganisation inside web/views:
sylvain.thenault@logilab.fr
parents: 984
diff changeset
   222