[gen] Allow to group transitions.

This commit is contained in:
Gaetan Delannay 2013-09-24 12:26:31 +02:00
parent 180b3473e8
commit 91e0bd2240
8 changed files with 166 additions and 95 deletions

View file

@ -130,15 +130,14 @@ class Group:
if self.group: return self.group.getMasterData()
def generateLabels(self, messages, classDescr, walkedGroups,
forSearch=False):
content='fields'):
'''This method allows to generate all the needed i18n labels related to
this group. p_messages is the list of i18n p_messages (a PoMessages
instance) that we are currently building; p_classDescr is the
descriptor of the class where this group is defined. If p_forSearch
is True, this group is used for grouping searches, and not fields.'''
# A part of the group label depends on p_forSearch.
if forSearch: gp = 'searchgroup'
else: gp = 'group'
descriptor of the class where this group is defined. The type of
content in this group is specified by p_content.'''
# A part of the group label depends on p_content.
gp = (content == 'searches') and 'searchgroup' or 'group'
if self.hasLabel:
msgId = '%s_%s_%s' % (classDescr.name, gp, self.name)
messages.append(msgId, self.name)
@ -157,25 +156,25 @@ class Group:
not self.group.label:
# We remember walked groups for avoiding infinite recursion.
self.group.generateLabels(messages, classDescr, walkedGroups,
forSearch=forSearch)
content=content)
def insertInto(self, fields, uiGroups, page, metaType, forSearch=False):
def insertInto(self, elems, uiGroups, page, className, content='fields'):
'''Inserts the UiGroup instance corresponding to this Group instance
into p_fields, the recursive structure used for displaying all
fields in a given p_page (or all searches), and returns this
UiGroup instance.'''
into p_elems, the recursive structure used for displaying all
elements in a given p_page (fields, searches, transitions...) and
returns this UiGroup instance.'''
# First, create the corresponding UiGroup if not already in p_uiGroups.
if self.name not in uiGroups:
uiGroup = uiGroups[self.name] = UiGroup(self, page, metaType,
forSearch=forSearch)
# Insert the group at the higher level (ie, directly in p_fields)
uiGroup = uiGroups[self.name] = UiGroup(self, page, className,
content=content)
# Insert the group at the higher level (ie, directly in p_elems)
# if the group is not itself in a group.
if not self.group:
fields.append(uiGroup)
elems.append(uiGroup)
else:
outerGroup = self.group.insertInto(fields, uiGroups, page,
metaType,forSearch=forSearch)
outerGroup.addField(uiGroup)
outerGroup = self.group.insertInto(elems, uiGroups, page,
className, content=content)
outerGroup.addElement(uiGroup)
else:
uiGroup = uiGroups[self.name]
return uiGroup
@ -187,14 +186,15 @@ class Column:
self.align = align
class UiGroup:
'''On-the-fly-generated data structure that groups all fields sharing the
same appy.fields.Group instance, that some logged user can see.'''
'''On-the-fly-generated data structure that groups all elements
(fields, searches, transitions...) sharing the same Group instance, that
the currently logged user can see.'''
# PX that renders a help icon for a group.
pxHelp = Px('''<acronym title="obj.translate('help', field=field)"><img
src=":url('help')"/></acronym>''')
# PX that renders the content of a group.
# PX that renders the content of a group (which is refered as var "field").
pxContent = Px('''
<table var="cellgap=field.cellgap" width=":field.wide"
align=":ztool.flipLanguageDirection(field.align, dir)"
@ -219,7 +219,7 @@ class UiGroup:
_('%s_col%d' % (field.labelId, (colNb+1))) or ''</th>
</tr>
<!-- The rows of widgets -->
<tr valign=":field.valign" for="row in field.fields">
<tr valign=":field.valign" for="row in field.elements">
<td for="field in row"
colspan="field.colspan"
style=":not loop.field.last and ('padding-right:%s'% cellgap) or ''">
@ -231,7 +231,7 @@ class UiGroup:
</tr>
</table>''')
# PX that renders a group of fields.
# PX that renders a group of fields (the group is refered as var "field").
pxView = Px('''
<x var="tagCss=field.master and ('slave_%s_%s' % \
(field.masterName, '_'.join(field.masterValue))) or '';
@ -253,14 +253,14 @@ class UiGroup:
<x if="field.style not in ('fieldset', 'tabs')">:field.pxContent</x>
<!-- Render the group as tabs if required -->
<x if="field.style == 'tabs'" var2="lenFields=len(field.fields)">
<x if="field.style == 'tabs'" var2="lenFields=len(field.elements)">
<table width=":field.wide" class=":groupCss" id=":tagId" name=":tagName">
<!-- First row: the tabs. -->
<tr valign="middle"><td style="border-bottom: 1px solid #ff8040">
<table style="position:relative; bottom:-2px"
cellpadding="0" cellspacing="0">
<tr valign="bottom">
<x for="row in field.fields"
<x for="row in field.elements"
var2="rowNb=loop.row.nb;
tabId='tab_%s_%d_%d' % (field.name, rowNb, lenFields)">
<td><img src=":url('tabLeft')" id=":'%s_left' % tabId"/></td>
@ -276,7 +276,7 @@ class UiGroup:
</td></tr>
<!-- Other rows: the fields -->
<tr for="row in field.fields"
<tr for="row in field.elements"
id=":'tabcontent_%s_%d_%d' % (field.name, loop.row.nb, lenFields)"
style=":loop.row.nb==0 and 'display:table-row' or 'display:none')">
<td var="field=row[0]">
@ -318,53 +318,77 @@ class UiGroup:
</div>
</x>''')
def __init__(self, group, page, metaType, forSearch=False):
# PX that renders a group of transitions.
pxViewTransitions = Px('''
<!-- Render a group of transitions, as a one-column table -->
<table>
<x for="row in uiGroup.elements">
<x for="transition in row"><tr><td>:transition.pxView</td></tr></x>
</x>
</table>''')
# What PX to use, depending on group content?
pxByContent = {'fields': pxView, 'searches': pxViewSearches,
'transitions': pxViewTransitions}
def __init__(self, group, page, className, content='fields'):
'''A UiGroup can group various kinds of elements: fields, searches,
transitions..., The type of content that one may find in this group
is given in p_content.
* p_group is the Group instance corresponding to this UiGroup;
* p_page is the Page instance where the group is rendered (for
transitions, it corresponds to a virtual page
"workflow");
* p_className is the name of the class that holds the elements to
group.'''
self.type = 'group'
# All p_group attributes become self attributes.
# All p_group attributes become self attributes. This is required
# because a UiGroup, in some PXs, must behave like a Field (ie, have
# the same attributes, like "master".
for name, value in group.__dict__.iteritems():
if not name.startswith('_'):
setattr(self, name, value)
self.group = group
self.columnsWidths = [col.width for col in group.columns]
self.columnsAligns = [col.align for col in group.columns]
# Names of i18n labels
# Names of i18n labels for this group.
labelName = self.name
prefix = metaType
prefix = className
if group.label:
if isinstance(group.label, basestring): prefix = group.label
else: # It is a tuple (metaType, name)
else: # It is a tuple (className, name)
if group.label[1]: labelName = group.label[1]
if group.label[0]: prefix = group.label[0]
if forSearch: gp = 'searchgroup'
else: gp = 'group'
gp = (content == 'searches') and 'searchgroup' or 'group'
self.labelId = '%s_%s_%s' % (prefix, gp, labelName)
self.descrId = self.labelId + '_descr'
self.helpId = self.labelId + '_help'
# The name of the page where the group lies
self.page = page.name
# The fields belonging to the group that the current user may see.
# They will be stored by m_addField below as a list of lists because
# The elements contained in the group, that the current user may see.
# They will be stored by m_addElement below as a list of lists because
# they will be rendered as a table.
self.fields = [[]]
# PX to user for rendering this group.
self.px = forSearch and self.pxViewSearches or self.pxView
self.elements = [[]]
# PX to use for rendering this group.
self.px = self.pxByContent[content]
def addField(self, field):
'''Adds p_field into self.fields. We try first to add p_field into the
last row. If it is not possible, we create a new row.'''
def addElement(self, element):
'''Adds p_element into self.elements. We try first to add p_element into
the last row. If it is not possible, we create a new row.'''
# Get the last row
lastRow = self.fields[-1]
lastRow = self.elements[-1]
numberOfColumns = len(self.columnsWidths)
# Compute the number of columns already filled in the last row.
filledColumns = 0
for rowField in lastRow: filledColumns += rowField.colspan
for rowElem in lastRow: filledColumns += rowElem.colspan
freeColumns = numberOfColumns - filledColumns
if freeColumns >= field.colspan:
# We can add the widget in the last row.
lastRow.append(field)
if freeColumns >= element.colspan:
# We can add the element in the last row.
lastRow.append(element)
else:
if freeColumns:
# Terminate the current row by appending empty cells
for i in range(freeColumns): lastRow.append('')
# Create a new row
self.fields.append([field])
self.elements.append([element])
# ------------------------------------------------------------------------------

