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
|
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
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
# Classes used by edit/view templates for accessing information ----------------
|
|
|
|
class Descr:
|
|
|
|
'''Abstract class for description classes.'''
|
|
|
|
def get(self): return self.__dict__
|
|
|
|
|
|
|
|
class GroupDescr(Descr):
|
2012-11-14 04:36:48 -06:00
|
|
|
def __init__(self, group, page, metaType, forSearch=False):
|
|
|
|
'''Creates the data structure manipulated in ZPTs for p_group, the
|
2010-08-05 11:23:17 -05:00
|
|
|
Group instance used in the field definition.'''
|
|
|
|
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 = [[]]
|
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):
|
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
|
|
|
|
|
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.'''
|
|
|
|
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 = ''
|
|
|
|
|
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)
|
2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|