2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
2011-12-15 15:56:53 -06:00
|
|
|
import re, os, os.path
|
2012-09-26 16:13:02 -05:00
|
|
|
from appy.shared.utils import normalizeText
|
2013-06-27 04:57:39 -05:00
|
|
|
from appy.px import Px
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2011-11-25 11:01:20 -06:00
|
|
|
# Function for creating a Zope object ------------------------------------------
|
2012-06-02 07:36:49 -05:00
|
|
|
def createObject(folder, id, className, appName, wf=True, noSecurity=False):
|
2011-11-25 11:01:20 -06:00
|
|
|
'''Creates, in p_folder, object with some p_id. Object will be an instance
|
|
|
|
of p_className from application p_appName. In a very special case (the
|
|
|
|
creation of the config object), computing workflow-related info is not
|
|
|
|
possible at this time. This is why this function can be called with
|
|
|
|
p_wf=False.'''
|
|
|
|
exec 'from Products.%s.%s import %s as ZopeClass' % (appName, className,
|
|
|
|
className)
|
2012-06-02 07:36:49 -05:00
|
|
|
if not noSecurity:
|
|
|
|
# Check that the user can create objects of className
|
|
|
|
if folder.meta_type.endswith('Folder'): # Folder or temp folder.
|
|
|
|
tool = folder.config
|
|
|
|
else:
|
|
|
|
tool = folder.getTool()
|
|
|
|
user = tool.getUser()
|
|
|
|
userRoles = user.getRoles()
|
|
|
|
allowedRoles=ZopeClass.wrapperClass.getCreators(tool.getProductConfig())
|
|
|
|
allowed = False
|
|
|
|
for role in userRoles:
|
|
|
|
if role in allowedRoles:
|
|
|
|
allowed = True
|
|
|
|
break
|
|
|
|
if not allowed:
|
|
|
|
from AccessControl import Unauthorized
|
|
|
|
raise Unauthorized("User can't create instances of %s" % \
|
|
|
|
ZopeClass.__name__)
|
2011-11-25 11:01:20 -06:00
|
|
|
obj = ZopeClass(id)
|
|
|
|
folder._objects = folder._objects + \
|
|
|
|
({'id':id, 'meta_type':className},)
|
|
|
|
folder._setOb(id, obj)
|
|
|
|
obj = folder._getOb(id) # Important. Else, obj is not really in the folder.
|
|
|
|
obj.portal_type = className
|
|
|
|
obj.id = id
|
|
|
|
obj._at_uid = id
|
2012-03-26 12:09:45 -05:00
|
|
|
user = obj.getUser()
|
|
|
|
if not user.getId():
|
|
|
|
if user.name == 'System Processes':
|
|
|
|
userId = 'admin' # This is what happens when Zope is starting.
|
|
|
|
else:
|
|
|
|
userId = None # Anonymous.
|
|
|
|
else:
|
|
|
|
userId = user.getId()
|
2012-02-16 11:13:51 -06:00
|
|
|
obj.creator = userId or 'Anonymous User'
|
2011-11-25 11:01:20 -06:00
|
|
|
from DateTime import DateTime
|
|
|
|
obj.created = DateTime()
|
2012-11-05 03:21:27 -06:00
|
|
|
obj.modified = obj.created
|
2012-02-16 11:13:51 -06:00
|
|
|
obj.__ac_local_roles__ = { userId: ['Owner'] } # userId can be None (anon).
|
2011-11-25 11:01:20 -06:00
|
|
|
if wf: obj.notifyWorkflowCreated()
|
|
|
|
return obj
|
|
|
|
|
2013-06-27 04:57:39 -05:00
|
|
|
# Classes used by edit/view PXs for accessing information ----------------------
|
2009-06-29 07:06:01 -05:00
|
|
|
class Descr:
|
|
|
|
'''Abstract class for description classes.'''
|
|
|
|
def get(self): return self.__dict__
|
|
|
|
|
|
|
|
class GroupDescr(Descr):
|
2013-06-27 04:57:39 -05:00
|
|
|
'''Intermediary, on-the-fly-generated data structure that groups all fields
|
|
|
|
sharing the same appy.gen.Group instance, that some logged user can
|
|
|
|
see.'''
|
|
|
|
# PX that renders a group of fields
|
2013-07-15 04:23:29 -05:00
|
|
|
pxView = Px('''<p>pxGroupedFields</p>''')
|
2013-06-27 04:57:39 -05:00
|
|
|
|
2013-07-15 04:23:29 -05:00
|
|
|
# PX that renders a group of searches
|
|
|
|
pxViewSearches = Px('''
|
|
|
|
<x var="expanded=req.get(field.labelId, 'collapsed') == 'expanded'">
|
2013-06-27 04:57:39 -05:00
|
|
|
<!-- Group name, prefixed by the expand/collapse icon -->
|
|
|
|
<div class="portletGroup">
|
2013-07-15 06:39:05 -05:00
|
|
|
<img class="clickable" style="margin-right: 3px" align=":dleft"
|
2013-07-15 04:23:29 -05:00
|
|
|
id=":'%s_img' % field.labelId"
|
2013-07-20 12:56:17 -05:00
|
|
|
src=":expanded and url('collapse.gif') or url('expand.gif')"
|
2013-07-15 04:23:29 -05:00
|
|
|
onclick=":'toggleCookie(%s)' % q(field.labelId)"/>
|
|
|
|
<x if="not field.translated">:_(field.labelId)</x>
|
|
|
|
<x if="field.translated">:field.translated</x>
|
2013-06-27 04:57:39 -05:00
|
|
|
</div>
|
|
|
|
<!-- Group content -->
|
|
|
|
<div var="display=expanded and 'display:block' or 'display:none'"
|
2013-07-15 04:23:29 -05:00
|
|
|
id=":field.labelId" style=":'padding-left: 10px; %s' % display">
|
|
|
|
<x for="searches in field.widgets">
|
|
|
|
<x for="elem in searches">
|
2013-06-27 04:57:39 -05:00
|
|
|
<!-- An inner group within this group -->
|
2013-07-15 04:23:29 -05:00
|
|
|
<x if="elem['type'] == 'group'"
|
|
|
|
var2="field=elem">:field.pxViewSearches</x>
|
2013-06-27 04:57:39 -05:00
|
|
|
<!-- A search -->
|
2013-07-15 04:23:29 -05:00
|
|
|
<x if="elem['type'] != 'group'" var2="search=elem">:search.pxView</x>
|
2013-06-27 04:57:39 -05:00
|
|
|
</x>
|
|
|
|
</x>
|
|
|
|
</div>
|
2013-07-15 04:23:29 -05:00
|
|
|
</x>''')
|
2013-06-27 04:57:39 -05:00
|
|
|
|
2012-11-14 04:36:48 -06:00
|
|
|
def __init__(self, group, page, metaType, forSearch=False):
|
2010-08-05 11:23:17 -05:00
|
|
|
self.type = 'group'
|
|
|
|
# All p_group attributes become self attributes.
|
|
|
|
for name, value in group.__dict__.iteritems():
|
|
|
|
if not name.startswith('_'):
|
|
|
|
setattr(self, name, value)
|
|
|
|
self.columnsWidths = [col.width for col in group.columns]
|
|
|
|
self.columnsAligns = [col.align for col in group.columns]
|
|
|
|
# Names of i18n labels
|
2011-09-10 18:59:22 -05:00
|
|
|
labelName = self.name
|
|
|
|
prefix = metaType
|
|
|
|
if group.label:
|
|
|
|
if isinstance(group.label, basestring): prefix = group.label
|
|
|
|
else: # It is a tuple (metaType, name)
|
|
|
|
if group.label[1]: labelName = group.label[1]
|
|
|
|
if group.label[0]: prefix = group.label[0]
|
2012-11-14 04:36:48 -06:00
|
|
|
if forSearch: gp = 'searchgroup'
|
|
|
|
else: gp = 'group'
|
|
|
|
self.labelId = '%s_%s_%s' % (prefix, gp, labelName)
|
2010-08-05 11:23:17 -05:00
|
|
|
self.descrId = self.labelId + '_descr'
|
|
|
|
self.helpId = self.labelId + '_help'
|
|
|
|
# The name of the page where the group lies
|
2010-10-19 03:47:42 -05:00
|
|
|
self.page = page.name
|
2010-08-05 11:23:17 -05:00
|
|
|
# The widgets belonging to the group that the current user may see.
|
|
|
|
# They will be stored by m_addWidget below as a list of lists because
|
|
|
|
# they will be rendered as a table.
|
|
|
|
self.widgets = [[]]
|
2013-06-27 04:57:39 -05:00
|
|
|
# PX to user for rendering this group.
|
2013-07-15 04:23:29 -05:00
|
|
|
self.px = forSearch and self.pxViewSearches or self.pxView
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2010-08-05 11:23:17 -05:00
|
|
|
@staticmethod
|
|
|
|
def addWidget(groupDict, newWidget):
|
|
|
|
'''Adds p_newWidget into p_groupDict['widgets']. We try first to add
|
|
|
|
p_newWidget into the last widget row. If it is not possible, we
|
|
|
|
create a new row.
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2010-08-05 11:23:17 -05:00
|
|
|
This method is a static method taking p_groupDict as first param
|
|
|
|
instead of being an instance method because at this time the object
|
|
|
|
has already been converted to a dict (for being maniputated within
|
|
|
|
ZPTs).'''
|
|
|
|
# Get the last row
|
|
|
|
widgetRow = groupDict['widgets'][-1]
|
|
|
|
numberOfColumns = len(groupDict['columnsWidths'])
|
|
|
|
# Computes the number of columns already filled by widgetRow
|
|
|
|
rowColumns = 0
|
|
|
|
for widget in widgetRow: rowColumns += widget['colspan']
|
|
|
|
freeColumns = numberOfColumns - rowColumns
|
|
|
|
if freeColumns >= newWidget['colspan']:
|
|
|
|
# We can add the widget in the last row.
|
|
|
|
widgetRow.append(newWidget)
|
2009-06-29 07:06:01 -05:00
|
|
|
else:
|
2010-08-05 11:23:17 -05:00
|
|
|
if freeColumns:
|
|
|
|
# Terminate the current row by appending empty cells
|
|
|
|
for i in range(freeColumns): widgetRow.append('')
|
|
|
|
# Create a new row
|
|
|
|
newRow = [newWidget]
|
|
|
|
groupDict['widgets'].append(newRow)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
|
|
class PhaseDescr(Descr):
|
2013-06-27 04:57:39 -05:00
|
|
|
'''Describes a phase.'''
|
|
|
|
|
|
|
|
pxPhase = Px('''
|
|
|
|
<tr var="singlePage=len(phase['pages']) == 1">
|
|
|
|
<td var="label='%s_phase_%s' % (contextObj.meta_type, phase['name'])">
|
|
|
|
|
|
|
|
<!-- The title of the phase -->
|
|
|
|
<div class="portletGroup"
|
|
|
|
if="not singlePhase and not singlePage">::_(label)</div>
|
|
|
|
|
|
|
|
<!-- The page(s) within the phase -->
|
|
|
|
<x for="aPage in phase['pages']">
|
|
|
|
<!-- First line: page name and icons -->
|
|
|
|
<div if="not (singlePhase and singlePage)"
|
|
|
|
class=":aPage==page and 'portletCurrent portletPage' or \
|
|
|
|
'portletPage'">
|
|
|
|
<a href=":contextObj.getUrl(page=aPage)">::_('%s_page_%s' % \
|
|
|
|
(contextObj.meta_type, aPage))</a>
|
|
|
|
<x var="locked=contextObj.isLocked(user, aPage);
|
|
|
|
editable=mayEdit and phase['pagesInfo'][aPage]['showOnEdit']">
|
|
|
|
<a if="editable and not locked"
|
|
|
|
href="contextObj.getUrl(mode='edit', page=aPage)">
|
2013-07-20 12:56:17 -05:00
|
|
|
<img src=":url('edit')" title=":_('object_edit')"/></a>
|
2013-06-27 04:57:39 -05:00
|
|
|
<a if="editable and locked">
|
|
|
|
<img style="cursor: help"
|
|
|
|
var="lockDate=tool.formatDate(locked[1]);
|
|
|
|
lockMap={'user':ztool.getUserName(locked[0]), \
|
|
|
|
'date':lockDate};
|
|
|
|
lockMsg=_('page_locked', mapping=lockMap)"
|
2013-07-20 12:56:17 -05:00
|
|
|
src=":url('locked')" title=":lockMsg"/></a>
|
2013-06-27 04:57:39 -05:00
|
|
|
<a if="editable and locked and user.has_role('Manager')">
|
2013-07-20 12:56:17 -05:00
|
|
|
<img class="clickable" title=":_('page_unlock')" src=":url('unlock')"
|
2013-07-15 04:23:29 -05:00
|
|
|
onclick=":'onUnlockPage(%s,%s)' % \
|
|
|
|
(q(contextObj.UID()), q(aPage))"/></a>
|
2013-06-27 04:57:39 -05:00
|
|
|
</x>
|
|
|
|
</div>
|
|
|
|
<!-- Next lines: links -->
|
|
|
|
<x var="links=phase['pagesInfo'][aPage].get('links')" if="links">
|
|
|
|
<div for="link in links">
|
|
|
|
<a href=":link['url']">:link['title']</a>
|
|
|
|
</div>
|
|
|
|
</x>
|
|
|
|
</x>
|
|
|
|
</td>
|
2013-07-15 04:23:29 -05:00
|
|
|
</tr>''')
|
2013-06-27 04:57:39 -05:00
|
|
|
|
2012-11-05 03:21:27 -06:00
|
|
|
def __init__(self, name, obj):
|
2009-06-29 07:06:01 -05:00
|
|
|
self.name = name
|
2010-08-05 11:23:17 -05:00
|
|
|
self.obj = obj
|
2010-10-19 03:47:42 -05:00
|
|
|
# The list of names of pages in this phase
|
|
|
|
self.pages = []
|
|
|
|
# The list of hidden pages in this phase
|
|
|
|
self.hiddenPages = []
|
|
|
|
# The dict below stores infor about every page listed in self.pages.
|
|
|
|
self.pagesInfo = {}
|
2009-06-29 07:06:01 -05:00
|
|
|
self.totalNbOfPhases = None
|
2010-08-05 11:23:17 -05:00
|
|
|
# The following attributes allows to browse, from a given page, to the
|
|
|
|
# last page of the previous phase and to the first page of the following
|
|
|
|
# phase if allowed by phase state.
|
|
|
|
self.previousPhase = None
|
|
|
|
self.nextPhase = None
|
2013-06-27 04:57:39 -05:00
|
|
|
self.px = self.pxPhase
|
2010-08-05 11:23:17 -05:00
|
|
|
|
2012-03-27 03:37:41 -05:00
|
|
|
def addPageLinks(self, appyType, obj):
|
|
|
|
'''If p_appyType is a navigable Ref, we must add, within self.pagesInfo,
|
|
|
|
those links.'''
|
|
|
|
if appyType.page.name in self.hiddenPages: return
|
|
|
|
infos = []
|
|
|
|
for obj in appyType.getValue(obj, type="zobjects"):
|
|
|
|
infos.append({'title': obj.title, 'url':obj.absolute_url()})
|
|
|
|
self.pagesInfo[appyType.page.name]['links'] = infos
|
|
|
|
|
2010-10-19 03:47:42 -05:00
|
|
|
def addPage(self, appyType, obj, layoutType):
|
|
|
|
'''Adds page-related information in the phase.'''
|
|
|
|
# If the page is already there, we have nothing more to do.
|
|
|
|
if (appyType.page.name in self.pages) or \
|
|
|
|
(appyType.page.name in self.hiddenPages): return
|
|
|
|
# Add the page only if it must be shown.
|
2010-10-29 07:36:36 -05:00
|
|
|
isShowableOnView = appyType.page.isShowable(obj, 'view')
|
|
|
|
isShowableOnEdit = appyType.page.isShowable(obj, 'edit')
|
|
|
|
if isShowableOnView or isShowableOnEdit:
|
2010-10-19 03:47:42 -05:00
|
|
|
# The page must be added.
|
|
|
|
self.pages.append(appyType.page.name)
|
|
|
|
# Create the dict about page information and add it in self.pageInfo
|
|
|
|
pageInfo = {'page': appyType.page,
|
2010-10-29 07:36:36 -05:00
|
|
|
'showOnView': isShowableOnView,
|
|
|
|
'showOnEdit': isShowableOnEdit}
|
2010-10-19 03:47:42 -05:00
|
|
|
pageInfo.update(appyType.page.getInfo(obj, layoutType))
|
|
|
|
self.pagesInfo[appyType.page.name] = pageInfo
|
|
|
|
else:
|
|
|
|
self.hiddenPages.append(appyType.page.name)
|
2010-08-05 11:23:17 -05:00
|
|
|
|
2012-12-07 04:23:08 -06:00
|
|
|
def computeNextPrevious(self, allPhases):
|
|
|
|
'''This method also fills fields "previousPhase" and "nextPhase"
|
2010-08-05 11:23:17 -05:00
|
|
|
if relevant, based on list of p_allPhases.'''
|
2012-11-05 03:21:27 -06:00
|
|
|
# Identify previous and next phases
|
|
|
|
for phaseInfo in allPhases:
|
|
|
|
if phaseInfo['name'] == self.name:
|
|
|
|
i = allPhases.index(phaseInfo)
|
|
|
|
if i > 0:
|
|
|
|
self.previousPhase = allPhases[i-1]
|
|
|
|
if i < (len(allPhases)-1):
|
|
|
|
self.nextPhase = allPhases[i+1]
|
2009-06-29 07:06:01 -05:00
|
|
|
|
2012-11-14 10:40:52 -06:00
|
|
|
class SearchDescr(Descr):
|
|
|
|
'''Describes a Search.'''
|
2013-06-27 04:57:39 -05:00
|
|
|
# PX for rendering a search.
|
2013-07-15 04:23:29 -05:00
|
|
|
pxView = Px('''
|
2013-06-27 04:57:39 -05:00
|
|
|
<div class="portletSearch">
|
|
|
|
<a href=":'%s?className=%s&search=%s' % \
|
|
|
|
(queryUrl, rootClass, search['name'])"
|
|
|
|
class=":search['name'] == currentSearch and 'portletCurrent' or ''"
|
|
|
|
title=":search['translatedDescr']">:search['translated']</a>
|
2013-07-15 04:23:29 -05:00
|
|
|
</div>''')
|
2013-06-27 04:57:39 -05:00
|
|
|
|
2012-11-14 10:40:52 -06:00
|
|
|
def __init__(self, search, className, tool):
|
|
|
|
self.search = search
|
|
|
|
self.name = search.name
|
|
|
|
self.type = 'search'
|
|
|
|
self.colspan = search.colspan
|
|
|
|
if search.translated:
|
|
|
|
self.translated = search.translated
|
|
|
|
self.translatedDescr = search.translatedDescr
|
|
|
|
else:
|
|
|
|
# The label may be specific in some special cases.
|
|
|
|
labelDescr = ''
|
|
|
|
if search.name == 'allSearch':
|
|
|
|
label = '%s_plural' % className
|
|
|
|
elif search.name == 'customSearch':
|
|
|
|
label = 'search_results'
|
|
|
|
else:
|
|
|
|
label = '%s_search_%s' % (className, search.name)
|
|
|
|
labelDescr = label + '_descr'
|
|
|
|
self.translated = tool.translate(label)
|
|
|
|
if labelDescr:
|
|
|
|
self.translatedDescr = tool.translate(labelDescr)
|
|
|
|
else:
|
|
|
|
self.translatedDescr = ''
|
2013-07-15 09:41:45 -05:00
|
|
|
self.px = self.pxView
|
2012-11-14 10:40:52 -06:00
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
upperLetter = re.compile('[A-Z]')
|
|
|
|
def produceNiceMessage(msg):
|
|
|
|
'''Transforms p_msg into a nice msg.'''
|
|
|
|
res = ''
|
|
|
|
if msg:
|
|
|
|
res = msg[0].upper()
|
|
|
|
for c in msg[1:]:
|
|
|
|
if c == '_':
|
|
|
|
res += ' '
|
|
|
|
elif upperLetter.match(c):
|
|
|
|
res += ' ' + c.lower()
|
|
|
|
else:
|
|
|
|
res += c
|
|
|
|
return res
|
|
|
|
|
2009-10-25 15:42:08 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
2009-11-03 08:02:18 -06:00
|
|
|
class SomeObjects:
|
|
|
|
'''Represents a bunch of objects retrieved from a reference or a query in
|
2011-11-25 11:01:20 -06:00
|
|
|
the catalog.'''
|
2010-01-12 14:15:14 -06:00
|
|
|
def __init__(self, objects=None, batchSize=None, startNumber=0,
|
|
|
|
noSecurity=False):
|
2009-10-25 15:42:08 -05:00
|
|
|
self.objects = objects or [] # The objects
|
|
|
|
self.totalNumber = len(self.objects) # self.objects may only represent a
|
|
|
|
# part of all available objects.
|
2009-11-03 08:02:18 -06:00
|
|
|
self.batchSize = batchSize or self.totalNumber # The max length of
|
|
|
|
# self.objects.
|
|
|
|
self.startNumber = startNumber # The index of first object in
|
|
|
|
# self.objects in the whole list.
|
2010-01-12 14:15:14 -06:00
|
|
|
self.noSecurity = noSecurity
|
2009-11-03 08:02:18 -06:00
|
|
|
def brainsToObjects(self):
|
2011-11-25 11:01:20 -06:00
|
|
|
'''self.objects has been populated from brains from the catalog,
|
2009-11-03 08:02:18 -06:00
|
|
|
not from True objects. This method turns them (or some of them
|
2010-01-12 14:15:14 -06:00
|
|
|
depending on batchSize and startNumber) into real objects.
|
|
|
|
If self.noSecurity is True, it gets the objects even if the logged
|
|
|
|
user does not have the right to get them.'''
|
2009-11-03 08:02:18 -06:00
|
|
|
start = self.startNumber
|
|
|
|
brains = self.objects[start:start + self.batchSize]
|
2010-01-12 14:15:14 -06:00
|
|
|
if self.noSecurity: getMethod = '_unrestrictedGetObject'
|
|
|
|
else: getMethod = 'getObject'
|
|
|
|
self.objects = [getattr(b, getMethod)() for b in brains]
|
2010-03-25 10:34:37 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class Keywords:
|
|
|
|
'''This class allows to handle keywords that a user enters and that will be
|
2012-09-26 16:13:02 -05:00
|
|
|
used as basis for performing requests in a TextIndex/XhtmlIndex.'''
|
2010-03-25 10:34:37 -05:00
|
|
|
|
|
|
|
toRemove = '?-+*()'
|
|
|
|
def __init__(self, keywords, operator='AND'):
|
|
|
|
# Clean the p_keywords that the user has entered.
|
2012-09-26 16:13:02 -05:00
|
|
|
words = normalizeText(keywords)
|
2010-03-25 10:34:37 -05:00
|
|
|
if words == '*': words = ''
|
|
|
|
for c in self.toRemove: words = words.replace(c, ' ')
|
|
|
|
self.keywords = words.split()
|
|
|
|
# Store the operator to apply to the keywords (AND or OR)
|
|
|
|
self.operator = operator
|
|
|
|
|
|
|
|
def merge(self, other, append=False):
|
|
|
|
'''Merges our keywords with those from p_other. If p_append is True,
|
|
|
|
p_other keywords are appended at the end; else, keywords are appended
|
|
|
|
at the begin.'''
|
|
|
|
for word in other.keywords:
|
|
|
|
if word not in self.keywords:
|
|
|
|
if append:
|
|
|
|
self.keywords.append(word)
|
|
|
|
else:
|
|
|
|
self.keywords.insert(0, word)
|
|
|
|
|
|
|
|
def get(self):
|
2012-09-26 16:13:02 -05:00
|
|
|
'''Returns the keywords as needed by the TextIndex.'''
|
2010-03-25 10:34:37 -05:00
|
|
|
if self.keywords:
|
|
|
|
op = ' %s ' % self.operator
|
|
|
|
return op.join(self.keywords)+'*'
|
|
|
|
return ''
|
2010-03-31 08:49:54 -05:00
|
|
|
|
2010-09-02 09:16:08 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
def getClassName(klass, appName=None):
|
|
|
|
'''Generates, from appy-class p_klass, the name of the corresponding
|
2011-12-05 03:52:18 -06:00
|
|
|
Zope class. For some classes, name p_appName is required: it is
|
2010-09-02 09:16:08 -05:00
|
|
|
part of the class name.'''
|
|
|
|
moduleName = klass.__module__
|
2011-12-05 08:11:29 -06:00
|
|
|
if (moduleName == 'appy.gen.model') or moduleName.endswith('.wrappers'):
|
2010-09-02 09:16:08 -05:00
|
|
|
# This is a model (generation time or run time)
|
|
|
|
res = appName + klass.__name__
|
|
|
|
elif klass.__bases__ and (klass.__bases__[-1].__module__ == 'appy.gen'):
|
|
|
|
# This is a customized class (inherits from appy.gen.Tool, User,...)
|
|
|
|
res = appName + klass.__bases__[-1].__name__
|
|
|
|
else: # This is a standard class
|
|
|
|
res = klass.__module__.replace('.', '_') + '_' + klass.__name__
|
|
|
|
return res
|
2011-09-26 14:19:34 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
def updateRolesForPermission(permission, roles, obj):
|
|
|
|
'''Adds roles from list p_roles to the list of roles that are granted
|
|
|
|
p_permission on p_obj.'''
|
|
|
|
from AccessControl.Permission import Permission
|
|
|
|
# Find existing roles that were granted p_permission on p_obj
|
|
|
|
existingRoles = ()
|
|
|
|
for p in obj.ac_inherited_permissions(1):
|
|
|
|
name, value = p[:2]
|
|
|
|
if name == permission:
|
|
|
|
perm = Permission(name, value, obj)
|
|
|
|
existingRoles = perm.getRoles()
|
|
|
|
allRoles = set(existingRoles).union(roles)
|
|
|
|
obj.manage_permission(permission, tuple(allRoles), acquire=0)
|
2013-06-09 17:13:29 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
def callMethod(obj, method, klass=None, cache=True):
|
|
|
|
'''This function is used to call a p_method on some Appy p_obj. m_method
|
|
|
|
can be an instance method on p_obj; it can also be a static method. In
|
|
|
|
this latter case, p_obj is the tool and the static method, defined in
|
|
|
|
p_klass, will be called with the tool as unique arg.
|
|
|
|
|
|
|
|
A method cache is implemented on the request object (available at
|
|
|
|
p_obj.request). So while handling a single request from the ui, every
|
|
|
|
method is called only once. Some method calls must not be cached (ie,
|
|
|
|
values of Computed fields). In this case, p_cache will be False.'''
|
|
|
|
rq = obj.request
|
|
|
|
# Create the method cache if it does not exist on the request
|
|
|
|
if not hasattr(rq, 'methodCache'): rq.methodCache = {}
|
|
|
|
# If m_method is a static method or an instance method, unwrap the true
|
|
|
|
# Python function object behind it.
|
|
|
|
methodType = method.__class__.__name__
|
|
|
|
if methodType == 'staticmethod':
|
|
|
|
method = method.__get__(klass)
|
|
|
|
elif methodType == 'instancemethod':
|
|
|
|
method = method.im_func
|
|
|
|
# Call the method if cache is not needed.
|
|
|
|
if not cache: return method(obj)
|
|
|
|
# If first arg of method is named "tool" instead of the traditional "self",
|
|
|
|
# we cheat and will call the method with the tool as first arg. This will
|
|
|
|
# allow to consider this method as if it was a static method on the tool.
|
|
|
|
# Every method call, even on different instances, will be cached in a unique
|
|
|
|
# key.
|
|
|
|
cheat = False
|
|
|
|
if not klass and (method.func_code.co_varnames[0] == 'tool'):
|
|
|
|
prefix = obj.klass.__name__
|
|
|
|
obj = obj.tool
|
|
|
|
cheat = True
|
|
|
|
# Build the key of this method call in the cache.
|
|
|
|
# First part of the key: the p_obj's uid (if p_method is an instance method)
|
|
|
|
# or p_className (if p_method is a static method).
|
|
|
|
if not cheat:
|
|
|
|
if klass:
|
|
|
|
prefix = klass.__name__
|
|
|
|
else:
|
|
|
|
prefix = obj.uid
|
|
|
|
# Second part of the key: p_method name
|
|
|
|
key = '%s:%s' % (prefix, method.func_name)
|
|
|
|
# Return the cached value if present in the method cache.
|
|
|
|
if key in rq.methodCache:
|
|
|
|
return rq.methodCache[key]
|
|
|
|
# No cached value: call the method, cache the result and return it
|
|
|
|
res = method(obj)
|
|
|
|
rq.methodCache[key] = res
|
|
|
|
return res
|
2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|