View file

@ -15,6 +15,8 @@
# Appy. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
import types, string
from group import Group
from appy.px import Px
from appy.gen.mail import sendNotification
# Default Appy permissions -----------------------------------------------------
@ -97,7 +99,7 @@ class State:
class Transition:
'''Represents a workflow transition.'''
def __init__(self, states, condition=True, action=None, notify=None,
show=True, confirm=False):
show=True, confirm=False, group=None):
self.states = states # In its simpler form, it is a tuple with 2
# states: (fromState, toState). But it can also be a tuple of several
# (fromState, toState) sub-tuples. This way, you may define only 1
@ -113,6 +115,7 @@ class Transition:
self.show = show # If False, the end user will not be able to trigger
# the transition. It will only be possible by code.
self.confirm = confirm # If True, a confirm popup will show up.
self.group = Group.get(group)
def getName(self, wf):
'''Returns the name for this state in workflow p_wf.'''
@ -266,6 +269,44 @@ class Transition:
if not msg: msg = obj.translate('object_saved')
obj.say(msg)
class UiTransition:
'''Represents a widget that displays a transition.'''
pxView = Px('''<x>
<!-- Real button -->
<input if="transition.mayTrigger"
type="button" class="button" title=":transition.title"
style=":url('buttonTransition', bg=True)"
value=":ztool.truncateValue(transition.title)"
onclick=":'triggerTransition(%s,%s,%s)' % (q(formId), \
q(transition.name), q(transition.confirm))"/>
<!-- Fake button, explaining why the transition can't be triggered -->
<input type="button" class="button" if="not transition.mayTrigger"
style=":url('buttonFake', bg=True) + ';cursor: help'"
value=":ztool.truncateValue(transition.title)"
title=":'%s: %s' % (transition.title, transition.reason)"/>
</x>''')
def __init__(self, name, transition, obj, mayTrigger, ):
self.name = name
self.transition = transition
self.type = 'transition'
label = obj.getWorkflowLabel(name)
self.title = obj.translate(label)
if transition.confirm:
self.confirm = obj.translate('%s_confirm' % label)
else:
self.confirm = ''
# May this transition be triggered via the UI?
self.mayTrigger = True
self.reason = ''
if not mayTrigger:
self.mayTrigger = False
self.reason = mayTrigger.msg
# Require by the UiGroup.
self.colspan = 1
# ------------------------------------------------------------------------------
class Permission:
'''If you need to define a specific read or write permission for some field

View file

@ -761,7 +761,7 @@ class ZopeGenerator(Generator):
# Generate labels for groups of searches
if search.group and not search.group.label:
search.group.generateLabels(self.labels, classDescr, set(),
forSearch=True)
content='searches')
# Generate the resulting Zope class.
self.copyFile('Class.pyt', repls, destName=fileName)

View file

@ -261,8 +261,11 @@ class ZopeInstaller:
indexed=True, searchable=True)
title.init('title', None, 'appy')
setattr(wrapperClass, 'title', title)
# Special field "state" must be added for every class
state = gen.String(show='result')
# Special field "state" must be added for every class. It must be a
# "select" field, because it will be necessary for displaying the
# translated state name.
state = gen.String(validator=gen.Selection('listStates'),
show='result')
state.init('state', None, 'workflow')
setattr(wrapperClass, 'state', state)
names = self.config.attributes[wrapperClass.__name__[:-8]]

View file

@ -738,8 +738,9 @@ class ToolMixin(BaseMixin):
res = []
default = None # Also retrieve the default one here.
groups = {} # The already encountered groups
page = Page('main') # A dummy page required by class UiGroup
page = Page('searches') # A dummy page required by class UiGroup
# Get the searches statically defined on the class
className = self.getPortalType(klass)
searches = ClassDescriptor.getSearches(klass, tool=self.appy())
# Get the dynamically computed searches
if hasattr(klass, 'getDynamicSearches'):
@ -752,8 +753,8 @@ class ToolMixin(BaseMixin):
res.append(uiSearch)
else:
uiGroup = search.group.insertInto(res, groups, page, className,
forSearch=True)
uiGroup.addField(uiSearch)
content='searches')
uiGroup.addElement(uiSearch)
# Is this search the default search?
if search.default: default = uiSearch
return Object(searches=res, default=default)

View file

@ -5,6 +5,7 @@
# ------------------------------------------------------------------------------
import os, os.path, sys, types, urllib, cgi
from appy import Object
from appy.fields.workflow import UiTransition
import appy.gen as gen
from appy.gen.utils import *
from appy.gen.layout import Table, defaultPageLayouts
@ -742,11 +743,10 @@ class BaseMixin:
if not field.group:
res.append(field)
else:
# Insert the UiGroup instance corresponding to field.group at
# the right place.
# Insert the UiGroup instance corresponding to field.group.
uiGroup = field.group.insertInto(res, groups, field.page,
self.meta_type)
uiGroup.addField(field)
uiGroup.addElement(field)
if collectCssJs:
cssJs['css'] = css or ()
cssJs['js'] = js or ()
@ -786,9 +786,10 @@ class BaseMixin:
return klass.styles[elem]
return elem
def getTransitions(self, includeFake=True, includeNotShowable=False):
'''This method returns info about transitions that one can trigger from
the user interface.
def getTransitions(self, includeFake=True, includeNotShowable=False,
grouped=True):
'''This method returns info about transitions (as UiTransition
instances) that one can trigger from the user interface.
* if p_includeFake is True, it retrieves transitions that the user
can't trigger, but for which he needs to know for what reason he
can't trigger it;
@ -797,8 +798,12 @@ class BaseMixin:
and not a security concern, in some cases it has sense to set
includeNotShowable=True, because those transitions are triggerable
from a security point of view.
* If p_grouped is True, transitions are grouped according to their
"group" attribute, in a similar way to fields or searches.
'''
res = []
groups = {} # The already encountered groups of transitions.
wfPage = gen.Page('workflow')
wf = self.getWorkflow()
currentState = self.State(name=False)
# Loop on every transition
@ -818,17 +823,16 @@ class BaseMixin:
if not includeNotShowable:
includeIt = includeIt and transition.isShowable(wf, self)
if not includeIt: continue
# Add transition-info to the result.
label = self.getWorkflowLabel(name)
tInfo = {'name': name, 'title': self.translate(label),
'confirm': '', 'may_trigger': True}
if transition.confirm:
cLabel = '%s_confirm' % label
tInfo['confirm'] = self.translate(cLabel)
if not mayTrigger:
tInfo['may_trigger'] = False
tInfo['reason'] = mayTrigger.msg
res.append(tInfo)
# Create the UiTransition instance.
info = UiTransition(name, transition, self, mayTrigger)
# Add the transition into the result.
if not transition.group or not grouped:
res.append(info)
else:
# Insert the UiGroup instance corresponding to transition.group.
uiGroup = transition.group.insertInto(res, groups, wfPage,
self.__class__.__name__, content='transitions')
uiGroup.addElement(info)
return res
def getAppyPhases(self, currentOnly=False, layoutType='view'):

