# ------------------------------------------------------------------------------ import os.path, time import appy from appy.px import Px from appy.gen.mail import sendMail from appy.gen.wrappers import AbstractWrapper from appy.shared.utils import executeCommand # ------------------------------------------------------------------------------ class ToolWrapper(AbstractWrapper): # -------------------------------------------------------------------------- # Navigation-related PXs # -------------------------------------------------------------------------- # Icon for hiding/showing details below the title of an object shown in a # list of objects. pxShowDetails = Px(''' ''') # Displays up/down arrows in a table header column for sorting a given # column. Requires variables "sortable", 'filterable' and 'field'. pxSortAndFilter = Px(''' ''') # Buttons for navigating among a list of objects (from a Ref field or a # query): next,back,first,last... pxNavigate = Px('''
:startNumber + 1 :startNumber + batchNumber // :totalNumber
''') # -------------------------------------------------------------------------- # PXs for graphical elements shown on every page # -------------------------------------------------------------------------- # Global elements included in every page. pxPagePrologue = Px('''
''') pxPageBottom = Px(''' ''') pxPortlet = Px(''' :phase.pxView
:search.px :search.pxView
''') # The message that is shown when a user triggers an action. pxMessage = Px('''
::messages
''') # The page footer. pxFooter = Px(''' ''') # Hook for defining a PX that proposes additional links, after the links # corresponding to top-level pages. pxLinks = Px('') # Hook for defining a PX that proposes additional icons after standard # icons in the user strip. pxIcons = Px('') # Displays the content of a layouted object (a page or a field). If the # layouted object is a page, the "layout target" (where to look for PXs) # will be the object whose page is shown; if the layouted object is a field, # the layout target will be this field. pxLayoutedObject = Px('''
:getattr(layoutTarget, px)
''') pxHome = Px('''
::_('front_page_text')
''', template=AbstractWrapper.pxTemplate, hook='content') # Show on query list or grid, the field content for a given object. pxQueryField = Px(''' ::zobj.getSupTitle(navInfo) :zobj.Title():zobj.Title()::zobj.getSubTitle()
:targetObj.appy().pxTransitions
:field.pxRender ''') # Show query results as a list. pxQueryResultList = Px('''
::ztool.truncateText(_(field.labelId)) :tool.pxSortAndFilter:tool.pxShowDetails
:tool.pxQueryField
''') # Show query results as a grid. pxQueryResultGrid = Px('''
:tool.pxQueryField
''') # Show paginated query results as a list or grid. pxQueryResult = Px('''
:field.pxRender

:uiSearch.translated (:totalNumber)  —  :_('search_new')

:uiSearch.translatedDescr
:tool.pxNavigate
:tool.pxQueryResultList :tool.pxQueryResultGrid :tool.pxNavigate
:_('query_no_result')
:_('search_new')
''') pxQuery = Px(''' :tool.pxPagePrologue:tool.pxQueryResult ''', template=AbstractWrapper.pxTemplate, hook='content') pxSearch = Px('''

:_('%s_plural'%className):_('search_title')


''', template=AbstractWrapper.pxTemplate, hook='content') def isManager(self): '''Some pages on the tool can only be accessed by managers.''' if self.user.has_role('Manager'): return 'view' def isManagerEdit(self): '''Some pages on the tool can only be accessed by managers, also in edit mode.''' if self.user.has_role('Manager'): return True def computeConnectedUsers(self): '''Computes a table showing users that are currently connected.''' res = '' \ '' % \ self.translate('last_user_access') rows = [] for userId, lastAccess in self.o.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('' % \ (user.o.absolute_url(), user.title,access)) return res + '\n'.join(rows) + '
%s
%s%s
' def getInitiator(self, field=False): '''Retrieves the object that triggered the creation of the object being currently created (if any), or the name of the field in this object if p_field is given.''' nav = self.o.REQUEST.get('nav', '') if not nav or not nav.startswith('ref.'): return if not field: return self.getObject(nav.split('.')[1]) return nav.split('.')[2].split(':')[0] def getObject(self, uid): '''Allow to retrieve an object from its unique identifier p_uid.''' return self.o.getObject(uid, appy=True) def getDiskFolder(self): '''Returns the disk folder where the Appy application is stored.''' return self.o.config.diskFolder def getClass(self, zopeName): '''Gets the Appy class corresponding to technical p_zopeName.''' return self.o.getAppyClass(zopeName) def getAvailableLanguages(self): '''Returns the list of available languages for this application.''' return [(t.id, t.title) for t in self.translations] 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 def sendMail(self, to, subject, body, attachments=None): '''Sends a mail. See doc for appy.gen.mail.sendMail.''' sendMail(self, to, subject, body, attachments=attachments) 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: # This is a global refresh. Clear the catalog completely, and then # reindex all Appy-managed objects, ie those in folders "config" # and "data". # First, clear the catalog. self.log('Recomputing the whole catalog...') app = self.o.getParentNode() app.catalog._catalog.clear() nb = 1 failed = [] for obj in app.config.objectValues(): subNb, subFailed = self.refreshCatalog(startObject=obj) nb += subNb failed += subFailed try: app.config.reindex() except: failed.append(app.config) # Then, refresh objects in the "data" folder. for obj in app.data.objectValues(): 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)) else: nb = 1 failed = [] for obj in startObject.objectValues(): subNb, subFailed = self.refreshCatalog(startObject=obj) nb += subNb failed += subFailed try: startObject.reindex() except Exception, e: failed.append(startObject) return nb, failed # ------------------------------------------------------------------------------