def _getLanguage(self): """determine language""" plt = getToolByName(self, 'portal_languages', None) if plt is not None: # if we have PLT take it to vary the language lang = plt.getPreferredLanguage() else: # try to get it from PTS accepted = getLangPrefs(self.REQUEST) if len(accepted) > 0: lang = accepted[0] else: # bummer, it cant determine a language lang = 'neutral' return lang and lang[:2] or None
def _makeCacheKeywords(self, vocab): """returns a key for use in the cache.""" key = {} key['uid'] = vocab.UID() plt = getToolByName(self, 'portal_languages', None) if plt is not None: # if we have PLT take it to vary the language lang = plt.getPreferredLanguage() key['lang'] = lang else: # try to get it from PTS accepted = getLangPrefs(self.REQUEST) if len(accepted) > 0: key['lang'] = accepted[0] else: # bummer, it cant determine a language key['lang'] = 'neutral' return key
def getPreferredLanguages(self): from Products.PlacelessTranslationService.Negotiator import getLangPrefs return getLangPrefs(self.context)
def _fFindCachedEntryAndDirectoryAndFilePathForElement( self, theModelDDvlPloneTool, theContextualObject, theTemplateName, theEnforceThreadSafety=True): """ Return an existing CacheEntry from cache, for a project, for an element (by it's UID), for the specified view, and for the currently negotiared language. """ unaCachedEntry = None unDirectory = '' unFilePath = '' if theModelDDvlPloneTool == None: return [ unaCachedEntry, unDirectory, unFilePath, ] if theContextualObject == None: return [ unaCachedEntry, unDirectory, unFilePath, ] # ########################################################### """Only cache objects that allow caching. """ anIsCacheable = False try: anIsCacheable = theContextualObject.fIsCacheable() except: None if not anIsCacheable: return [ unaCachedEntry, unDirectory, unFilePath, ] # ########################################################### """Gather all information to look up the cache for a matching cached entry with a rendered template. """ aProjectName = '' try: aProjectName = theContextualObject.getNombreProyecto() except: None if not aProjectName: aProjectName = cDefaultNombreProyecto unElementUID = '' try: unElementUID = theContextualObject.UID() except: None if not unElementUID: return [ unaCachedEntry, unDirectory, unFilePath, ] unElementId = '' try: unElementId = theContextualObject.getId() except: None unRootElementId = '' unRootElementUID = '' unRootElement = None try: unRootElement = theContextualObject.getRaiz() except: None if (unRootElement == None): unRootElementUID = unElementUID unRootElementId = unElementId else: try: unRootElementUID = unRootElement.UID() except: None if not unRootElementUID: unRootElementUID = unElementUID try: unRootElementId = unRootElement.getId() except: None if not unRootElementId: unRootElementId = unElementId unRootElementURL = unRootElement.absolute_url() unParsedRootElementURL = self.fURLParse( theModelDDvlPloneTool, theContextualObject, unRootElementURL, ) if (not unParsedRootElementURL): return [ unaCachedEntry, unDirectory, unFilePath, ] if (not unParsedRootElementURL[0]) or (not unParsedRootElementURL[1]): return [ unaCachedEntry, unDirectory, unFilePath, ] unScheme = unParsedRootElementURL[0] unDomain = unParsedRootElementURL[1] if not (unScheme and unDomain): return [ unaCachedEntry, unDirectory, unFilePath, ] unDomain = unDomain.replace(':', '-p-') unSchemeHostAndDomain = '%s-%s' % ( unScheme, unDomain, ) # ########################################################### """Determine if the user specific cache should be used. Determine the role kind to use to index the cache entry. """ aCacheName = cCacheName_ForElements aCacheKind = cCacheKind_ForElements aRoleKindToIndex = cRoleKind_Anonymous aRoleKind, unMemberId, aRoleName = self.fGetMemberRoleKindAndUserId( theModelDDvlPloneTool, theContextualObject, theTemplateName) aRoleKindToIndex = aRoleKind if (aRoleKind == cRoleKind_UserSpecific): if self.fIsPrivateCacheViewForQualifiedUsers( theContextualObject, theTemplateName): aRoleKindToIndex = unMemberId aCacheName = cCacheName_ForUsers aCacheKind = cCacheKind_ForUsers else: aRoleKindToIndex = aRoleName aCacheName = cCacheName_ForElements aCacheKind = cCacheKind_ForElements if not aRoleKindToIndex: return [ unaCachedEntry, unDirectory, unFilePath, ] # ################################################################### """If theEnforceThreadSafety : CRITICAL SECTION to access configuration information. """ try: # ################# """If theEnforceThreadSafety MUTEX LOCK. """ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if theEnforceThreadSafety: self.pAcquireCacheLock( theModelDDvlPloneTool, theContextualObject, ) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # ########################################################### """Retrieve within thread safe section, to be used later, the configuration paameters specifying: whether to render a cache hit information collapsible section at the top or bottom of the view, or none at all, whether the disk caching is enabled, and the disk cache files base path. """ aCacheDiskEnabled = theModelDDvlPloneTool.fGetCacheConfigParameterValue( theContextualObject, aCacheName, cCacheConfigPpty_CacheDiskEnabled) aCacheDiskPath = theModelDDvlPloneTool.fGetCacheConfigParameterValue( theContextualObject, aCacheName, cCacheConfigPpty_CacheDiskPath) finally: # ################# """If theEnforceThreadSafety MUTEX UNLOCK. """ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if theEnforceThreadSafety: self.pReleaseCacheLock( theModelDDvlPloneTool, theContextualObject, ) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if aCacheDiskEnabled: # ########################################################### """Assemble the name of the directory holding all disk files with cached HTML for all entries on the element. """ aProjectPath = os.path.join(aCacheDiskPath, aProjectName) unRootElementIdShortened = unRootElementId[: cMaxRootElementIdInDiscCachePath] unRootElementIdShortened = unRootElementIdShortened.replace( ' ', '_').strip() aRootElementFolderName = '%s-%s' % ( unRootElementIdShortened, unRootElementUID, ) aRootUIDPath = os.path.join(aProjectPath, aRootElementFolderName) anElementUIDModulus = self.fModulusUID(unElementUID, cCacheDisk_UIDModulus) aElementUIDModulusPath = os.path.join(aRootUIDPath, anElementUIDModulus) unElementIdShortened = unElementId[:cMaxElementIdInDiscCachePath] unElementIdShortened = unElementIdShortened.replace(' ', '_').strip() anElementFolderName = '%s-%s' % ( unElementIdShortened, unElementUID, ) aElementUIDPath = os.path.join(aElementUIDModulusPath, anElementFolderName) unDirectory = aElementUIDPath # ########################################################### """Determine the language under which to register the cache entry. """ unosPreferredLanguages = getLangPrefs(theContextualObject.REQUEST) if not unosPreferredLanguages: return [ unaCachedEntry, unDirectory, unFilePath, ] aNegotiatedLanguage = unosPreferredLanguages[0] if not aNegotiatedLanguage: return [ unaCachedEntry, unDirectory, unFilePath, ] # ########################################################### """Determine the view name. """ aViewName = theTemplateName if not aViewName: return False if aViewName.find('%s') >= 0: if not (aProjectName == cDefaultNombreProyecto): aViewName = aViewName % aProjectName else: aViewName = aViewName.replace('%s', '') if not aViewName: return [ unaCachedEntry, unDirectory, unFilePath, ] unRelationCursorName = '' unCurrentElementUID = '' unaRequest = theContextualObject.REQUEST if unaRequest: unRelationCursorName = unaRequest.get('theRelationCursorName', '') unCurrentElementUID = unaRequest.get('theCurrentElementUID', '') if not unRelationCursorName: unRelationCursorName = cNoRelationCursorName if not unCurrentElementUID: unCurrentElementUID = cNoCurrentElementUID if aCacheDiskEnabled: # ########################################################### """Assemble the name of the directory holding all disk files with cached HTML for all entries on the element. """ aLanguagePath = os.path.join(aElementUIDPath, aNegotiatedLanguage) aViewNameShortened = aViewName if cNoHeaderNoFooterToReplaceForCachePaths in aViewName: aViewNameShortened = aViewName.replace( cNoHeaderNoFooterToReplaceForCachePaths, cNoHeaderNoFooterReplacementForCachePaths) aFileName = '%s-%s-%s-%s-%s%s' % ( aViewNameShortened, unRelationCursorName, unCurrentElementUID, unSchemeHostAndDomain, aRoleKindToIndex, cCacheDiskFilePostfix) aFilePath = os.path.join(aLanguagePath, aFileName) if cForbidCaches or not self.fGetCacheConfigParameter_CacheEnabled( theModelDDvlPloneTool, theContextualObject, aCacheName): return [ unaCachedEntry, unDirectory, unFilePath, ] # ################################################################### """IF theEnforceThreadSafety: CRITICAL SECTION to access and modify cache control structure, for Cache entries associated with specific elements. """ try: # ################# """IF theEnforceThreadSafety MUTEX LOCK. """ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if theEnforceThreadSafety: self.pAcquireCacheLock( theModelDDvlPloneTool, theContextualObject, ) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # ########################################################### """Traverse cache control structures to access the cache entry corresponding to the parameters. """ someCachedTemplatesForProject = self.fGetCachedTemplatesForProject( theModelDDvlPloneTool, theContextualObject, aCacheName, aProjectName) if someCachedTemplatesForProject: someCachedTemplatesForLanguage = someCachedTemplatesForProject.get( aNegotiatedLanguage, None) if someCachedTemplatesForLanguage: someCachedTemplatesForRoot = someCachedTemplatesForLanguage.get( unRootElementUID, None) if someCachedTemplatesForRoot: someCachedTemplatesForElement = someCachedTemplatesForRoot.get( unElementUID, None) if someCachedTemplatesForElement: someCachedTemplatesForView = someCachedTemplatesForElement.get( aViewName, None) if someCachedTemplatesForView: someCachedTemplateForRelationCursor = someCachedTemplatesForView.get( unRelationCursorName, None) if someCachedTemplateForRelationCursor: someCachedTemplateForRelatedElement = someCachedTemplateForRelationCursor.get( unCurrentElementUID, None) if someCachedTemplateForRelatedElement: someCachedTemplateForSchemeHostAndDomain = someCachedTemplateForRelatedElement.get( unSchemeHostAndDomain, None) if someCachedTemplateForSchemeHostAndDomain: unaCachedEntry = someCachedTemplateForSchemeHostAndDomain.get( aRoleKindToIndex, None) finally: # ################# """IF theEnforceThreadSafety MUTEX UNLOCK. """ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if theEnforceThreadSafety: self.pReleaseCacheLock( theModelDDvlPloneTool, theContextualObject, ) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! return [ unaCachedEntry, unDirectory, unFilePath, ]