2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|
2012-07-18 14:58:11 -05:00
|
|
|
import os.path, time
|
2011-01-18 08:48:55 -06:00
|
|
|
import appy
|
|
|
|
from appy.shared.utils import executeCommand
|
2011-12-05 08:11:29 -06:00
|
|
|
from appy.gen.wrappers import AbstractWrapper
|
2012-07-18 14:58:11 -05:00
|
|
|
from appy.gen.installer import loggedUsers
|
2010-08-05 11:23:17 -05:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
_PY = 'Please specify a file corresponding to a Python interpreter ' \
|
|
|
|
'(ie "/usr/bin/python").'
|
|
|
|
FILE_NOT_FOUND = 'Path "%s" was not found.'
|
|
|
|
VALUE_NOT_FILE = 'Path "%s" is not a file. ' + _PY
|
|
|
|
NO_PYTHON = "Name '%s' does not starts with 'python'. " + _PY
|
|
|
|
NOT_UNO_ENABLED_PYTHON = '"%s" is not a UNO-enabled Python interpreter. ' \
|
|
|
|
'To check if a Python interpreter is UNO-enabled, ' \
|
|
|
|
'launch it and type "import uno". If you have no ' \
|
|
|
|
'ImportError exception it is ok.'
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
class ToolWrapper(AbstractWrapper):
|
|
|
|
def validPythonWithUno(self, value):
|
|
|
|
'''This method represents the validator for field unoEnabledPython.'''
|
|
|
|
if value:
|
|
|
|
if not os.path.exists(value):
|
|
|
|
return FILE_NOT_FOUND % value
|
|
|
|
if not os.path.isfile(value):
|
|
|
|
return VALUE_NOT_FILE % value
|
|
|
|
if not os.path.basename(value).startswith('python'):
|
|
|
|
return NO_PYTHON % value
|
|
|
|
if os.system('%s -c "import uno"' % value):
|
|
|
|
return NOT_UNO_ENABLED_PYTHON % value
|
2011-03-18 10:52:15 -05:00
|
|
|
return True
|
2010-08-05 11:23:17 -05:00
|
|
|
|
2012-05-05 10:04:19 -05:00
|
|
|
def isManager(self):
|
|
|
|
'''Some pages on the tool can only be accessed by God.'''
|
|
|
|
if self.user.has_role('Manager'): return 'view'
|
|
|
|
|
2012-05-08 07:49:45 -05:00
|
|
|
def isManagerEdit(self):
|
|
|
|
'''Some pages on the tool can only be accessed by God, also in edit.'''
|
|
|
|
if self.user.has_role('Manager'): return True
|
|
|
|
|
2012-07-18 14:58:11 -05:00
|
|
|
def computeConnectedUsers(self):
|
|
|
|
'''Computes a table showing users that are currently connected.'''
|
|
|
|
res = '<table cellpadding="0" cellspacing="0" class="list"><tr>' \
|
|
|
|
'<th></th><th>%s</th></tr>' % self.translate('last_user_access')
|
|
|
|
rows = []
|
|
|
|
for userId, lastAccess in loggedUsers.items():
|
|
|
|
user = self.search1('User', noSecurity=True, login=userId)
|
|
|
|
if not user: continue # Could have been deleted in the meanwhile
|
|
|
|
fmt = '%s (%s)' % (self.dateFormat, self.hourFormat)
|
|
|
|
access = time.strftime(fmt, time.localtime(lastAccess))
|
|
|
|
rows.append('<tr><td><a href="%s">%s</a></td><td>%s</td></tr>' % \
|
|
|
|
(user.o.absolute_url(), user.title,access))
|
|
|
|
return res + '\n'.join(rows) + '</table>'
|
|
|
|
|
2011-01-28 07:36:30 -06:00
|
|
|
podOutputFormats = ('odt', 'pdf', 'doc', 'rtf')
|
|
|
|
def getPodOutputFormats(self):
|
|
|
|
'''Gets the available output formats for POD documents.'''
|
|
|
|
return [(of, self.translate(of)) for of in self.podOutputFormats]
|
|
|
|
|
2012-05-05 10:04:19 -05:00
|
|
|
def getInitiator(self, field=False):
|
2009-06-29 07:06:01 -05:00
|
|
|
'''Retrieves the object that triggered the creation of the object
|
2012-05-05 10:04:19 -05:00
|
|
|
being currently created (if any), or the name of the field in this
|
|
|
|
object if p_field is given.'''
|
2011-11-25 11:01:20 -06:00
|
|
|
nav = self.o.REQUEST.get('nav', '')
|
2012-05-05 10:04:19 -05:00
|
|
|
if not nav or not nav.startswith('ref.'): return
|
|
|
|
if not field: return self.getObject(nav.split('.')[1])
|
|
|
|
return nav.split('.')[2].split(':')[0]
|
2009-11-17 03:05:19 -06:00
|
|
|
|
|
|
|
def getObject(self, uid):
|
|
|
|
'''Allow to retrieve an object from its unique identifier p_uid.'''
|
|
|
|
return self.o.getObject(uid, appy=True)
|
2009-12-01 13:36:59 -06:00
|
|
|
|
|
|
|
def getDiskFolder(self):
|
|
|
|
'''Returns the disk folder where the Appy application is stored.'''
|
2012-05-08 07:49:45 -05:00
|
|
|
return self.o.config.diskFolder
|
|
|
|
|
|
|
|
def getClass(self, zopeName):
|
|
|
|
'''Gets the Appy class corresponding to technical p_zopeName.'''
|
|
|
|
return self.o.getAppyClass(zopeName)
|
2010-10-14 07:43:56 -05:00
|
|
|
|
|
|
|
def getAttributeName(self, attributeType, klass, attrName=None):
|
|
|
|
'''Some names of Tool attributes are not easy to guess. For example,
|
|
|
|
the attribute that stores the names of the columns to display in
|
|
|
|
query results for class A that is in package x.y is
|
|
|
|
"tool.resultColumnsForx_y_A". Other example: the attribute that
|
|
|
|
stores the editable default value of field "f1" of class x.y.A is
|
|
|
|
"tool.defaultValueForx_y_A_f1". This method generates the attribute
|
|
|
|
name based on p_attributeType, a p_klass from the application, and a
|
|
|
|
p_attrName (given only if needed, for example if p_attributeType is
|
|
|
|
"defaultValue"). p_attributeType may be:
|
|
|
|
|
|
|
|
"defaultValue"
|
|
|
|
Stores the editable default value for a given p_attrName of a
|
|
|
|
given p_klass.
|
|
|
|
|
|
|
|
"podTemplate"
|
|
|
|
Stores the pod template for p_attrName.
|
|
|
|
|
|
|
|
"formats"
|
|
|
|
Stores the output format(s) of a given pod template for
|
|
|
|
p_attrName.
|
|
|
|
|
|
|
|
"resultColumns"
|
|
|
|
Stores the list of columns that must be shown when displaying
|
|
|
|
instances of the a given root p_klass.
|
|
|
|
|
|
|
|
"enableAdvancedSearch"
|
|
|
|
Determines if the advanced search screen must be enabled for
|
|
|
|
p_klass.
|
|
|
|
|
|
|
|
"numberOfSearchColumns"
|
|
|
|
Determines in how many columns the search screen for p_klass
|
|
|
|
is rendered.
|
|
|
|
|
|
|
|
"searchFields"
|
|
|
|
Determines, among all indexed fields for p_klass, which one will
|
|
|
|
really be used in the search screen.
|
|
|
|
|
|
|
|
"optionalFields"
|
|
|
|
Stores the list of optional attributes that are in use in the
|
|
|
|
tool for the given p_klass.
|
|
|
|
|
|
|
|
"showWorkflow"
|
|
|
|
Stores the boolean field indicating if we must show workflow-
|
|
|
|
related information for p_klass or not.
|
|
|
|
|
|
|
|
"showAllStatesInPhase"
|
|
|
|
Stores the boolean field indicating if we must show all states
|
|
|
|
linked to the current phase or not. If this field is False, we
|
|
|
|
simply show the current state, be it linked to the current phase
|
|
|
|
or not.
|
|
|
|
'''
|
|
|
|
fullClassName = self.o.getPortalType(klass)
|
|
|
|
res = '%sFor%s' % (attributeType, fullClassName)
|
|
|
|
if attrName: res += '_%s' % attrName
|
|
|
|
return res
|
2011-01-14 02:06:25 -06:00
|
|
|
|
|
|
|
def getAvailableLanguages(self):
|
|
|
|
'''Returns the list of available languages for this application.'''
|
|
|
|
return [(t.id, t.title) for t in self.translations]
|
2011-01-18 08:48:55 -06:00
|
|
|
|
|
|
|
def convert(self, fileName, format):
|
|
|
|
'''Launches a UNO-enabled Python interpreter as defined in the self for
|
|
|
|
converting, using OpenOffice in server mode, a file named p_fileName
|
|
|
|
into an output p_format.'''
|
|
|
|
convScript = '%s/pod/converter.py' % os.path.dirname(appy.__file__)
|
|
|
|
cmd = '%s %s "%s" %s -p%d' % (self.unoEnabledPython, convScript,
|
|
|
|
fileName, format, self.openOfficePort)
|
|
|
|
self.log('Executing %s...' % cmd)
|
|
|
|
return executeCommand(cmd) # The result can contain an error message
|
2011-09-08 09:33:16 -05:00
|
|
|
|
|
|
|
def refreshSecurity(self):
|
|
|
|
'''Refreshes, on every object in the database, security-related,
|
|
|
|
workflow-managed information.'''
|
|
|
|
context = {'nb': 0}
|
|
|
|
for className in self.o.getProductConfig().allClassNames:
|
|
|
|
self.compute(className, context=context, noSecurity=True,
|
|
|
|
expression="ctx['nb'] += int(obj.o.refreshSecurity())")
|
|
|
|
msg = 'Security refresh: %d object(s) updated.' % context['nb']
|
|
|
|
self.log(msg)
|
2012-03-19 11:00:44 -05:00
|
|
|
|
|
|
|
def refreshCatalog(self, startObject=None):
|
|
|
|
'''Reindex all Appy objects. For some unknown reason, method
|
|
|
|
catalog.refreshCatalog is not able to recatalog Appy objects.'''
|
|
|
|
if not startObject:
|
2012-05-14 10:35:34 -05:00
|
|
|
# This is a global refresh. Clear the catalog completely, and then
|
2012-03-19 11:00:44 -05:00
|
|
|
# reindex all Appy-managed objects, ie those in folders "config"
|
|
|
|
# and "data".
|
|
|
|
# First, clear the catalog.
|
2012-09-04 11:00:22 -05:00
|
|
|
self.log('Recomputing the whole catalog...')
|
2012-03-19 11:00:44 -05:00
|
|
|
app = self.o.getParentNode()
|
|
|
|
app.catalog._catalog.clear()
|
|
|
|
nb = 1
|
2012-09-04 11:00:22 -05:00
|
|
|
failed = []
|
2012-03-19 11:00:44 -05:00
|
|
|
for obj in app.config.objectValues():
|
2012-09-04 11:00:22 -05:00
|
|
|
subNb, subFailed = self.refreshCatalog(startObject=obj)
|
|
|
|
nb += subNb
|
|
|
|
failed += subFailed
|
|
|
|
try:
|
|
|
|
app.config.reindex()
|
|
|
|
except:
|
|
|
|
failed.append(app.config)
|
2012-03-19 11:00:44 -05:00
|
|
|
# Then, refresh objects in the "data" folder.
|
|
|
|
for obj in app.data.objectValues():
|
2012-09-04 11:00:22 -05:00
|
|
|
subNb, subFailed = self.refreshCatalog(startObject=obj)
|
|
|
|
nb += subNb
|
|
|
|
failed += subFailed
|
|
|
|
# Re-try to index all objects for which reindexation has failed.
|
|
|
|
for obj in failed: obj.reindex()
|
|
|
|
if failed:
|
|
|
|
failMsg = ' (%d retried)' % len(failed)
|
|
|
|
else:
|
|
|
|
failMsg = ''
|
|
|
|
self.log('%d object(s) were reindexed%s.' % (nb, failMsg))
|
2012-03-19 11:00:44 -05:00
|
|
|
else:
|
|
|
|
nb = 1
|
2012-09-04 11:00:22 -05:00
|
|
|
failed = []
|
2012-03-19 11:00:44 -05:00
|
|
|
for obj in startObject.objectValues():
|
2012-09-04 11:00:22 -05:00
|
|
|
subNb, subFailed = self.refreshCatalog(startObject=obj)
|
|
|
|
nb += subNb
|
|
|
|
failed += subFailed
|
|
|
|
try:
|
|
|
|
startObject.reindex()
|
|
|
|
except Exception, e:
|
|
|
|
failed.append(startObject)
|
|
|
|
return nb, failed
|
2009-06-29 07:06:01 -05:00
|
|
|
# ------------------------------------------------------------------------------
|