2013-07-08 16:39:16 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# This file is part of Appy, a framework for building applications in the Python
|
|
|
|
# language. Copyright (C) 2007 Gaetan Delannay
|
|
|
|
|
|
|
|
# Appy is free software; you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
|
|
|
|
# Appy is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Appy. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2014-07-23 15:29:47 -05:00
|
|
|
import sys, re, os.path
|
2014-02-26 03:40:27 -06:00
|
|
|
from appy import Object
|
2013-08-21 06:54:56 -05:00
|
|
|
from appy.fields import Field
|
2014-07-18 09:54:11 -05:00
|
|
|
from appy.fields.search import Search
|
2013-07-08 16:39:16 -05:00
|
|
|
from appy.px import Px
|
|
|
|
from appy.gen.layout import Table
|
|
|
|
from appy.gen import utils as gutils
|
|
|
|
from appy.shared import utils as sutils
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Ref(Field):
|
|
|
|
# Some default layouts. "w" stands for "wide": those layouts produce tables
|
|
|
|
# of Ref objects whose width is 100%.
|
|
|
|
wLayouts = Table('lrv-f', width='100%')
|
|
|
|
# "d" stands for "description": a description label is added.
|
|
|
|
wdLayouts = {'view': Table('l-d-f', width='100%')}
|
|
|
|
|
|
|
|
# This PX displays the title of a referenced object, with a link on it to
|
|
|
|
# reach the consult view for this object. If we are on a back reference, the
|
|
|
|
# link allows to reach the correct page where the forward reference is
|
|
|
|
# defined. If we are on a forward reference, the "nav" parameter is added to
|
2014-06-15 17:58:45 -05:00
|
|
|
# the URL for allowing to navigate from one object to the next/previous one.
|
2013-07-08 16:39:16 -05:00
|
|
|
pxObjectTitle = Px('''
|
2014-10-21 02:25:37 -05:00
|
|
|
<x var="navInfo=field.getNavInfo(zobj, loop.tied.nb + 1 + startNumber, \
|
|
|
|
totalNumber, inPickList);
|
2014-09-23 11:38:55 -05:00
|
|
|
pageName=field.isBack and field.back.pageName or 'main'">
|
2014-04-01 11:34:12 -05:00
|
|
|
<x>::tied.o.getSupTitle(navInfo)</x>
|
2014-09-23 11:38:55 -05:00
|
|
|
<x>::tied.o.getListTitle(nav=navInfo, target=target, page=pageName, \
|
|
|
|
inPopup=inPopup)</x>
|
|
|
|
<span style=":showSubTitles and 'display:inline' or 'display:none'"
|
|
|
|
name="subTitle" var="sub=tied.o.getSubTitle()" if="sub">::sub</span>
|
|
|
|
</x>''')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-04-03 10:32:57 -05:00
|
|
|
# This PX displays buttons for triggering global actions on several linked
|
|
|
|
# objects (delete many, unlink many,...)
|
|
|
|
pxGlobalActions = Px('''
|
|
|
|
<!-- Insert several objects (if in pick list) -->
|
2014-04-20 12:22:40 -05:00
|
|
|
<input if="inPickList" type="button" class="button"
|
|
|
|
var2="action='link'; label=_('object_link_many')" value=":label"
|
2014-04-04 09:49:22 -05:00
|
|
|
onclick=":'onLinkMany(%s,%s)' % (q(action), q(ajaxHookId))"
|
2014-04-20 12:22:40 -05:00
|
|
|
style=":'%s; %s' % (url('linkMany', bg=True), \
|
|
|
|
ztool.getButtonWidth(label))"/>
|
2014-04-04 09:49:22 -05:00
|
|
|
<!-- Unlink several objects -->
|
2014-04-25 05:14:50 -05:00
|
|
|
<input if="mayUnlink"
|
2014-04-04 09:49:22 -05:00
|
|
|
var2="imgName=linkList and 'unlinkManyUp' or 'unlinkMany';
|
2014-04-20 12:22:40 -05:00
|
|
|
action='unlink'; label=_('object_unlink_many')"
|
|
|
|
type="button" class="button" value=":label"
|
2014-04-04 09:49:22 -05:00
|
|
|
onclick=":'onLinkMany(%s,%s)' % (q(action), q(ajaxHookId))"
|
2014-04-20 12:22:40 -05:00
|
|
|
style=":'%s; %s' % (url(imgName, bg=True), \
|
|
|
|
ztool.getButtonWidth(label))"/>
|
2014-04-04 09:49:22 -05:00
|
|
|
<!-- Delete several objects -->
|
2014-05-03 15:45:51 -05:00
|
|
|
<input if="mayEdit and field.delete"
|
2014-04-20 12:22:40 -05:00
|
|
|
var2="action='delete'; label=_('object_delete_many')"
|
|
|
|
type="button" class="button" value=":label"
|
2014-04-04 09:49:22 -05:00
|
|
|
onclick=":'onLinkMany(%s,%s)' % (q(action), q(ajaxHookId))"
|
2014-04-20 12:22:40 -05:00
|
|
|
style=":'%s; %s' % (url('deleteMany', bg=True), \
|
|
|
|
ztool.getButtonWidth(label))"/>
|
2014-04-03 10:32:57 -05:00
|
|
|
''')
|
|
|
|
|
2013-07-08 16:39:16 -05:00
|
|
|
# This PX displays icons for triggering actions on a given referenced object
|
|
|
|
# (edit, delete, etc).
|
|
|
|
pxObjectActions = Px('''
|
2014-04-15 12:52:18 -05:00
|
|
|
<table class="noStyle">
|
2013-07-08 16:39:16 -05:00
|
|
|
<tr>
|
|
|
|
<!-- Arrows for moving objects up or down -->
|
2014-06-15 17:58:45 -05:00
|
|
|
<td if="(totalNumber >1) and changeOrder and not inPickList \
|
|
|
|
and not inMenu"
|
2014-04-15 12:52:18 -05:00
|
|
|
var2="ajaxBaseCall=navBaseCall.replace('**v**','%s,%s,{%s:%s,%s:%s}'%\
|
2013-08-21 05:35:30 -05:00
|
|
|
(q(startNumber), q('doChangeOrder'), q('refObjectUid'),
|
2014-04-15 06:01:39 -05:00
|
|
|
q(tiedUid), q('move'), q('**v**')))">
|
|
|
|
<!-- Move to top -->
|
|
|
|
<img if="objectIndex > 1" class="clickable"
|
|
|
|
src=":url('arrowsUp')" title=":_('move_top')"
|
|
|
|
onclick=":ajaxBaseCall.replace('**v**', 'top')"/>
|
|
|
|
<!-- Move to bottom -->
|
|
|
|
<img if="objectIndex < (totalNumber-2)" class="clickable"
|
|
|
|
src=":url('arrowsDown')" title=":_('move_bottom')"
|
|
|
|
onclick=":ajaxBaseCall.replace('**v**', 'bottom')"/>
|
|
|
|
<!-- Move up -->
|
2013-07-20 12:56:17 -05:00
|
|
|
<img if="objectIndex > 0" class="clickable" src=":url('arrowUp')"
|
2013-07-15 06:39:05 -05:00
|
|
|
title=":_('move_up')"
|
2013-07-15 04:23:29 -05:00
|
|
|
onclick=":ajaxBaseCall.replace('**v**', 'up')"/>
|
2014-04-15 06:01:39 -05:00
|
|
|
<!-- Move down -->
|
2013-07-15 06:39:05 -05:00
|
|
|
<img if="objectIndex < (totalNumber-1)" class="clickable"
|
2013-07-20 12:56:17 -05:00
|
|
|
src=":url('arrowDown')" title=":_('move_down')"
|
2013-07-08 16:39:16 -05:00
|
|
|
onclick=":ajaxBaseCall.replace('**v**', 'down')"/>
|
|
|
|
</td>
|
|
|
|
<!-- Edit -->
|
2014-04-15 12:52:18 -05:00
|
|
|
<td if="not field.noForm and tied.o.mayEdit()">
|
2014-10-21 02:25:37 -05:00
|
|
|
<a var="navInfo=field.getNavInfo(zobj, loop.tied.nb + 1 + startNumber, \
|
|
|
|
totalNumber);
|
2014-06-15 17:58:45 -05:00
|
|
|
linkInPopup=inPopup or (target.target != '_self')"
|
|
|
|
href=":tied.o.getUrl(mode='edit', page='main', nav=navInfo, \
|
|
|
|
inPopup=linkInPopup)"
|
|
|
|
target=":target.target" onclick=":target.openPopup">
|
2013-07-20 12:56:17 -05:00
|
|
|
<img src=":url('edit')" title=":_('object_edit')"/></a>
|
2013-07-08 16:39:16 -05:00
|
|
|
</td>
|
|
|
|
<!-- Delete -->
|
2014-05-03 15:45:51 -05:00
|
|
|
<td if="mayEdit and field.delete and tied.o.mayDelete()">
|
2013-07-23 03:29:39 -05:00
|
|
|
<img class="clickable" title=":_('object_delete')" src=":url('delete')"
|
2014-04-15 06:01:39 -05:00
|
|
|
onclick=":'onDeleteObject(%s)' % q(tiedUid)"/>
|
2013-07-08 16:39:16 -05:00
|
|
|
</td>
|
|
|
|
<!-- Unlink -->
|
2014-07-10 02:46:39 -05:00
|
|
|
<td if="mayUnlink and field.mayUnlinkElement(obj, tied)">
|
2014-04-25 05:14:50 -05:00
|
|
|
<img var="imgName=linkList and 'unlinkUp' or 'unlink'; action='unlink'"
|
2014-04-03 10:32:57 -05:00
|
|
|
class="clickable" title=":_('object_unlink')" src=":url(imgName)"
|
|
|
|
onclick=":'onLink(%s,%s,%s,%s)' % (q(action), q(zobj.id), \
|
2014-04-15 06:01:39 -05:00
|
|
|
q(field.name), q(tiedUid))"/>
|
2014-04-01 11:34:12 -05:00
|
|
|
</td>
|
|
|
|
<!-- Insert (if in pick list) -->
|
|
|
|
<td if="inPickList">
|
2014-04-03 10:32:57 -05:00
|
|
|
<img var="action='link'" class="clickable" title=":_('object_link')"
|
|
|
|
src=":url(action)"
|
|
|
|
onclick=":'onLink(%s,%s,%s,%s)' % (q(action), q(zobj.id), \
|
2014-04-15 06:01:39 -05:00
|
|
|
q(field.name), q(tiedUid))"/>
|
2013-07-08 16:39:16 -05:00
|
|
|
</td>
|
2014-04-21 10:19:27 -05:00
|
|
|
<!-- Workflow transitions -->
|
|
|
|
<td if="tied.o.showTransitions('result')"
|
|
|
|
var2="targetObj=tied.o; buttonsMode='small'">:tied.pxTransitions</td>
|
2013-07-08 16:39:16 -05:00
|
|
|
</tr>
|
|
|
|
</table>''')
|
|
|
|
|
|
|
|
# Displays the button allowing to add a new object through a Ref field, if
|
|
|
|
# it has been declared as addable and if multiplicities allow it.
|
|
|
|
pxAdd = Px('''
|
2014-06-15 17:58:45 -05:00
|
|
|
<form if="mayAdd and not inPickList"
|
|
|
|
class=":inMenu and 'addFormMenu' or 'addForm'"
|
|
|
|
var2="formName='%s_%s_add' % (zobj.id, field.name)"
|
|
|
|
name=":formName" id=":formName" target=":target.target"
|
|
|
|
action=":'%s/do' % folder.absolute_url()">
|
|
|
|
<input type="hidden" name="action" value="Create"/>
|
|
|
|
<input type="hidden" name="className" value=":tiedClassName"/>
|
|
|
|
<input type="hidden" name="nav"
|
2014-10-21 02:25:37 -05:00
|
|
|
value=":field.getNavInfo(zobj, 0, totalNumber)"/>
|
2014-06-15 17:58:45 -05:00
|
|
|
<input type="hidden" name="popup"
|
|
|
|
value=":(inPopup or (target.target != '_self')) and '1' or '0'"/>
|
|
|
|
<input
|
|
|
|
type=":(field.addConfirm or field.noForm) and 'button' or 'submit'"
|
|
|
|
class="buttonSmall button"
|
|
|
|
var="label=_('add_ref')" value=":label"
|
|
|
|
style=":'%s; %s' % (url('add', bg=True), \
|
|
|
|
ztool.getButtonWidth(label))"
|
|
|
|
onclick=":field.getOnAdd(q, formName, addConfirmMsg, target, \
|
|
|
|
navBaseCall, startNumber)"/>
|
|
|
|
</form>''')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-07-28 05:29:16 -05:00
|
|
|
# Displays the button allowing to select from a popup objects to be linked
|
|
|
|
# via the Ref field.
|
|
|
|
pxLink = Px('''
|
|
|
|
<a target="appyIFrame"
|
|
|
|
var="tiedClassName=ztool.getPortalType(field.klass);
|
|
|
|
className=ztool.getPortalType(obj.klass)"
|
|
|
|
href=":'%s/query?className=%s&search=%s:%s:%s&popup=1' % \
|
|
|
|
(ztool.absolute_url(), tiedClassName, obj.uid, field.name, \
|
|
|
|
popupMode)">
|
|
|
|
<input type="button" class="buttonSmall button"
|
|
|
|
var="labelId= (popupMode=='repl') and 'search_button' or 'add_ref';
|
|
|
|
icon= (popupMode=='repl') and 'search' or 'add';
|
|
|
|
label=_(labelId)" value=":label"
|
|
|
|
style=":'%s;%s' % (url(icon,bg=True), ztool.getButtonWidth(label))"
|
|
|
|
onclick="openPopup('iframePopup')"/>
|
|
|
|
</a>''')
|
|
|
|
|
2013-07-08 16:39:16 -05:00
|
|
|
# This PX displays, in a cell header from a ref table, icons for sorting the
|
|
|
|
# ref field according to the field that corresponds to this column.
|
|
|
|
pxSortIcons = Px('''
|
2014-10-03 12:56:34 -05:00
|
|
|
<x if="changeOrder and (len(objects) > 1) and \
|
2014-11-28 07:42:32 -06:00
|
|
|
refField.isSortable(usage='ref')"
|
2013-07-15 04:23:29 -05:00
|
|
|
var2="ajaxBaseCall=navBaseCall.replace('**v**', '%s,%s,{%s:%s,%s:%s}'% \
|
2013-08-21 05:35:30 -05:00
|
|
|
(q(startNumber), q('sort'), q('sortKey'), q(refField.name), \
|
|
|
|
q('reverse'), q('**v**')))">
|
2013-07-20 12:56:17 -05:00
|
|
|
<img class="clickable" src=":url('sortAsc')"
|
2014-10-07 08:03:07 -05:00
|
|
|
var="js=ajaxBaseCall.replace('**v**', 'False')"
|
|
|
|
onclick=":'askConfirm(%s,%s,%s)' % (q('script'), q(js,False), \
|
|
|
|
q(sortConfirm))"/>
|
2013-07-20 12:56:17 -05:00
|
|
|
<img class="clickable" src=":url('sortDesc')"
|
2014-10-07 08:03:07 -05:00
|
|
|
var="js=ajaxBaseCall.replace('**v**', 'True')"
|
|
|
|
onclick=":'askConfirm(%s,%s,%s)' % (q('script'), q(js,False), \
|
|
|
|
q(sortConfirm))"/>
|
2013-07-08 16:39:16 -05:00
|
|
|
</x>''')
|
|
|
|
|
2014-04-15 12:52:18 -05:00
|
|
|
# Shows the object number in a numbered list of tied objects.
|
|
|
|
pxNumber = Px('''
|
|
|
|
<x if="not changeNumber">:objectIndex+1</x>
|
|
|
|
<div if="changeNumber" class="dropdownMenu"
|
|
|
|
var2="id='%s_%d' % (ajaxHookId, objectIndex);
|
2014-07-31 10:04:08 -05:00
|
|
|
dropdownId='%s_dd' % id;
|
|
|
|
imgId='%s_img' % id"
|
2014-04-15 12:52:18 -05:00
|
|
|
onmouseover=":'toggleDropdown(%s)' % q(dropdownId)"
|
|
|
|
onmouseout=":'toggleDropdown(%s,%s)' % (q(dropdownId), q('none'))">
|
2014-07-31 10:04:08 -05:00
|
|
|
<input type="text" size=":numberWidth" id=":id" value=":objectIndex+1"
|
|
|
|
onclick="this.select()"
|
|
|
|
onkeydown=":'if (event.keyCode==13) \
|
|
|
|
document.getElementById(%s).click()' % q(imgId)"/>
|
2014-04-15 12:52:18 -05:00
|
|
|
<!-- The menu -->
|
|
|
|
<div id=":dropdownId" class="dropdown">
|
2014-07-31 10:04:08 -05:00
|
|
|
<img class="clickable" src=":url('move')" id=":imgId"
|
2014-04-15 12:52:18 -05:00
|
|
|
title=":_('move_number')"
|
|
|
|
onclick=":navBaseCall.replace('**v**','%s,%s,{%s:%s,%s:this}' % \
|
|
|
|
(q(startNumber), q('doChangeOrder'), q('refObjectUid'),
|
|
|
|
q(tiedUid), q('move')))"/>
|
|
|
|
</div>
|
|
|
|
</div>''')
|
|
|
|
|
2014-02-26 03:40:27 -06:00
|
|
|
# PX that displays referred objects as a list.
|
2014-04-01 11:34:12 -05:00
|
|
|
pxViewList = Px('''
|
2014-07-28 05:29:16 -05:00
|
|
|
<div if="not innerRef or mayAdd or mayLink" style="margin-bottom: 4px">
|
2014-05-02 05:35:09 -05:00
|
|
|
<span if="subLabel" class="discreet">:_(subLabel)</span>
|
|
|
|
(<span class="discreet">:totalNumber</span>)
|
|
|
|
<x>:field.pxAdd</x>
|
2014-07-28 05:29:16 -05:00
|
|
|
<!-- This button opens a popup for linking additional objects -->
|
|
|
|
<x if="mayLink and not inPickList"
|
|
|
|
var2="popupMode='add'">:field.pxLink</x>
|
2014-05-02 05:35:09 -05:00
|
|
|
<!-- The search button if field is queryable -->
|
|
|
|
<input if="objects and field.queryable" type="button"
|
|
|
|
class="buttonSmall button"
|
|
|
|
var2="label=_('search_button')" value=":label"
|
|
|
|
style=":'%s; %s' % (url('search', bg=True), \
|
|
|
|
ztool.getButtonWidth(label))"
|
|
|
|
onclick=":'goto(%s)' % \
|
|
|
|
q('%s/search?className=%s&ref=%s:%s' % \
|
|
|
|
(ztool.absolute_url(), tiedClassName, zobj.id, field.name))"/>
|
|
|
|
</div>
|
2014-02-26 03:40:27 -06:00
|
|
|
|
2014-05-02 05:35:09 -05:00
|
|
|
<!-- (Top) navigation -->
|
|
|
|
<x>:tool.pxNavigate</x>
|
2014-04-01 11:34:12 -05:00
|
|
|
|
2014-05-02 05:35:09 -05:00
|
|
|
<!-- No object is present -->
|
2014-05-03 08:18:41 -05:00
|
|
|
<p class="discreet"
|
2014-05-03 15:45:51 -05:00
|
|
|
if="not objects and (innerRef and mayAdd)">:_('no_ref')</p>
|
2014-04-01 11:34:12 -05:00
|
|
|
|
2014-05-02 05:35:09 -05:00
|
|
|
<!-- Linked objects -->
|
|
|
|
<table if="objects" class=":not innerRef and 'list' or ''"
|
|
|
|
width=":innerRef and '100%' or field.layouts['view'].width"
|
|
|
|
var2="columns=ztool.getColumnsSpecifiers(tiedClassName, \
|
|
|
|
field.shownInfo, dir)">
|
|
|
|
<tr if="field.showHeaders">
|
|
|
|
<th if="not inPickList and numbered" width=":numbered"></th>
|
2014-07-29 12:21:37 -05:00
|
|
|
<th if="checkboxes" class="cbCell">
|
|
|
|
<img src=":url('checkall')" class="clickable"
|
|
|
|
title=":_('check_uncheck')"
|
|
|
|
onclick=":'toggleAllCbs(%s)' % q(ajaxHookId)"/>
|
|
|
|
</th>
|
2014-05-02 05:35:09 -05:00
|
|
|
<th for="column in columns" width=":column.width"
|
|
|
|
align=":column.align" var2="refField=column.field">
|
|
|
|
<span>:_(refField.labelId)</span>
|
|
|
|
<x>:field.pxSortIcons</x>
|
|
|
|
<x var="className=tiedClassName;
|
|
|
|
field=refField">:tool.pxShowDetails</x>
|
|
|
|
</th>
|
|
|
|
</tr>
|
|
|
|
<!-- Loop on every (tied or selectable) object. -->
|
|
|
|
<tr for="tied in objects" valign="top"
|
|
|
|
class=":loop.tied.odd and 'even' or 'odd'"
|
|
|
|
var2="tiedUid=tied.o.id;
|
2014-05-03 08:18:41 -05:00
|
|
|
objectIndex=field.getIndexOf(zobj, tiedUid)|None;
|
2014-05-03 15:45:51 -05:00
|
|
|
mayView=tied.o.mayView()">
|
2014-05-02 05:35:09 -05:00
|
|
|
<td if="not inPickList and numbered">:field.pxNumber</td>
|
2014-07-29 12:21:37 -05:00
|
|
|
<td if="checkboxes" class="cbCell">
|
|
|
|
<input if="mayView" type="checkbox" name=":ajaxHookId" checked="checked"
|
|
|
|
value=":tiedUid" onclick="toggleCb(this)"/>
|
|
|
|
</td>
|
2014-05-02 05:35:09 -05:00
|
|
|
<td for="column in columns" width=":column.width" align=":column.align"
|
|
|
|
var2="refField=column.field">
|
|
|
|
<!-- The "title" field -->
|
|
|
|
<x if="refField.name == 'title'">
|
2014-05-03 08:18:41 -05:00
|
|
|
<x if="mayView">
|
|
|
|
<x>:field.pxObjectTitle</x>
|
|
|
|
<div if="tied.o.mayAct()">:field.pxObjectActions</div>
|
|
|
|
</x>
|
|
|
|
<div if="not mayView">
|
|
|
|
<img src=":url('fake')" style="margin-right: 5px"/>
|
|
|
|
<x>:_('unauthorized')</x></div>
|
2014-05-02 05:35:09 -05:00
|
|
|
</x>
|
|
|
|
<!-- Any other field -->
|
2014-05-03 08:18:41 -05:00
|
|
|
<x if="(refField.name != 'title') and mayView">
|
2014-05-02 05:35:09 -05:00
|
|
|
<x var="zobj=tied.o; obj=tied; layoutType='cell';
|
|
|
|
innerRef=True; field=refField"
|
|
|
|
if="field.isShowable(zobj, 'result')">:field.pxRender</x>
|
|
|
|
</x>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2014-04-01 11:34:12 -05:00
|
|
|
|
2014-05-02 05:35:09 -05:00
|
|
|
<!-- Global actions -->
|
2014-07-29 12:21:37 -05:00
|
|
|
<div if="mayEdit and checkboxes">:field.pxGlobalActions</div>
|
2014-04-03 10:32:57 -05:00
|
|
|
|
2014-05-02 05:35:09 -05:00
|
|
|
<!-- (Bottom) navigation -->
|
|
|
|
<x>:tool.pxNavigate</x>
|
2014-04-03 10:32:57 -05:00
|
|
|
|
2014-05-02 05:35:09 -05:00
|
|
|
<!-- Init checkboxes if present. -->
|
|
|
|
<script if="checkboxes"
|
2014-07-19 06:42:39 -05:00
|
|
|
type="text/javascript">:'initCbs(%s)' % q(ajaxHookId)
|
2014-05-02 05:35:09 -05:00
|
|
|
</script>''')
|
2014-04-01 11:34:12 -05:00
|
|
|
|
|
|
|
# PX that displays the list of objects the user may select to insert into a
|
|
|
|
# ref field with link="list".
|
|
|
|
pxViewPickList = Px('''
|
2014-04-03 10:32:57 -05:00
|
|
|
<x var="innerRef=False;
|
|
|
|
ajaxHookId=ajaxHookId|'%s_%s_poss' % (zobj.id, field.name);
|
2014-04-01 11:34:12 -05:00
|
|
|
inPickList=True;
|
2014-04-03 10:32:57 -05:00
|
|
|
startNumber=field.getStartNumber('list', req, ajaxHookId);
|
2014-04-01 11:34:12 -05:00
|
|
|
info=field.getPossibleValues(zobj, startNumber=startNumber, \
|
|
|
|
someObjects=True, removeLinked=True);
|
|
|
|
objects=info.objects;
|
|
|
|
totalNumber=info.totalNumber;
|
|
|
|
batchSize=info.batchSize;
|
|
|
|
batchNumber=len(objects);
|
2014-04-03 10:32:57 -05:00
|
|
|
tiedClassName=tiedClassName|ztool.getPortalType(field.klass);
|
2014-07-18 09:54:11 -05:00
|
|
|
target=ztool.getLinksTargetInfo(field.klass);
|
2014-05-03 15:45:51 -05:00
|
|
|
mayEdit=mayEdit|\
|
|
|
|
not field.isBack and zobj.mayEdit(field.writePermission);
|
2014-04-25 05:14:50 -05:00
|
|
|
mayUnlink=False;
|
2014-05-03 15:45:51 -05:00
|
|
|
mayAdd=False;
|
2014-07-28 05:29:16 -05:00
|
|
|
mayLink=False;
|
|
|
|
navBaseCall='askRefField(%s,%s,%s,**v**)' % \
|
|
|
|
(q(ajaxHookId), q(zobj.absolute_url()), q(innerRef));
|
2014-04-03 10:32:57 -05:00
|
|
|
changeOrder=False;
|
2014-04-15 12:52:18 -05:00
|
|
|
changeNumber=False;
|
2014-04-05 02:49:54 -05:00
|
|
|
checkboxes=field.getAttribute(zobj, 'checkboxes') and \
|
|
|
|
(totalNumber > 1);
|
2014-04-03 10:32:57 -05:00
|
|
|
showSubTitles=showSubTitles|\
|
|
|
|
req.get('showSubTitles', 'true') == 'true';
|
2014-04-01 11:34:12 -05:00
|
|
|
subLabel='selectable_objects'">:field.pxViewList</x>''')
|
2014-02-26 03:40:27 -06:00
|
|
|
|
2014-06-15 17:58:45 -05:00
|
|
|
# PX that displays referred objects as dropdown menus.
|
|
|
|
pxMenu = Px('''
|
|
|
|
<img if="menu.icon" src=":menu.icon" title=":menu.text"/><x
|
|
|
|
if="not menu.icon">:menu.text</x>
|
|
|
|
<!-- Nb of objects in the menu -->
|
|
|
|
<x>:len(menu.objects)</x>''')
|
2014-02-26 03:40:27 -06:00
|
|
|
|
2014-06-15 17:58:45 -05:00
|
|
|
pxViewMenus = Px('''
|
|
|
|
<x var2="dtc='display: table-cell'; inMenu=True">
|
|
|
|
<!-- No object is present -->
|
|
|
|
<div if="not objects" style=":'padding-left: 3px; %s' % dtc"
|
|
|
|
class="discreet">-</div>
|
2014-02-26 03:40:27 -06:00
|
|
|
|
2014-06-15 17:58:45 -05:00
|
|
|
<!-- One menu for every object type -->
|
|
|
|
<div for="menu in field.getLinkedObjectsByMenu(obj, objects)"
|
|
|
|
style=":not loop.menu.last and ('%s;padding-right:4px') % dtc or dtc">
|
|
|
|
<div class="dropdownMenu"
|
|
|
|
var2="dropdownId='%s_%s_%d' % (zobj.id, name, loop.menu.nb);
|
|
|
|
singleObject=len(menu.objects) == 1"
|
2014-02-26 03:40:27 -06:00
|
|
|
onmouseover=":'toggleDropdown(%s)' % q(dropdownId)"
|
|
|
|
onmouseout=":'toggleDropdown(%s,%s)' % (q(dropdownId), q('none'))">
|
2014-06-15 17:58:45 -05:00
|
|
|
|
|
|
|
<!-- The menu name and/or icon, that is clickable if there is a single
|
|
|
|
object in the menu. -->
|
|
|
|
<x if="singleObject" var2="tied=menu.objects[0]">
|
|
|
|
<a if="field.menuUrlMethod" class="dropdownMenu"
|
|
|
|
href=":field.getMenuUrl(zobj, tied)"
|
|
|
|
title=":tied.title">:field.pxMenu</a>
|
|
|
|
<a if="not field.menuUrlMethod" class="dropdownMenu"
|
|
|
|
var2="linkInPopup=inPopup or (target.target != '_self')"
|
|
|
|
target=":target.target" onclick=":target.openPopup"
|
|
|
|
href=":tied.o.getUrl(nav='',inPopup=linkInPopup)"
|
|
|
|
title=":tied.title">:field.pxMenu</a>
|
|
|
|
</x>
|
|
|
|
<x if="not singleObject">:field.pxMenu</x>
|
|
|
|
|
|
|
|
<!-- The dropdown menu containing tied objects -->
|
2014-05-29 06:32:19 -05:00
|
|
|
<div id=":dropdownId" class="dropdown" style="width:150px">
|
2014-04-01 11:34:12 -05:00
|
|
|
<div for="tied in menu.objects"
|
2014-06-15 17:58:45 -05:00
|
|
|
var2="startNumber=0;
|
|
|
|
totalNumber=len(menu.objects);
|
|
|
|
tiedUid=tied.uid"
|
|
|
|
class=":not loop.tied.first and 'refMenuItem' or ''">
|
|
|
|
<!-- A specific link may have to be computed from
|
|
|
|
field.menuUrlMethod -->
|
|
|
|
<a if="field.menuUrlMethod"
|
|
|
|
href=":field.getMenuUrl(zobj, tied)">:tied.title</a>
|
|
|
|
<!-- Show standard pxObjectTitle else -->
|
|
|
|
<x if="not field.menuUrlMethod">:field.pxObjectTitle</x>
|
|
|
|
<div if="tied.o.mayAct()">:field.pxObjectActions</div>
|
2014-02-26 03:40:27 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2014-06-15 17:58:45 -05:00
|
|
|
</div><x>:field.pxAdd</x></x> ''')
|
2014-02-26 03:40:27 -06:00
|
|
|
|
2014-05-19 05:12:33 -05:00
|
|
|
# Simplified widget showing minimal info about tied objects.
|
|
|
|
pxViewMinimal = Px('''
|
|
|
|
<x var2="infos=[field.getReferenceLabel(o, True) \
|
|
|
|
for o in objects]">:', '.join(infos) or _('no_ref')</x>''')
|
2014-04-30 14:08:42 -05:00
|
|
|
|
2014-04-03 10:32:57 -05:00
|
|
|
# PX that displays referred objects through this field. In mode link="list",
|
|
|
|
# if, in the request, key "scope" is present and holds value "objs", the
|
|
|
|
# pick list (containing possible values) will not be rendered.
|
2014-02-26 03:40:27 -06:00
|
|
|
pxView = Px('''
|
2014-04-01 11:34:12 -05:00
|
|
|
<x var="innerRef=req.get('innerRef', False) == 'True';
|
|
|
|
ajaxHookId='%s_%s_objs' % (zobj.id, field.name);
|
2014-04-19 12:58:02 -05:00
|
|
|
layoutType=layoutType|'view';
|
2014-04-30 14:08:42 -05:00
|
|
|
render=field.getRenderMode(layoutType);
|
2014-04-01 11:34:12 -05:00
|
|
|
linkList=field.link == 'list';
|
2014-04-03 10:32:57 -05:00
|
|
|
renderAll=req.get('scope') != 'objs';
|
2014-04-01 11:34:12 -05:00
|
|
|
inPickList=False;
|
2014-06-15 17:58:45 -05:00
|
|
|
inMenu=False;
|
2014-04-01 11:34:12 -05:00
|
|
|
startNumber=field.getStartNumber(render, req, ajaxHookId);
|
|
|
|
info=field.getValue(zobj,startNumber=startNumber,someObjects=True);
|
|
|
|
objects=info.objects;
|
|
|
|
totalNumber=info.totalNumber;
|
2014-04-15 12:52:18 -05:00
|
|
|
numberWidth=len(str(totalNumber));
|
2014-04-01 11:34:12 -05:00
|
|
|
batchSize=info.batchSize;
|
|
|
|
batchNumber=len(objects);
|
|
|
|
folder=zobj.getCreateFolder();
|
|
|
|
tiedClassName=ztool.getPortalType(field.klass);
|
2014-06-15 17:58:45 -05:00
|
|
|
target=ztool.getLinksTargetInfo(field.klass);
|
2014-05-03 15:45:51 -05:00
|
|
|
mayEdit=not field.isBack and zobj.mayEdit(field.writePermission);
|
|
|
|
mayUnlink=mayEdit and field.getAttribute(zobj, 'unlink');
|
|
|
|
mayAdd=mayEdit and field.mayAdd(zobj, checkMayEdit=False);
|
2014-07-28 05:29:16 -05:00
|
|
|
mayLink=mayEdit and field.mayAdd(zobj, mode='link', \
|
|
|
|
checkMayEdit=False);
|
2014-04-01 11:34:12 -05:00
|
|
|
addConfirmMsg=field.addConfirm and \
|
|
|
|
_('%s_addConfirm' % field.labelId) or '';
|
2014-07-28 05:29:16 -05:00
|
|
|
navBaseCall='askRefField(%s,%s,%s,**v**)' % \
|
|
|
|
(q(ajaxHookId), q(zobj.absolute_url()), q(innerRef));
|
2014-05-03 15:45:51 -05:00
|
|
|
changeOrder=mayEdit and field.getAttribute(zobj, 'changeOrder');
|
2014-10-07 08:03:07 -05:00
|
|
|
sortConfirm=changeOrder and _('sort_confirm');
|
2014-04-15 12:52:18 -05:00
|
|
|
numbered=field.isNumbered(zobj);
|
2014-10-21 02:25:37 -05:00
|
|
|
gotoNumber=numbered;
|
2014-05-03 15:45:51 -05:00
|
|
|
changeNumber=not inPickList and numbered and changeOrder and \
|
|
|
|
(totalNumber > 3);
|
2014-04-19 12:58:02 -05:00
|
|
|
checkboxesEnabled=field.getAttribute(zobj, 'checkboxes') and \
|
|
|
|
(layoutType != 'cell');
|
2014-04-04 09:49:22 -05:00
|
|
|
checkboxes=checkboxesEnabled and (totalNumber > 1);
|
2014-04-01 11:34:12 -05:00
|
|
|
showSubTitles=req.get('showSubTitles', 'true') == 'true'">
|
2014-04-03 10:32:57 -05:00
|
|
|
<!-- JS tables storing checkbox statuses if checkboxes are enabled -->
|
2014-05-19 05:12:33 -05:00
|
|
|
<script if="checkboxesEnabled and renderAll and (render == 'list')"
|
2014-04-03 10:32:57 -05:00
|
|
|
type="text/javascript">:field.getCbJsInit(zobj)</script>
|
2014-05-03 15:45:51 -05:00
|
|
|
<div if="linkList and renderAll and mayEdit"
|
2014-04-03 10:32:57 -05:00
|
|
|
var2="ajaxHookId='%s_%s_poss' % (zobj.id, field.name)"
|
|
|
|
id=":ajaxHookId">:field.pxViewPickList</div>
|
2014-04-01 11:34:12 -05:00
|
|
|
<x if="render == 'list'"
|
2014-04-03 10:32:57 -05:00
|
|
|
var2="subLabel=linkList and 'selected_objects' or None">
|
|
|
|
<div if="renderAll" id=":ajaxHookId">:field.pxViewList</div>
|
|
|
|
<x if="not renderAll">:field.pxViewList</x>
|
|
|
|
</x>
|
2014-05-19 05:12:33 -05:00
|
|
|
<x if="render in ('menus','minimal')">:getattr(field, 'pxView%s' % \
|
2014-04-30 14:08:42 -05:00
|
|
|
render.capitalize())</x>
|
2014-04-01 11:34:12 -05:00
|
|
|
</x>''')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-04-30 14:08:42 -05:00
|
|
|
pxCell = pxView
|
2014-07-23 15:29:47 -05:00
|
|
|
|
|
|
|
# Edit widget, for Refs with link='popup'.
|
|
|
|
pxEditPopup = Px('''
|
|
|
|
<x var="objects=field.getPopupObjects(obj, req, requestValue)">
|
|
|
|
<!-- The select field allowing to store the selected objects -->
|
|
|
|
<select if="objects" name=":name" id=":name" multiple=":isMultiple"
|
|
|
|
size=":isMultiple and field.height or ''">
|
|
|
|
<option for="tied in objects" value=":tied.uid" selected="selected"
|
|
|
|
var2="title=field.getReferenceLabel(tied, unlimited=True)"
|
|
|
|
title=":title">:ztool.truncateValue(title, field.width)</option>
|
|
|
|
</select>
|
|
|
|
<div if="not objects">-</div>
|
|
|
|
<!-- The button for opening the popup -->
|
2014-07-28 05:29:16 -05:00
|
|
|
<x var="popupMode='repl'">:field.pxLink</x></x>''')
|
2014-07-23 15:29:47 -05:00
|
|
|
|
|
|
|
pxEdit = Px('''
|
|
|
|
<x if="(field.link) and (field.link != 'list')">
|
|
|
|
<select if="field.link != 'popup'"
|
|
|
|
var2="objects=field.getPossibleValues(zobj);
|
|
|
|
uids=[o.id for o in field.getValue(zobj, appy=False)]"
|
|
|
|
name=":name" id=":name" size=":isMultiple and field.height or ''"
|
|
|
|
onchange=":field.getOnChange(zobj, layoutType)"
|
|
|
|
multiple=":isMultiple">
|
|
|
|
<option value="" if="not isMultiple">:_('choose_a_value')</option>
|
|
|
|
<option for="tied in objects"
|
|
|
|
var2="uid=tied.uid;
|
|
|
|
title=field.getReferenceLabel(tied, unlimited=True)"
|
|
|
|
selected=":inRequest and (uid in requestValue) or \
|
|
|
|
(uid in uids)" value=":uid"
|
|
|
|
title=":title">:ztool.truncateValue(title, field.width)</option>
|
|
|
|
</select>
|
|
|
|
<x if="field.link == 'popup'">:field.pxEditPopup</x></x>''')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-03-05 06:25:36 -06:00
|
|
|
pxSearch = Px('''
|
2013-07-15 04:23:29 -05:00
|
|
|
<!-- The "and" / "or" radio buttons -->
|
|
|
|
<x if="field.multiplicity[1] != 1"
|
|
|
|
var2="operName='o_%s' % name;
|
2013-07-08 16:39:16 -05:00
|
|
|
orName='%s_or' % operName;
|
2013-07-15 04:23:29 -05:00
|
|
|
andName='%s_and' % operName">
|
|
|
|
<input type="radio" name=":operName" id=":orName" checked="checked"
|
|
|
|
value="or"/>
|
2013-08-21 05:35:30 -05:00
|
|
|
<label lfor=":orName">:_('search_or')</label>
|
2013-07-15 04:23:29 -05:00
|
|
|
<input type="radio" name=":operName" id=":andName" value="and"/>
|
2013-08-21 05:35:30 -05:00
|
|
|
<label lfor=":andName">:_('search_and')</label><br/>
|
2013-07-15 04:23:29 -05:00
|
|
|
</x>
|
|
|
|
<!-- The list of values -->
|
2014-04-01 11:34:12 -05:00
|
|
|
<select var="objects=field.getPossibleValues(ztool);
|
2014-03-05 06:25:36 -06:00
|
|
|
selectAll='masterValues' in req"
|
|
|
|
name=":widgetName" size=":field.sheight" multiple="multiple"
|
|
|
|
onchange=":field.getOnChange(ztool, 'search', className)">
|
|
|
|
<option for="tied in objects" value=":tied.uid" selected=":selectAll"
|
|
|
|
var2="title=field.getReferenceLabel(tied, unlimited=True)"
|
|
|
|
title=":title">:ztool.truncateValue(title, field.swidth)</option>
|
|
|
|
</select>''')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
|
|
|
def __init__(self, klass=None, attribute=None, validator=None,
|
|
|
|
multiplicity=(0,1), default=None, add=False, addConfirm=False,
|
2014-07-10 02:46:39 -05:00
|
|
|
delete=None, noForm=False, link=True, unlink=None,
|
|
|
|
unlinkElement=None, insert=None, beforeLink=None,
|
|
|
|
afterLink=None, afterUnlink=None, back=None, show=True,
|
2014-06-25 08:42:34 -05:00
|
|
|
page='main', group=None, layouts=None, showHeaders=False,
|
|
|
|
shownInfo=(), select=None, maxPerPage=30, move=0,
|
|
|
|
indexed=False, searchable=False, specificReadPermission=False,
|
|
|
|
specificWritePermission=False, width=None, height=5,
|
|
|
|
maxChars=None, colspan=1, master=None, masterValue=None,
|
|
|
|
focus=False, historized=False, mapping=None, label=None,
|
|
|
|
queryable=False, queryFields=None, queryNbCols=1,
|
2014-04-15 12:52:18 -05:00
|
|
|
navigable=False, changeOrder=True, numbered=False,
|
|
|
|
checkboxes=True, checkboxesDefault=None, sdefault='',
|
|
|
|
scolspan=1, swidth=None, sheight=None, sselect=None,
|
|
|
|
persist=True, render='list', menuIdMethod=None,
|
|
|
|
menuInfoMethod=None, menuUrlMethod=None):
|
2013-07-08 16:39:16 -05:00
|
|
|
self.klass = klass
|
|
|
|
self.attribute = attribute
|
2014-06-24 10:07:59 -05:00
|
|
|
# May the user add new objects through this ref ? "add" may also contain
|
|
|
|
# a method whose result must be a boolean value.
|
2013-07-08 16:39:16 -05:00
|
|
|
self.add = add
|
|
|
|
# When the user adds a new object, must a confirmation popup be shown?
|
|
|
|
self.addConfirm = addConfirm
|
|
|
|
# May the user delete objects via this Ref?
|
|
|
|
self.delete = delete
|
|
|
|
if delete == None:
|
|
|
|
# By default, one may delete objects via a Ref for which one can
|
|
|
|
# add objects.
|
|
|
|
self.delete = bool(self.add)
|
|
|
|
# If noForm is True, when clicking to create an object through this ref,
|
|
|
|
# the object will be created automatically, and no creation form will
|
|
|
|
# be presented to the user.
|
|
|
|
self.noForm = noForm
|
2014-04-01 11:34:12 -05:00
|
|
|
# May the user link existing objects through this ref? If "link" is;
|
|
|
|
# True, the user will, on the edit page, choose objects from a
|
|
|
|
# dropdown menu;
|
|
|
|
# "list", the user will, on the view page, choose objects from a list
|
|
|
|
# of objects which is similar to those rendered in pxViewList;
|
|
|
|
# "popup", the user will, on the edit page, choose objects from a popup
|
2014-07-18 09:54:11 -05:00
|
|
|
# menu. In this case, parameter "select" must hold a Search
|
|
|
|
# instance.
|
2013-07-08 16:39:16 -05:00
|
|
|
self.link = link
|
|
|
|
# May the user unlink existing objects?
|
|
|
|
self.unlink = unlink
|
2014-06-25 08:42:34 -05:00
|
|
|
if unlink == None:
|
|
|
|
# By default, one may unlink objects via a Ref for which one can
|
|
|
|
# link objects.
|
|
|
|
self.unlink = bool(self.link)
|
2014-07-10 02:46:39 -05:00
|
|
|
# "unlink" above is a global flag. If it is True, you can go further and
|
|
|
|
# determine, for every linked object, if it can be unlinked or not by
|
|
|
|
# defining a method in parameter "unlinkElement" below. This method
|
|
|
|
# accepts the linked object as unique arg.
|
|
|
|
self.unlinkElement = unlinkElement
|
2014-06-24 10:07:59 -05:00
|
|
|
# When an object is inserted through this Ref field, at what position is
|
|
|
|
# it inserted? If "insert" is:
|
|
|
|
# None, it will be inserted at the end;
|
|
|
|
# "start", it will be inserted at the start of the tied objects;
|
|
|
|
# a method, (called with the object to insert as single arg), its return
|
|
|
|
# value (a number or a tuple of numbers) will be
|
|
|
|
# used to insert the object at the corresponding position
|
|
|
|
# (this method will also be applied to other objects to know
|
|
|
|
# where to insert the new one);
|
|
|
|
# a tuple, ('sort', method), the given method (called with the object
|
|
|
|
# to insert as single arg) will be used to sort tied objects
|
|
|
|
# and will be given as param "key" of the standard Python
|
|
|
|
# method "sort" applied on the list of tied objects.
|
|
|
|
# With value ('sort', method), a full sort is performed and may hardly
|
|
|
|
# reshake the tied objects; with value "method" alone, the tied
|
|
|
|
# object is inserted at some given place: tied objects are more
|
|
|
|
# maintained in the order of their insertion.
|
|
|
|
self.insert = insert
|
2014-06-25 08:42:34 -05:00
|
|
|
# Immediately before an object is going to be linked via this Ref field,
|
2014-07-10 02:46:39 -05:00
|
|
|
# method potentially specified in "beforeLink" will be executed and will
|
2014-06-25 08:42:34 -05:00
|
|
|
# take the object to link as single parameter.
|
|
|
|
self.beforeLink = beforeLink
|
2014-07-10 02:46:39 -05:00
|
|
|
# Immediately after an object has been linked via this Ref field, method
|
|
|
|
# potentially specified in "afterLink" will be executed and will take
|
|
|
|
# the linked object as single parameter.
|
|
|
|
self.afterLink = afterLink
|
2014-06-25 08:42:34 -05:00
|
|
|
# Immediately after an object as been unlinked from this Ref field,
|
2014-07-10 02:46:39 -05:00
|
|
|
# method potentially specified in "afterUnlink" will be executed and
|
2014-06-25 08:42:34 -05:00
|
|
|
# will take the unlinked object as single parameter.
|
|
|
|
self.afterUnlink = afterUnlink
|
2013-07-08 16:39:16 -05:00
|
|
|
self.back = None
|
|
|
|
if back:
|
|
|
|
# It is a forward reference
|
|
|
|
self.isBack = False
|
|
|
|
# Initialise the backward reference
|
|
|
|
self.back = back
|
|
|
|
back.back = self
|
|
|
|
# klass may be None in the case we are defining an auto-Ref to the
|
|
|
|
# same class as the class where this field is defined. In this case,
|
|
|
|
# when defining the field within the class, write
|
|
|
|
# myField = Ref(None, ...)
|
|
|
|
# and, at the end of the class definition (name it K), write:
|
|
|
|
# K.myField.klass = K
|
|
|
|
# setattr(K, K.myField.back.attribute, K.myField.back)
|
|
|
|
if klass: setattr(klass, back.attribute, back)
|
2014-07-18 09:54:11 -05:00
|
|
|
else:
|
|
|
|
self.isBack = True
|
2013-07-08 16:39:16 -05:00
|
|
|
# When displaying a tabular list of referenced objects, must we show
|
|
|
|
# the table headers?
|
|
|
|
self.showHeaders = showHeaders
|
|
|
|
# When displaying referenced object(s), we will display its title + all
|
|
|
|
# other fields whose names are listed in the following attribute.
|
|
|
|
self.shownInfo = list(shownInfo)
|
|
|
|
if not self.shownInfo: self.shownInfo.append('title')
|
|
|
|
# If a method is defined in this field "select", it will be used to
|
2014-03-05 08:47:12 -06:00
|
|
|
# return the list of possible tied objects. Be careful: this method can
|
|
|
|
# receive, in its first argument ("self"), the tool instead of an
|
|
|
|
# instance of the class where this field is defined. This little cheat
|
|
|
|
# is:
|
|
|
|
# - not really a problem: in this method you will mainly use methods
|
|
|
|
# that are available on a tool as well as on any object (like
|
|
|
|
# "search");
|
|
|
|
# - necessary because in some cases we do not have an instance at our
|
|
|
|
# disposal, ie, when we need to compute a list of objects on a
|
|
|
|
# search screen.
|
2014-07-18 09:54:11 -05:00
|
|
|
# "select" can also hold a Search instance. In this case, put any name
|
|
|
|
# in Search's mandatory parameter "name": it will be ignored and
|
|
|
|
# replaced with an internal technical name.
|
2014-03-05 08:47:12 -06:00
|
|
|
# NOTE that when a method is defined in field "masterValue" (see parent
|
|
|
|
# class "Field"), it will be used instead of select (or sselect below).
|
2013-07-08 16:39:16 -05:00
|
|
|
self.select = select
|
2014-07-19 06:42:39 -05:00
|
|
|
if isinstance(select, Search):
|
|
|
|
select.name = '_field_'
|
|
|
|
select.checkboxes = True
|
|
|
|
select.checkboxesDefault = False
|
2014-03-05 08:47:12 -06:00
|
|
|
# If you want to specify, for the search screen, a list of objects that
|
|
|
|
# is different from the one produced by self.select, define an
|
|
|
|
# alternative method in field "sselect" below.
|
|
|
|
self.sselect = sselect or self.select
|
2013-07-08 16:39:16 -05:00
|
|
|
# Maximum number of referenced objects shown at once.
|
|
|
|
self.maxPerPage = maxPerPage
|
|
|
|
# If param p_queryable is True, the user will be able to perform queries
|
|
|
|
# from the UI within referenced objects.
|
|
|
|
self.queryable = queryable
|
|
|
|
# Here is the list of fields that will appear on the search screen.
|
|
|
|
# If None is specified, by default we take every indexed field
|
|
|
|
# defined on referenced objects' class.
|
|
|
|
self.queryFields = queryFields
|
|
|
|
# The search screen will have this number of columns
|
|
|
|
self.queryNbCols = queryNbCols
|
|
|
|
# Within the portlet, will referred elements appear ?
|
|
|
|
self.navigable = navigable
|
2014-04-15 12:52:18 -05:00
|
|
|
# If "changeOrder" is or returns False, it even if the user has the
|
|
|
|
# right to modify the field, it will not be possible to move objects or
|
|
|
|
# sort them.
|
2013-07-08 16:39:16 -05:00
|
|
|
self.changeOrder = changeOrder
|
2014-04-15 12:52:18 -05:00
|
|
|
# If "numbered" is or returns True, a leading column will show the
|
|
|
|
# number of every tied object. Moreover, if the user can change order of
|
|
|
|
# tied objects, an input field will allow him to enter a new number for
|
|
|
|
# the tied object. If "numbered" is or returns a string, it will be used
|
|
|
|
# as width for the column containing the number. Else, a default width
|
|
|
|
# will be used.
|
|
|
|
self.numbered = numbered
|
|
|
|
# If "checkboxes" is or returns True, every linked object will be
|
|
|
|
# "selectable" via a checkbox. Global actions will be activated and will
|
|
|
|
# act on the subset of selected objects: delete, unlink, etc.
|
2014-04-03 10:32:57 -05:00
|
|
|
self.checkboxes = checkboxes
|
2014-04-05 02:49:54 -05:00
|
|
|
# Default value for checkboxes, if enabled.
|
|
|
|
if checkboxesDefault == None:
|
|
|
|
self.checkboxesDefault = bool(self.link)
|
|
|
|
else:
|
|
|
|
self.checkboxesDefault = checkboxesDefault
|
2014-02-26 03:40:27 -06:00
|
|
|
# There are different ways to render a bunch of linked objects:
|
|
|
|
# - "list" (the default) renders them as a list (=a XHTML table);
|
|
|
|
# - "menus" renders them as a series of popup menus, grouped by type.
|
|
|
|
# Note that render mode "menus" will only be applied in "cell" layouts.
|
|
|
|
# Indeed, we need to keep the "list" rendering in the "view" layout
|
|
|
|
# because the "menus" rendering is minimalist and does not allow to
|
2014-04-30 14:08:42 -05:00
|
|
|
# perform all operations on linked objects (add, move, delete, edit...);
|
2014-05-19 05:12:33 -05:00
|
|
|
# - "minimal" renders a list of comma-separated, not-even-clickable,
|
|
|
|
# data about the tied objects (according to shownInfo).
|
2014-02-26 03:40:27 -06:00
|
|
|
self.render = render
|
|
|
|
# If render is 'menus', 2 methods must be provided.
|
|
|
|
# "menuIdMethod" will be called, with every linked object as single arg,
|
|
|
|
# and must return an ID that identifies the menu into which the object
|
|
|
|
# will be inserted.
|
|
|
|
self.menuIdMethod = menuIdMethod
|
|
|
|
# "menuInfoMethod" will be called with every collected menu ID (from
|
|
|
|
# calls to the previous method) to get info about this menu. This info
|
|
|
|
# must be a tuple (text, icon):
|
|
|
|
# - "text" is the menu name;
|
|
|
|
# - "icon" (can be None) gives the URL of an icon, if you want to render
|
|
|
|
# the menu as an icon instead of a text.
|
|
|
|
self.menuInfoMethod = menuInfoMethod
|
|
|
|
# "menuUrlMethod" is an optional method that allows to compute an
|
|
|
|
# alternative URL for the tied object that is shown within the menu.
|
|
|
|
self.menuUrlMethod = menuUrlMethod
|
2013-07-08 16:39:16 -05:00
|
|
|
Field.__init__(self, validator, multiplicity, default, show, page,
|
|
|
|
group, layouts, move, indexed, False,
|
|
|
|
specificReadPermission, specificWritePermission, width,
|
|
|
|
height, None, colspan, master, masterValue, focus,
|
2014-03-05 09:19:11 -06:00
|
|
|
historized, mapping, label, sdefault, scolspan, swidth,
|
|
|
|
sheight, persist)
|
2014-04-01 11:34:12 -05:00
|
|
|
self.validable = bool(self.link)
|
2014-07-18 09:54:11 -05:00
|
|
|
self.checkParameters()
|
|
|
|
|
|
|
|
def checkParameters(self):
|
|
|
|
'''Ensures this Ref is correctly defined.'''
|
|
|
|
# For forward Refs, "add" and "link" can't both be used.
|
|
|
|
if not self.isBack and (self.add and self.link):
|
|
|
|
raise Exception('Parameters "add" and "link" can\'t both be used.')
|
|
|
|
# If link is "popup", "select" must hold a Search instance.
|
|
|
|
if (self.link == 'popup') and not isinstance(self.select, Search):
|
|
|
|
raise Exception('When "link" is "popup", "select" must be a ' \
|
|
|
|
'appy.fields.search.Search instance.')
|
2013-07-08 16:39:16 -05:00
|
|
|
|
|
|
|
def getDefaultLayouts(self):
|
|
|
|
return {'view': Table('l-f', width='100%'), 'edit': 'lrv-f'}
|
|
|
|
|
|
|
|
def isShowable(self, obj, layoutType):
|
|
|
|
res = Field.isShowable(self, obj, layoutType)
|
|
|
|
if not res: return res
|
|
|
|
# We add here specific Ref rules for preventing to show the field under
|
|
|
|
# some inappropriate circumstances.
|
2014-04-01 11:34:12 -05:00
|
|
|
if layoutType == 'edit':
|
2014-05-03 15:45:51 -05:00
|
|
|
if self.mayAdd(obj): return
|
|
|
|
if self.link in (False, 'list'): return
|
2013-07-08 16:39:16 -05:00
|
|
|
if self.isBack:
|
2014-05-03 15:45:51 -05:00
|
|
|
if layoutType == 'edit': return
|
2013-07-08 16:39:16 -05:00
|
|
|
else: return getattr(obj.aq_base, self.name, None)
|
|
|
|
return res
|
|
|
|
|
2014-04-01 11:34:12 -05:00
|
|
|
def getValue(self, obj, appy=True, noListIfSingleObj=False,
|
2013-07-08 16:39:16 -05:00
|
|
|
startNumber=None, someObjects=False):
|
2014-04-01 11:34:12 -05:00
|
|
|
'''Returns the objects linked to p_obj through this Ref field. It
|
|
|
|
returns Appy wrappers if p_appy is True, the Zope objects else.
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-04-01 11:34:12 -05:00
|
|
|
* If p_startNumber is None, it returns all referred objects;
|
|
|
|
* if p_startNumber is a number, it returns self.maxPerPage objects,
|
2013-07-08 16:39:16 -05:00
|
|
|
starting at p_startNumber.
|
|
|
|
|
|
|
|
If p_noListIfSingleObj is True, it returns the single reference as
|
|
|
|
an object and not as a list.
|
|
|
|
|
|
|
|
If p_someObjects is True, it returns an instance of SomeObjects
|
|
|
|
instead of returning a list of references.'''
|
|
|
|
uids = getattr(obj.aq_base, self.name, [])
|
|
|
|
if not uids:
|
|
|
|
# Maybe is there a default value?
|
|
|
|
defValue = Field.getValue(self, obj)
|
|
|
|
if defValue:
|
2014-04-01 11:34:12 -05:00
|
|
|
if type(defValue) in sutils.sequenceTypes:
|
|
|
|
uids = [o.o.id for o in defValue]
|
2013-07-08 16:39:16 -05:00
|
|
|
else:
|
2014-04-01 11:34:12 -05:00
|
|
|
uids = [defValue.o.id]
|
2013-07-08 16:39:16 -05:00
|
|
|
# Prepare the result: an instance of SomeObjects, that will be unwrapped
|
|
|
|
# if not required.
|
|
|
|
res = gutils.SomeObjects()
|
|
|
|
res.totalNumber = res.batchSize = len(uids)
|
|
|
|
batchNeeded = startNumber != None
|
|
|
|
if batchNeeded:
|
|
|
|
res.batchSize = self.maxPerPage
|
|
|
|
if startNumber != None:
|
|
|
|
res.startNumber = startNumber
|
|
|
|
# Get the objects given their uids
|
|
|
|
i = res.startNumber
|
|
|
|
while i < (res.startNumber + res.batchSize):
|
|
|
|
if i >= res.totalNumber: break
|
|
|
|
# Retrieve every reference in the correct format according to p_type
|
2014-04-01 11:34:12 -05:00
|
|
|
tied = obj.getTool().getObject(uids[i])
|
|
|
|
if appy: tied = tied.appy()
|
|
|
|
res.objects.append(tied)
|
2013-07-08 16:39:16 -05:00
|
|
|
i += 1
|
|
|
|
# Manage parameter p_noListIfSingleObj
|
|
|
|
if res.objects and noListIfSingleObj:
|
|
|
|
if self.multiplicity[1] == 1:
|
|
|
|
res.objects = res.objects[0]
|
|
|
|
if someObjects: return res
|
|
|
|
return res.objects
|
|
|
|
|
2014-09-14 14:40:35 -05:00
|
|
|
def getCopyValue(self, obj):
|
|
|
|
'''Here, as "value ready-to-copy", we return the list of tied object
|
|
|
|
UIDs, because m_store on the destination object can store tied
|
|
|
|
objects based on such a list.'''
|
|
|
|
res = getattr(obj.aq_base, self.name, ())
|
|
|
|
# Return a copy: it can be dangerous to give the real database value.
|
|
|
|
if res: return list(res)
|
|
|
|
|
2014-04-01 11:34:12 -05:00
|
|
|
def getPossibleValues(self, obj, startNumber=None, someObjects=False,
|
|
|
|
removeLinked=False):
|
|
|
|
'''This method returns the list of all objects that can be selected
|
|
|
|
to be linked as references to p_obj via p_self. It is applicable only
|
|
|
|
for Ref fields with link!=False. If master values are present in the
|
|
|
|
request, we use field.masterValues method instead of self.select.
|
2013-07-10 02:56:35 -05:00
|
|
|
|
2014-04-01 11:34:12 -05:00
|
|
|
If p_startNumber is a number, it returns self.maxPerPage objects,
|
|
|
|
starting at p_startNumber. If p_someObjects is True, it returns an
|
|
|
|
instance of SomeObjects instead of returning a list of objects.
|
|
|
|
|
|
|
|
If p_removeLinked is True, we remove, from the result, objects which
|
|
|
|
are already linked. For example, for Ref fields rendered as a
|
|
|
|
dropdown menu or a multi-selection box (with link=True), on the edit
|
|
|
|
page, we need to display all possible values: those that are already
|
|
|
|
linked appear to be selected in the widget. But for Ref fields
|
|
|
|
rendered as pick lists (link="list"), once an object is linked, it
|
|
|
|
must disappear from the "pick list".
|
|
|
|
'''
|
|
|
|
req = obj.REQUEST
|
|
|
|
obj = obj.appy()
|
2014-07-18 09:54:11 -05:00
|
|
|
paginated = startNumber != None
|
|
|
|
isSearch = False
|
2014-04-01 11:34:12 -05:00
|
|
|
if 'masterValues' in req:
|
|
|
|
# Convert masterValue(s) from id(s) to real object(s).
|
|
|
|
masterValues = req['masterValues'].strip()
|
|
|
|
if not masterValues: masterValues = None
|
|
|
|
else:
|
|
|
|
masterValues = masterValues.split('*')
|
|
|
|
tool = obj.tool
|
|
|
|
if len(masterValues) == 1:
|
|
|
|
masterValues = tool.getObject(masterValues[0])
|
|
|
|
else:
|
|
|
|
masterValues = [tool.getObject(v) for v in masterValues]
|
|
|
|
objects = self.masterValue(obj, masterValues)
|
|
|
|
else:
|
|
|
|
# If this field is an ajax-updatable slave, no need to compute
|
|
|
|
# possible values: it will be overridden by method self.masterValue
|
|
|
|
# by a subsequent ajax request (=the "if" statement above).
|
|
|
|
if self.masterValue and callable(self.masterValue):
|
|
|
|
objects = []
|
|
|
|
else:
|
|
|
|
if not self.select:
|
2014-07-18 09:54:11 -05:00
|
|
|
# No select method or search has been defined: we must
|
|
|
|
# retrieve all objects of the referred type that the user
|
|
|
|
# is allowed to access.
|
2014-04-01 11:34:12 -05:00
|
|
|
objects = obj.search(self.klass)
|
|
|
|
else:
|
2014-07-18 09:54:11 -05:00
|
|
|
if isinstance(self.select, Search):
|
|
|
|
isSearch = True
|
|
|
|
maxResults = paginated and self.maxPerPage or 'NO_LIMIT'
|
|
|
|
start = startNumber or 0
|
|
|
|
className = obj.tool.o.getPortalType(self.klass)
|
|
|
|
objects = obj.o.executeQuery(className,
|
|
|
|
startNumber=start, search=self.select,
|
|
|
|
maxResults=maxResults)
|
|
|
|
objects.objects = [o.appy() for o in objects.objects]
|
|
|
|
else:
|
|
|
|
objects = self.select(obj)
|
2014-04-01 11:34:12 -05:00
|
|
|
# Remove already linked objects if required.
|
|
|
|
if removeLinked:
|
|
|
|
uids = getattr(obj.o.aq_base, self.name, None)
|
|
|
|
if uids:
|
|
|
|
# Browse objects in reverse order and remove linked objects.
|
2014-07-18 09:54:11 -05:00
|
|
|
if isSearch: objs = objects.objects
|
|
|
|
else: objs = objects
|
|
|
|
i = len(objs) - 1
|
2014-04-01 11:34:12 -05:00
|
|
|
while i >= 0:
|
2014-07-18 09:54:11 -05:00
|
|
|
if objs[i].id in uids: del objs[i]
|
2014-04-01 11:34:12 -05:00
|
|
|
i -= 1
|
2014-07-18 09:54:11 -05:00
|
|
|
# If possible values are not retrieved from a Search, restrict (if
|
|
|
|
# required) the result to self.maxPerPage starting at p_startNumber.
|
|
|
|
# Indeed, in this case, unlike m_getValue, we already have all objects
|
|
|
|
# in "objects": we can't limit objects "waking up" to at most
|
2014-04-01 11:34:12 -05:00
|
|
|
# self.maxPerPage.
|
2014-07-18 09:54:11 -05:00
|
|
|
if paginated and not isSearch:
|
|
|
|
total = len(objects)
|
2014-04-01 11:34:12 -05:00
|
|
|
objects = objects[startNumber:startNumber + self.maxPerPage]
|
|
|
|
# Return the result, wrapped in a SomeObjects instance if required.
|
2014-07-18 09:54:11 -05:00
|
|
|
if not someObjects:
|
|
|
|
if isSearch: return objects.objects
|
|
|
|
return objects
|
|
|
|
if isSearch: return objects
|
2014-04-01 11:34:12 -05:00
|
|
|
res = gutils.SomeObjects()
|
|
|
|
res.totalNumber = total
|
|
|
|
res.batchSize = self.maxPerPage
|
|
|
|
res.startNumber = startNumber
|
|
|
|
res.objects = objects
|
|
|
|
return res
|
|
|
|
|
|
|
|
def getLinkedObjectsByMenu(self, obj, objects):
|
|
|
|
'''This method groups p_objects into sub-lists of objects, grouped by
|
2014-02-26 03:40:27 -06:00
|
|
|
menu (happens when self.render == 'menus').'''
|
2014-06-15 17:58:45 -05:00
|
|
|
if not objects: return ()
|
2014-02-26 03:40:27 -06:00
|
|
|
res = []
|
|
|
|
# We store in "menuIds" the already encountered menus:
|
|
|
|
# ~{s_menuId : i_indexInRes}~
|
|
|
|
menuIds = {}
|
2014-04-01 11:34:12 -05:00
|
|
|
# Browse every object from p_objects and put them in their menu
|
2014-02-26 03:40:27 -06:00
|
|
|
# (within "res").
|
2014-04-01 11:34:12 -05:00
|
|
|
for tied in objects:
|
|
|
|
menuId = self.menuIdMethod(obj, tied)
|
2014-02-26 03:40:27 -06:00
|
|
|
if menuId in menuIds:
|
|
|
|
# We have already encountered this menu.
|
|
|
|
menuIndex = menuIds[menuId]
|
2014-04-01 11:34:12 -05:00
|
|
|
res[menuIndex].objects.append(tied)
|
2014-02-26 03:40:27 -06:00
|
|
|
else:
|
|
|
|
# A new menu.
|
2014-04-01 11:34:12 -05:00
|
|
|
menu = Object(id=menuId, objects=[tied])
|
2014-02-26 03:40:27 -06:00
|
|
|
res.append(menu)
|
|
|
|
menuIds[menuId] = len(res) - 1
|
|
|
|
# Complete information about every menu by calling self.menuInfoMethod
|
|
|
|
for menu in res:
|
|
|
|
text, icon = self.menuInfoMethod(obj, menu.id)
|
|
|
|
menu.text = text
|
|
|
|
menu.icon = icon
|
|
|
|
return res
|
|
|
|
|
2014-04-15 12:52:18 -05:00
|
|
|
def isNumbered(self, obj):
|
|
|
|
'''Must we show the order number of every tied object?'''
|
|
|
|
res = self.getAttribute(obj, 'numbered')
|
|
|
|
if not res: return res
|
|
|
|
# Returns the column width.
|
|
|
|
if not isinstance(res, basestring): return '15px'
|
|
|
|
return res
|
|
|
|
|
2014-04-01 11:34:12 -05:00
|
|
|
def getMenuUrl(self, zobj, tied):
|
|
|
|
'''We must provide the URL of the p_tied object, when shown in a Ref
|
|
|
|
field in render mode 'menus'. If self.menuUrlMethod is specified,
|
2014-02-26 03:40:27 -06:00
|
|
|
use it. Else, returns the "normal" URL of the view page for the tied
|
|
|
|
object, but without any navigation information, because in this
|
|
|
|
render mode, tied object's order is lost and navigation is
|
|
|
|
impossible.'''
|
|
|
|
if self.menuUrlMethod:
|
2014-04-01 11:34:12 -05:00
|
|
|
return self.menuUrlMethod(zobj.appy(), tied)
|
|
|
|
return tied.o.getUrl(nav='')
|
2014-02-26 03:40:27 -06:00
|
|
|
|
|
|
|
def getStartNumber(self, render, req, ajaxHookId):
|
|
|
|
'''This method returns the index of the first linked object that must be
|
|
|
|
shown, or None if all linked objects must be shown at once (it
|
|
|
|
happens when p_render is "menus").'''
|
|
|
|
# When using 'menus' render mode, all linked objects must be shown.
|
|
|
|
if render == 'menus': return
|
|
|
|
# When using 'list' (=default) render mode, the index of the first
|
|
|
|
# object to show is in the request.
|
|
|
|
return int(req.get('%s_startNumber' % ajaxHookId, 0))
|
|
|
|
|
2014-09-08 08:13:18 -05:00
|
|
|
def getFormattedValue(self, obj, value, showChanges=False, language=None):
|
2013-07-08 16:39:16 -05:00
|
|
|
return value
|
|
|
|
|
|
|
|
def getIndexType(self): return 'ListIndex'
|
|
|
|
|
|
|
|
def getIndexValue(self, obj, forSearch=False):
|
|
|
|
'''Value for indexing is the list of UIDs of linked objects. If
|
|
|
|
p_forSearch is True, it will return a list of the linked objects'
|
|
|
|
titles instead.'''
|
|
|
|
if not forSearch:
|
2014-10-06 05:51:40 -05:00
|
|
|
res = getattr(obj.aq_base, self.name, None)
|
2013-07-08 16:39:16 -05:00
|
|
|
if res:
|
2014-10-06 05:51:40 -05:00
|
|
|
# The index does not like persistent lists. Moreover, I don't
|
|
|
|
# want to give to anyone access to the persistent list in the
|
|
|
|
# DB.
|
2013-07-08 16:39:16 -05:00
|
|
|
res = list(res)
|
|
|
|
else:
|
|
|
|
# Ugly catalog: if I return an empty list, the previous value
|
|
|
|
# is kept.
|
2014-10-06 05:51:40 -05:00
|
|
|
res = ['']
|
2013-07-08 16:39:16 -05:00
|
|
|
return res
|
|
|
|
else:
|
2014-11-28 07:42:32 -06:00
|
|
|
# For the global search: return linked objects' titles
|
|
|
|
return ' '.join([o.getShownValue('title') \
|
|
|
|
for o in self.getValue(obj, appy=False)])
|
|
|
|
|
|
|
|
def hasSortIndex(self):
|
|
|
|
'''An indexed Ref field is of type "ListIndex", which is not sortable.
|
|
|
|
So an additional FieldIndex is required.'''
|
|
|
|
return True
|
2013-07-08 16:39:16 -05:00
|
|
|
|
|
|
|
def validateValue(self, obj, value):
|
2014-04-01 11:34:12 -05:00
|
|
|
if not self.link: return
|
2013-07-08 16:39:16 -05:00
|
|
|
# We only check "link" Refs because in edit views, "add" Refs are
|
|
|
|
# not visible. So if we check "add" Refs, on an "edit" view we will
|
|
|
|
# believe that that there is no referred object even if there is.
|
2014-04-01 11:34:12 -05:00
|
|
|
# Also ensure that multiplicities are enforced.
|
2013-07-08 16:39:16 -05:00
|
|
|
if not value:
|
|
|
|
nbOfRefs = 0
|
|
|
|
elif isinstance(value, basestring):
|
|
|
|
nbOfRefs = 1
|
|
|
|
else:
|
|
|
|
nbOfRefs = len(value)
|
|
|
|
minRef = self.multiplicity[0]
|
|
|
|
maxRef = self.multiplicity[1]
|
|
|
|
if maxRef == None:
|
|
|
|
maxRef = sys.maxint
|
|
|
|
if nbOfRefs < minRef:
|
|
|
|
return obj.translate('min_ref_violated')
|
|
|
|
elif nbOfRefs > maxRef:
|
|
|
|
return obj.translate('max_ref_violated')
|
|
|
|
|
2014-09-29 03:06:40 -05:00
|
|
|
def linkObject(self, obj, value, back=False, noSecurity=True,
|
|
|
|
executeMethods=True):
|
2013-07-08 16:39:16 -05:00
|
|
|
'''This method links p_value (which can be a list of objects) to p_obj
|
2014-09-29 03:06:40 -05:00
|
|
|
through this Ref field. When linking 2 objects via a Ref,
|
|
|
|
p_linkObject must be called twice: once on the forward Ref and once
|
|
|
|
on the backward ref. p_back indicates if we are calling it on the
|
|
|
|
forward or backward Ref. If p_noSecurity is True, we bypass security
|
|
|
|
checks (has the logged user the right to modify this Ref field?).
|
|
|
|
If p_executeMethods is False, we do not execute methods that
|
|
|
|
customize the object insertion (parameters insert, beforeLink,
|
|
|
|
afterLink...). This can be useful while migrating data or duplicating
|
|
|
|
an object.'''
|
2014-06-24 10:07:59 -05:00
|
|
|
zobj = obj.o
|
2014-04-03 10:32:57 -05:00
|
|
|
# Security check
|
2014-06-24 10:07:59 -05:00
|
|
|
if not noSecurity: zobj.mayEdit(self.writePermission, raiseError=True)
|
2013-07-08 16:39:16 -05:00
|
|
|
# p_value can be a list of objects
|
|
|
|
if type(value) in sutils.sequenceTypes:
|
2014-09-29 03:06:40 -05:00
|
|
|
for v in value:
|
|
|
|
self.linkObject(obj, v, back, noSecurity, executeMethods)
|
2013-07-08 16:39:16 -05:00
|
|
|
return
|
|
|
|
# Gets the list of referred objects (=list of uids), or create it.
|
2014-06-24 10:07:59 -05:00
|
|
|
refs = getattr(zobj.aq_base, self.name, None)
|
2013-07-08 16:39:16 -05:00
|
|
|
if refs == None:
|
2014-06-24 10:07:59 -05:00
|
|
|
refs = zobj.getProductConfig().PersistentList()
|
|
|
|
setattr(zobj, self.name, refs)
|
2013-07-08 16:39:16 -05:00
|
|
|
# Insert p_value into it.
|
2014-04-01 11:34:12 -05:00
|
|
|
uid = value.o.id
|
2014-06-24 10:07:59 -05:00
|
|
|
if uid in refs: return
|
2014-06-25 08:42:34 -05:00
|
|
|
# Execute self.beforeLink if present
|
2014-09-29 03:06:40 -05:00
|
|
|
if executeMethods and self.beforeLink: self.beforeLink(obj, value)
|
2014-06-24 10:07:59 -05:00
|
|
|
# Where must we insert the object?
|
2014-09-29 03:06:40 -05:00
|
|
|
if not self.insert or not executeMethods:
|
2014-06-24 10:07:59 -05:00
|
|
|
refs.append(uid)
|
|
|
|
elif self.insert == 'start':
|
|
|
|
refs.insert(0, uid)
|
|
|
|
elif callable(self.insert):
|
|
|
|
# It is a method. Use it on every tied object until we find where to
|
|
|
|
# insert the new object.
|
|
|
|
tool = zobj.getTool()
|
|
|
|
insertOrder = self.insert(obj, value)
|
|
|
|
i = 0
|
|
|
|
inserted = False
|
|
|
|
while i < len(refs):
|
|
|
|
tied = tool.getObject(refs[i], appy=True)
|
|
|
|
if self.insert(obj, tied) > insertOrder:
|
|
|
|
refs.insert(i, uid)
|
|
|
|
inserted = True
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
if not inserted: refs.append(uid)
|
|
|
|
else:
|
|
|
|
# It is a tuple ('sort', method). Perform a full sort.
|
|
|
|
refs.append(uid)
|
|
|
|
tool = zobj.getTool()
|
2014-06-24 14:05:01 -05:00
|
|
|
# Warning: "refs" is a persistent list whose method "sort" has no
|
|
|
|
# param "key".
|
|
|
|
refs.data.sort(key=lambda uid:self.insert[1](obj, \
|
2014-06-24 10:07:59 -05:00
|
|
|
tool.getObject(uid, appy=True)))
|
2014-06-24 14:05:01 -05:00
|
|
|
refs._p_changed = 1
|
2014-07-10 02:46:39 -05:00
|
|
|
# Execute self.afterLink if present
|
2014-09-29 03:06:40 -05:00
|
|
|
if executeMethods and self.afterLink: self.afterLink(obj, value)
|
2014-06-24 10:07:59 -05:00
|
|
|
# Update the back reference
|
2014-09-29 03:06:40 -05:00
|
|
|
if not back:
|
|
|
|
self.back.linkObject(value, obj, True, noSecurity, executeMethods)
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-09-29 03:06:40 -05:00
|
|
|
def unlinkObject(self, obj, value, back=False, noSecurity=True,
|
|
|
|
executeMethods=True):
|
2013-07-08 16:39:16 -05:00
|
|
|
'''This method unlinks p_value (which can be a list of objects) from
|
2014-09-29 03:06:40 -05:00
|
|
|
p_obj through this Ref field. For an explanation about parameters
|
|
|
|
p_back, p_noSecurity and p_executeMethods, check m_linkObject's doc
|
|
|
|
above.'''
|
2014-06-24 10:07:59 -05:00
|
|
|
zobj = obj.o
|
2014-04-03 10:32:57 -05:00
|
|
|
# Security check
|
2014-07-10 02:46:39 -05:00
|
|
|
if not noSecurity:
|
|
|
|
zobj.mayEdit(self.writePermission, raiseError=True)
|
2014-09-29 03:06:40 -05:00
|
|
|
if executeMethods:
|
|
|
|
self.mayUnlinkElement(obj, value, raiseError=True)
|
2013-07-08 16:39:16 -05:00
|
|
|
# p_value can be a list of objects
|
|
|
|
if type(value) in sutils.sequenceTypes:
|
2014-09-29 03:06:40 -05:00
|
|
|
for v in value:
|
|
|
|
self.unlinkObject(obj, v, back, noSecurity, executeMethods)
|
2013-07-08 16:39:16 -05:00
|
|
|
return
|
2014-06-24 10:07:59 -05:00
|
|
|
refs = getattr(zobj.aq_base, self.name, None)
|
2013-07-08 16:39:16 -05:00
|
|
|
if not refs: return
|
|
|
|
# Unlink p_value
|
2014-06-24 10:07:59 -05:00
|
|
|
uid = value.o.id
|
2013-07-08 16:39:16 -05:00
|
|
|
if uid in refs:
|
|
|
|
refs.remove(uid)
|
2014-06-25 08:42:34 -05:00
|
|
|
# Execute self.afterUnlink if present
|
2014-09-29 03:06:40 -05:00
|
|
|
if executeMethods and self.afterUnlink: self.afterUnlink(obj, value)
|
2013-07-08 16:39:16 -05:00
|
|
|
# Update the back reference
|
2014-09-29 03:06:40 -05:00
|
|
|
if not back:
|
|
|
|
self.back.unlinkObject(value,obj,True,noSecurity,executeMethods)
|
2013-07-08 16:39:16 -05:00
|
|
|
|
|
|
|
def store(self, obj, value):
|
|
|
|
'''Stores on p_obj, the p_value, which can be:
|
|
|
|
* None;
|
|
|
|
* an object UID (=string);
|
|
|
|
* a list of object UIDs (=list of strings). Generally, UIDs or lists
|
|
|
|
of UIDs come from Ref fields with link:True edited through the web;
|
|
|
|
* a Zope object;
|
|
|
|
* a Appy object;
|
|
|
|
* a list of Appy or Zope objects.'''
|
2014-03-04 08:03:37 -06:00
|
|
|
if not self.persist: return
|
2014-06-24 10:07:59 -05:00
|
|
|
# Standardize p_value into a list of Appy objects
|
2013-07-08 16:39:16 -05:00
|
|
|
objects = value
|
|
|
|
if not objects: objects = []
|
|
|
|
if type(objects) not in sutils.sequenceTypes: objects = [objects]
|
|
|
|
tool = obj.getTool()
|
|
|
|
for i in range(len(objects)):
|
|
|
|
if isinstance(objects[i], basestring):
|
2014-06-24 10:07:59 -05:00
|
|
|
# We have an UID here
|
|
|
|
objects[i] = tool.getObject(objects[i], appy=True)
|
2013-07-08 16:39:16 -05:00
|
|
|
else:
|
2014-06-24 10:07:59 -05:00
|
|
|
# Be sure to have an Appy object
|
|
|
|
objects[i] = objects[i].appy()
|
|
|
|
uids = [o.o.id for o in objects]
|
|
|
|
appyObj = obj.appy()
|
2013-07-08 16:39:16 -05:00
|
|
|
# Unlink objects that are not referred anymore
|
|
|
|
refs = getattr(obj.aq_base, self.name, None)
|
|
|
|
if refs:
|
|
|
|
i = len(refs)-1
|
|
|
|
while i >= 0:
|
|
|
|
if refs[i] not in uids:
|
|
|
|
# Object having this UID must unlink p_obj
|
2014-06-24 10:07:59 -05:00
|
|
|
tied = tool.getObject(refs[i], appy=True)
|
|
|
|
self.back.unlinkObject(tied, appyObj)
|
2013-07-08 16:39:16 -05:00
|
|
|
i -= 1
|
|
|
|
# Link new objects
|
2014-06-24 10:07:59 -05:00
|
|
|
if objects: self.linkObject(appyObj, objects)
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-07-28 05:29:16 -05:00
|
|
|
def mayAdd(self, obj, mode='create', checkMayEdit=True):
|
|
|
|
'''May the user create (if p_mode == "create") or link
|
|
|
|
(if mode == "link") (a) new referred object(s) from p_obj via this
|
|
|
|
Ref? If p_checkMayEdit is False, it means that the condition of being
|
2014-05-03 15:45:51 -05:00
|
|
|
allowed to edit this Ref field has already been checked somewhere
|
|
|
|
else (it is always required, we just want to avoid checking it
|
|
|
|
twice).'''
|
2013-07-08 16:39:16 -05:00
|
|
|
# We can't (yet) do that on back references.
|
2013-08-21 15:25:27 -05:00
|
|
|
if self.isBack: return gutils.No('is_back')
|
2014-07-28 05:29:16 -05:00
|
|
|
# Check if this Ref is addable/linkable.
|
|
|
|
if mode == 'create':
|
|
|
|
add = self.getAttribute(obj, 'add')
|
|
|
|
if not add: return gutils.No('no_add')
|
|
|
|
elif mode == 'link':
|
|
|
|
if (self.link != 'popup') or not self.isMultiValued(): return
|
2013-07-08 16:39:16 -05:00
|
|
|
# Have we reached the maximum number of referred elements?
|
|
|
|
if self.multiplicity[1] != None:
|
|
|
|
refCount = len(getattr(obj, self.name, ()))
|
2013-08-21 06:54:56 -05:00
|
|
|
if refCount >= self.multiplicity[1]: return gutils.No('max_reached')
|
2013-07-08 16:39:16 -05:00
|
|
|
# May the user edit this Ref field?
|
2014-05-03 15:45:51 -05:00
|
|
|
if checkMayEdit:
|
|
|
|
if not obj.mayEdit(self.writePermission):
|
|
|
|
return gutils.No('no_write_perm')
|
2013-08-23 11:57:27 -05:00
|
|
|
# May the user create instances of the referred class?
|
2014-07-28 05:29:16 -05:00
|
|
|
if mode == 'create':
|
|
|
|
if not obj.getTool().userMayCreate(self.klass):
|
|
|
|
return gutils.No('no_create_perm')
|
2013-07-08 16:39:16 -05:00
|
|
|
return True
|
|
|
|
|
|
|
|
def checkAdd(self, obj):
|
|
|
|
'''Compute m_mayAdd above, and raise an Unauthorized exception if
|
|
|
|
m_mayAdd returns False.'''
|
|
|
|
may = self.mayAdd(obj)
|
|
|
|
if not may:
|
2014-05-03 15:45:51 -05:00
|
|
|
obj.raiseUnauthorized("User can't write Ref field '%s' (%s)." % \
|
|
|
|
(self.name, may.msg))
|
2013-07-08 16:39:16 -05:00
|
|
|
|
2014-06-15 17:58:45 -05:00
|
|
|
def getOnAdd(self, q, formName, addConfirmMsg, target, navBaseCall,
|
|
|
|
startNumber):
|
|
|
|
'''Computes the JS code to execute when button "add" is clicked.'''
|
|
|
|
if self.noForm:
|
|
|
|
# Ajax-refresh the Ref with a special param to link a newly created
|
|
|
|
# object.
|
|
|
|
res = navBaseCall.replace('**v**',
|
|
|
|
"%d,'doCreateWithoutForm'" % startNumber)
|
|
|
|
if self.addConfirm:
|
|
|
|
res = "askConfirm('script', %s, %s)" % \
|
|
|
|
(q(res, False), q(addConfirmMsg))
|
|
|
|
else:
|
|
|
|
# In the basic case, no JS code is executed: target.openPopup is
|
|
|
|
# empty and the button-related form is submitted in the main page.
|
|
|
|
res = target.openPopup
|
|
|
|
if self.addConfirm and not target.openPopup:
|
|
|
|
res = "askConfirm('form','%s',%s)" % (formName,q(addConfirmMsg))
|
|
|
|
elif self.addConfirm and target.openPopup:
|
|
|
|
res = "askConfirm('form+script',%s,%s)" % \
|
|
|
|
(q(formName + '+' + target.openPopup, False), \
|
|
|
|
q(addConfirmMsg))
|
|
|
|
return res
|
|
|
|
|
2014-07-10 02:46:39 -05:00
|
|
|
def mayUnlinkElement(self, obj, tied, raiseError=False):
|
|
|
|
'''May we unlink from this Ref field this specific p_tied object?'''
|
|
|
|
if not self.unlinkElement: return True
|
|
|
|
res = self.unlinkElement(obj, tied)
|
|
|
|
if res: return True
|
|
|
|
else:
|
|
|
|
if not raiseError: return
|
|
|
|
# Raise an exception.
|
|
|
|
obj.o.raiseUnauthorized('field.unlinkElement prevents you to ' \
|
|
|
|
'unlink this object.')
|
|
|
|
|
2014-04-03 10:32:57 -05:00
|
|
|
def getCbJsInit(self, obj):
|
|
|
|
'''When checkboxes are enabled, this method defines a JS associative
|
|
|
|
array (named "_appy_objs_cbs") that will store checkboxes' statuses.
|
|
|
|
This array is needed because all linked objects are not visible at
|
|
|
|
the same time (pagination).
|
|
|
|
|
|
|
|
Moreover, if self.link is "list", an additional array (named
|
|
|
|
"_appy_poss_cbs") is defined for possible values.
|
|
|
|
|
2014-04-05 02:49:54 -05:00
|
|
|
Semantics of this (those) array(s) can be as follows: if a key is
|
|
|
|
present in it for a given linked object, it means that the
|
|
|
|
checkbox is unchecked. In this case, all linked objects are selected
|
|
|
|
by default. But the semantics can be inverted: presence of a key may
|
|
|
|
mean that the checkbox is checked. The current array semantics is
|
|
|
|
stored in a variable named "_appy_objs_sem" (or "_appy_poss_sem")
|
|
|
|
and may hold "unchecked" (initial semantics) or "checked" (inverted
|
|
|
|
semantics). Inverting semantics allows to keep the array small even
|
2014-04-03 10:32:57 -05:00
|
|
|
when checking/unchecking all checkboxes.
|
2014-07-19 06:42:39 -05:00
|
|
|
|
2014-04-03 10:32:57 -05:00
|
|
|
The mentioned JS arrays and variables are stored as attributes of the
|
|
|
|
DOM node representing this field.'''
|
2014-04-05 02:49:54 -05:00
|
|
|
# The initial semantics depends on the checkboxes default value.
|
|
|
|
default = self.getAttribute(obj, 'checkboxesDefault') and \
|
|
|
|
'unchecked' or 'checked'
|
|
|
|
code = "\nnode['_appy_%%s_cbs']={};\nnode['_appy_%%s_sem']='%s';" % \
|
|
|
|
default
|
2014-04-03 10:32:57 -05:00
|
|
|
poss = (self.link == 'list') and (code % ('poss', 'poss')) or ''
|
|
|
|
return "var node=document.getElementById('%s_%s');%s%s" % \
|
|
|
|
(obj.id, self.name, code % ('objs', 'objs'), poss)
|
|
|
|
|
2013-08-21 05:35:30 -05:00
|
|
|
def doChangeOrder(self, obj):
|
2014-04-15 06:01:39 -05:00
|
|
|
'''Moves a referred object up/down/top/bottom.'''
|
2013-08-21 05:35:30 -05:00
|
|
|
rq = obj.REQUEST
|
2014-04-15 06:01:39 -05:00
|
|
|
# How to move the item?
|
|
|
|
move = rq['move']
|
|
|
|
# Get the UID of the tied object to move
|
2013-08-21 05:35:30 -05:00
|
|
|
uid = rq['refObjectUid']
|
|
|
|
uids = getattr(obj.aq_base, self.name)
|
|
|
|
oldIndex = uids.index(uid)
|
2014-04-15 06:01:39 -05:00
|
|
|
if move == 'up':
|
|
|
|
newIndex = oldIndex - 1
|
|
|
|
elif move == 'down':
|
|
|
|
newIndex = oldIndex + 1
|
|
|
|
elif move == 'top':
|
|
|
|
newIndex = 0
|
|
|
|
elif move == 'bottom':
|
2014-04-15 12:52:18 -05:00
|
|
|
newIndex = len(uids) - 1
|
|
|
|
elif move.startswith('index'):
|
|
|
|
# New index starts at 1 (oldIndex starts at 0).
|
|
|
|
try:
|
|
|
|
newIndex = int(move.split('_')[1]) - 1
|
|
|
|
except ValueError:
|
|
|
|
newIndex = -1
|
|
|
|
# If newIndex is negative, it means that the move can't occur.
|
|
|
|
if newIndex > -1:
|
|
|
|
uids.remove(uid)
|
|
|
|
uids.insert(newIndex, uid)
|
2013-08-21 05:35:30 -05:00
|
|
|
|
2014-05-19 05:12:33 -05:00
|
|
|
def doCreateWithoutForm(self, obj):
|
|
|
|
'''This method is called when a user wants to create a object from a
|
|
|
|
reference field, automatically (without displaying a form).'''
|
|
|
|
obj.appy().create(self.name)
|
|
|
|
|
2013-07-10 02:56:35 -05:00
|
|
|
xhtmlToText = re.compile('<.*?>', re.S)
|
2014-03-05 06:25:36 -06:00
|
|
|
def getReferenceLabel(self, refObject, unlimited=False):
|
2013-07-10 02:56:35 -05:00
|
|
|
'''p_self must have link=True. I need to display, on an edit view, the
|
|
|
|
p_refObject in the listbox that will allow the user to choose which
|
|
|
|
object(s) to link through the Ref. The information to display may
|
|
|
|
only be the object title or more if self.shownInfo is used.'''
|
|
|
|
res = ''
|
|
|
|
for fieldName in self.shownInfo:
|
|
|
|
refType = refObject.o.getAppyType(fieldName)
|
|
|
|
value = getattr(refObject, fieldName)
|
2014-09-06 08:20:59 -05:00
|
|
|
value = refType.getShownValue(refObject.o, value)
|
2013-07-10 02:56:35 -05:00
|
|
|
if refType.type == 'String':
|
|
|
|
if refType.format == 2:
|
|
|
|
value = self.xhtmlToText.sub(' ', value)
|
2013-08-21 05:35:30 -05:00
|
|
|
elif type(value) in sutils.sequenceTypes:
|
2013-07-10 02:56:35 -05:00
|
|
|
value = ', '.join(value)
|
2014-03-06 16:08:14 -06:00
|
|
|
prefix = res and ' | ' or ''
|
2013-07-10 02:56:35 -05:00
|
|
|
res += prefix + value
|
2014-03-05 06:25:36 -06:00
|
|
|
if unlimited: return res
|
2013-07-10 02:56:35 -05:00
|
|
|
maxWidth = self.width or 30
|
|
|
|
if len(res) > maxWidth:
|
2014-03-06 16:08:14 -06:00
|
|
|
res = refObject.tool.o.truncateValue(res, maxWidth)
|
2013-07-10 02:56:35 -05:00
|
|
|
return res
|
|
|
|
|
2014-05-02 07:34:10 -05:00
|
|
|
def getIndexOf(self, obj, tiedUid, raiseError=True):
|
2014-04-15 06:01:39 -05:00
|
|
|
'''Gets the position of tied object identified by p_tiedUid within this
|
|
|
|
field on p_obj.'''
|
2013-07-10 02:56:35 -05:00
|
|
|
uids = getattr(obj.aq_base, self.name, None)
|
2014-05-02 07:34:10 -05:00
|
|
|
if not uids:
|
|
|
|
if raiseError: raise IndexError()
|
|
|
|
else: return
|
|
|
|
if tiedUid in uids:
|
|
|
|
return uids.index(tiedUid)
|
|
|
|
else:
|
|
|
|
if raiseError: raise IndexError()
|
|
|
|
else: return
|
2013-07-10 02:56:35 -05:00
|
|
|
|
2013-08-21 05:35:30 -05:00
|
|
|
def sort(self, obj):
|
|
|
|
'''Called when the user wants to sort the content of this field.'''
|
|
|
|
rq = obj.REQUEST
|
|
|
|
sortKey = rq.get('sortKey')
|
|
|
|
reverse = rq.get('reverse') == 'True'
|
|
|
|
obj.appy().sort(self.name, sortKey=sortKey, reverse=reverse)
|
|
|
|
|
2014-04-30 14:08:42 -05:00
|
|
|
def getRenderMode(self, layoutType):
|
|
|
|
'''Gets the render mode, determined by self.render and some
|
|
|
|
exceptions.'''
|
|
|
|
if (layoutType == 'view') and (self.render == 'menus'): return 'list'
|
|
|
|
return self.render
|
|
|
|
|
2014-07-23 15:29:47 -05:00
|
|
|
def getPopupObjects(self, obj, rq, requestValue):
|
|
|
|
'''Gets the list of objects that were selected in the popup (for Ref
|
|
|
|
fields with link="popup").'''
|
|
|
|
if requestValue:
|
|
|
|
# We are validating the form. Return the request value instead of
|
|
|
|
# the popup value.
|
|
|
|
tool = obj.tool
|
|
|
|
if isinstance(requestValue, basestring):
|
|
|
|
return [tool.getObject(requestValue)]
|
|
|
|
else:
|
|
|
|
return [tool.getObject(rv) for rv in requestValue]
|
|
|
|
res = []
|
|
|
|
# No object can be selected if the popup has not been opened yet.
|
|
|
|
if 'semantics' not in rq:
|
|
|
|
# In this case, display already linked objects if any.
|
2014-07-25 08:07:31 -05:00
|
|
|
if not obj.isEmpty(self.name): return self.getValue(obj.o)
|
2014-07-23 15:29:47 -05:00
|
|
|
return res
|
|
|
|
uids = rq['selected'].split(',')
|
|
|
|
tool = obj.tool
|
|
|
|
if rq['semantics'] == 'checked':
|
|
|
|
# Simply get the selected objects from their uid.
|
|
|
|
return [tool.getObject(uid) for uid in uids]
|
|
|
|
else:
|
|
|
|
# Replay the search in self.select to get the list of uids that were
|
|
|
|
# shown in the popup.
|
|
|
|
className = tool.o.getPortalType(self.klass)
|
|
|
|
brains = obj.o.executeQuery(className, search=self.select,
|
|
|
|
maxResults='NO_LIMIT', brainsOnly=True,
|
|
|
|
sortBy=rq.get('sortKey'), sortOrder=rq.get('sortOrder'),
|
|
|
|
filterKey=rq.get('filterKey'),filterValue=rq.get('filterValue'))
|
|
|
|
queryUids = [os.path.basename(b.getPath()) for b in brains]
|
|
|
|
for uid in queryUids:
|
|
|
|
if uid not in uids:
|
|
|
|
res.append(tool.getObject(uid))
|
|
|
|
return res
|
|
|
|
|
2014-07-28 05:29:16 -05:00
|
|
|
def onSelectFromPopup(self, obj):
|
|
|
|
'''This method is called on Ref fields with link="popup", when a user
|
|
|
|
has selected objects from the popup, to be added to existing tied
|
|
|
|
objects, from the view widget.'''
|
|
|
|
obj = obj.appy()
|
|
|
|
for tied in self.getPopupObjects(obj, obj.request, None):
|
|
|
|
self.linkObject(obj, tied, noSecurity=False)
|
|
|
|
|
2014-04-03 10:32:57 -05:00
|
|
|
def onUiRequest(self, obj, rq):
|
|
|
|
'''This method is called when an action tied to this Ref field is
|
|
|
|
triggered from the user interface (link, unlink, link_many,
|
2014-04-04 09:49:22 -05:00
|
|
|
unlink_many, delete_many).'''
|
2014-04-03 10:32:57 -05:00
|
|
|
action = rq['linkAction']
|
|
|
|
tool = obj.getTool()
|
2014-04-05 03:54:40 -05:00
|
|
|
msg = None
|
2014-06-25 08:42:34 -05:00
|
|
|
appyObj = obj.appy()
|
2014-04-04 09:49:22 -05:00
|
|
|
if not action.endswith('_many'):
|
|
|
|
# "link" or "unlink"
|
2014-06-25 08:42:34 -05:00
|
|
|
tied = tool.getObject(rq['targetUid'], appy=True)
|
|
|
|
exec 'self.%sObject(appyObj, tied, noSecurity=False)' % action
|
2014-04-04 09:49:22 -05:00
|
|
|
else:
|
2014-04-05 02:49:54 -05:00
|
|
|
# "link_many", "unlink_many", "delete_many". As a preamble, perform
|
|
|
|
# a security check once, instead of doing it on every object-level
|
|
|
|
# operation.
|
2014-05-03 15:45:51 -05:00
|
|
|
obj.mayEdit(self.writePermission, raiseError=True)
|
2014-04-05 02:49:54 -05:00
|
|
|
# Get the (un-)checked objects from the request.
|
2014-07-23 15:29:47 -05:00
|
|
|
uids = rq['targetUid'].split(',')
|
2014-04-03 10:32:57 -05:00
|
|
|
unchecked = rq['semantics'] == 'unchecked'
|
2014-04-04 09:49:22 -05:00
|
|
|
if action == 'link_many':
|
|
|
|
# Get possible values (objects)
|
|
|
|
values = self.getPossibleValues(obj, removeLinked=True)
|
|
|
|
isObj = True
|
|
|
|
else:
|
|
|
|
# Get current values (uids)
|
2014-04-05 02:49:54 -05:00
|
|
|
values = getattr(obj.aq_base, self.name, ())
|
2014-04-04 09:49:22 -05:00
|
|
|
isObj = False
|
2014-04-05 02:49:54 -05:00
|
|
|
# Collect the objects onto which the action must be performed.
|
|
|
|
targets = []
|
2014-04-04 09:49:22 -05:00
|
|
|
for value in values:
|
|
|
|
uid = not isObj and value or value.uid
|
2014-04-03 10:32:57 -05:00
|
|
|
if unchecked:
|
2014-04-04 09:49:22 -05:00
|
|
|
# Keep only objects not among uids.
|
|
|
|
if uid in uids: continue
|
|
|
|
else:
|
|
|
|
# Keep only objects being in uids.
|
|
|
|
if uid not in uids: continue
|
2014-04-05 02:49:54 -05:00
|
|
|
# Collect this object
|
2014-06-25 08:42:34 -05:00
|
|
|
target = not isObj and tool.getObject(value, appy=True) or \
|
|
|
|
value
|
2014-04-05 02:49:54 -05:00
|
|
|
targets.append(target)
|
2014-04-05 03:54:40 -05:00
|
|
|
if not targets:
|
|
|
|
msg = obj.translate('action_null')
|
|
|
|
else:
|
|
|
|
# Perform the action on every target. Count the number of failed
|
|
|
|
# operations.
|
|
|
|
failed = 0
|
2014-07-10 02:46:39 -05:00
|
|
|
singleAction = action.split('_')[0]
|
|
|
|
mustDelete = singleAction == 'delete'
|
2014-04-05 03:54:40 -05:00
|
|
|
for target in targets:
|
|
|
|
if mustDelete:
|
|
|
|
# Delete
|
2014-06-25 08:42:34 -05:00
|
|
|
if target.o.mayDelete(): target.o.delete()
|
2014-04-05 03:54:40 -05:00
|
|
|
else: failed += 1
|
|
|
|
else:
|
2014-07-10 02:46:39 -05:00
|
|
|
# Link or unlink. For unlinking, we need to perform an
|
|
|
|
# additional check.
|
|
|
|
if (singleAction == 'unlink') and \
|
|
|
|
not self.mayUnlinkElement(appyObj, target):
|
|
|
|
failed += 1
|
|
|
|
else:
|
|
|
|
exec 'self.%sObject(appyObj, target)' % singleAction
|
2014-04-05 03:54:40 -05:00
|
|
|
if failed:
|
|
|
|
msg = obj.translate('action_partial', mapping={'nb':failed})
|
2014-04-03 10:32:57 -05:00
|
|
|
urlBack = obj.getUrl(rq['HTTP_REFERER'])
|
2014-04-05 03:54:40 -05:00
|
|
|
if not msg: msg = obj.translate('action_done')
|
2014-06-25 08:42:34 -05:00
|
|
|
appyObj.say(msg)
|
2014-04-03 10:32:57 -05:00
|
|
|
tool.goto(urlBack)
|
|
|
|
|
2014-10-21 02:25:37 -05:00
|
|
|
def getNavInfo(self, obj, nb, total, inPickList=False):
|
|
|
|
'''Gets the navigation info allowing to navigate from tied object number
|
|
|
|
p_nb to its siblings.'''
|
|
|
|
if self.isBack or inPickList: return ''
|
|
|
|
# If p_nb is None, we want to produce a generic nav info into which we
|
|
|
|
# will insert a specific number afterwards.
|
|
|
|
if nb == None: return 'ref.%s.%s.%%d.%d' % (obj.id, self.name, total)
|
|
|
|
return 'ref.%s.%s.%d.%d' % (obj.id, self.name, nb, total)
|
|
|
|
|
|
|
|
def onGotoTied(self, obj):
|
|
|
|
'''Called when the user wants to go to a tied object whose number is in
|
|
|
|
the request.'''
|
|
|
|
number = int(obj.REQUEST['number']) - 1
|
|
|
|
uids = getattr(obj.aq_base, self.name)
|
|
|
|
tiedUid = uids[number]
|
|
|
|
tied = obj.getTool().getObject(tiedUid)
|
|
|
|
tiedUrl = tied.getUrl(nav=self.getNavInfo(obj, number+1, len(uids)))
|
|
|
|
return obj.goto(tiedUrl)
|
|
|
|
|
2013-07-08 16:39:16 -05:00
|
|
|
def autoref(klass, field):
|
|
|
|
'''klass.field is a Ref to p_klass. This kind of auto-reference can't be
|
|
|
|
declared in the "normal" way, like this:
|
|
|
|
|
|
|
|
class A:
|
|
|
|
attr1 = Ref(A)
|
|
|
|
|
|
|
|
because at the time Python encounters the static declaration
|
|
|
|
"attr1 = Ref(A)", class A is not completely defined yet.
|
|
|
|
|
|
|
|
This method allows to overcome this problem. You can write such
|
|
|
|
auto-reference like this:
|
|
|
|
|
|
|
|
class A:
|
|
|
|
attr1 = Ref(None)
|
|
|
|
autoref(A, A.attr1)
|
2014-04-22 12:37:36 -05:00
|
|
|
|
|
|
|
This function can also be used to avoid circular imports between 2
|
|
|
|
classes from 2 different packages. Imagine class P1 in package p1 has a
|
|
|
|
Ref to class P2 in package p2; and class P2 has another Ref to p1.P1
|
|
|
|
(which is not the back Ref of the previous one: it is another,
|
|
|
|
independent Ref).
|
|
|
|
|
|
|
|
In p1, you have
|
|
|
|
|
|
|
|
from p2 import P2
|
|
|
|
class P1:
|
|
|
|
ref1 = Ref(P2)
|
|
|
|
|
|
|
|
Then, if you write the following in p2, python will complain because of a
|
|
|
|
circular import:
|
|
|
|
|
|
|
|
from p1 import P1
|
|
|
|
class P2:
|
|
|
|
ref2 = Ref(P1)
|
|
|
|
|
|
|
|
The solution is to write this. In p1:
|
|
|
|
|
|
|
|
from p2 import P2
|
|
|
|
class P1:
|
|
|
|
ref1 = Ref(P2)
|
|
|
|
autoref(P1, P2.ref2)
|
|
|
|
|
|
|
|
And, in p2:
|
|
|
|
class P2:
|
|
|
|
ref2 = Ref(None)
|
2013-07-08 16:39:16 -05:00
|
|
|
'''
|
|
|
|
field.klass = klass
|
|
|
|
setattr(klass, field.back.attribute, field.back)
|
|
|
|
# ------------------------------------------------------------------------------
|