View file

@ -326,10 +326,7 @@ class ToolWrapper(AbstractWrapper):
<!-- Actions -->
<table class="noStyle" if="zobj.mayAct()">
<tr>
<!-- Workflow transitions -->
<td if="zobj.showTransitions('result')"
var2="targetObj=zobj">:targetObj.appy().pxTransitions</td>
<tr valign="top">
<!-- Edit -->
<td if="zobj.mayEdit()">
<a var="navInfo='search.%s.%s.%d.%d' % \
@ -344,6 +341,9 @@ class ToolWrapper(AbstractWrapper):
title=":_('object_delete')"
onClick=":'onDeleteObject(%s)' % q(zobj.UID())"/>
</td>
<!-- Workflow transitions -->
<td if="zobj.showTransitions('result')"
var2="targetObj=zobj">:targetObj.appy().pxTransitions</td>
</tr>
</table>
</x>

View file

@ -349,34 +349,22 @@ class AbstractWrapper(object):
</table>
</x>''')
# Displays an object's transitions(s).
pxTransitions = Px('''
<form var="transitions=targetObj.getTransitions()" if="transitions"
var2="formId='trigger_%s' % targetObj.UID()" method="post"
id=":formId" action=":targetObj.absolute_url() + '/do'">
<input type="hidden" name="action" value="Trigger"/>
<input type="hidden" name="workflow_action"/>
<!-- Input field for storing the comment coming from the popup -->
<textarea id="comment" name="comment" cols="30" rows="3"
style="display:none"></textarea>
<table>
<tr valign="middle">
<!-- Input field for storing comment -->
<textarea id="comment" name="comment" cols="30" rows="3"
style="display:none"></textarea>
<!-- Buttons for triggering transitions -->
<td align=":dright" for="transition in transitions">
<!-- Real button -->
<input type="button" class="button" if="transition['may_trigger']"
style=":url('buttonTransition', bg=True)"
title=":transition['title']"
value=":ztool.truncateValue(transition['title'])"
onclick=":'triggerTransition(%s,%s,%s)' % (q(formId), \
q(transition['name']), q(transition['confirm']))"/>
<!-- Fake button, explaining why the transition can't be triggered -->
<input type="button" class="button" if="not transition['may_trigger']"
style=":url('buttonFake', bg=True) + ';cursor: help'"
value=":ztool.truncateValue(transition['title'])"
title=":'%s: %s' % (transition['title'], \
transition['reason'])"/>
<!-- Render a transition or a group of transitions. -->
<x if="transition.type == 'transition'">:transition.pxView</x>
<x if="transition.type == 'group'"
var2="uiGroup=transition">:uiGroup.px</x>
</td>
</tr>
</table>
@ -446,7 +434,7 @@ class AbstractWrapper(object):
nextPage=phaseObj.getNextPage(page)[0];
isEdit=layoutType == 'edit';
pageInfo=phaseObj.pagesInfo[page]">
<tr>
<tr valign="top">
<!-- Previous -->
<td if="previousPage and pageInfo.showPrevious">
<!-- Button on the edit page -->
@ -1045,4 +1033,14 @@ class AbstractWrapper(object):
'''Produces a representation of p_text into the desired p_format, which
is 'html' by default.'''
return self.o.formatText(text, format)
def listStates(self):
'''Lists the possible states for this object.'''
res = []
o = self.o
workflow = o.getWorkflow()
for name in dir(workflow):
if getattr(workflow, name).__class__.__name__ != 'State': continue
res.append((name, o.translate(o.getWorkflowLabel(name))))
return res
# ------------------------------------------------------------------------------