def createExprContext(folder, portal, object): expr_context = Expression.createExprContext(folder, portal, object) view_obj = object if view_obj is None: view_obj = portal req = view_obj.REQUEST globals_view = getMultiAdapter((view_obj, req), name='plone') expr_context.setGlobal('globals_view', globals_view) # XXX: For some reason, when using getMultiAdapter() here we get # authoriziation problems in some cases (e.g. when using one of these # in a python: expression in an action). plone_portal_state = view_obj.restrictedTraverse('@@plone_portal_state') expr_context.setGlobal('plone_portal_state', plone_portal_state) plone_context_state = view_obj.restrictedTraverse('@@plone_context_state') expr_context.setGlobal('plone_context_state', plone_context_state) plone_tools = view_obj.restrictedTraverse('@@plone_tools') expr_context.setGlobal('plone_tools', plone_tools) # Add checkPermission to the action expression context to make cleaner # faster expressions membership_tool = getToolByName(view_obj, 'portal_membership') checkPerm = membership_tool.checkPermission expr_context.setGlobal('checkPermission', checkPerm) return expr_context
def getExprContext(context, object=None): request = getattr(context, 'REQUEST', None) if request: cache = request.get('_ec_cache', None) if cache is None: request['_ec_cache'] = cache = {} ec = cache.get( id(object), None ) else: ec = None if ec is None: utool = getToolByName(context, 'portal_url') portal = utool.getPortalObject() if object is None or not hasattr(object, 'aq_base'): folder = portal else: folder = object # Search up the containment hierarchy until we find an # object that claims it's a folder. while folder is not None: if getattr(aq_base(folder), 'isPrincipiaFolderish', 0): # found it. break else: folder = aq_parent(aq_inner(folder)) ec = Expression.createExprContext(folder, portal, object) if request: cache[ id(object) ] = ec return ec
def createExprContext(folder, portal, object): expr_context = Expression.createExprContext(folder, portal, object) view_obj = object if view_obj is None: view_obj = portal req = view_obj.REQUEST expr_context.setGlobal('portal', portal) globals_view = getMultiAdapter((view_obj, req), name='plone') expr_context.setGlobal('globals_view', globals_view) # TODO: For some reason, when using getMultiAdapter() here we get # authoriziation problems in some cases (e.g. when using one of these # in a python: expression in an action). plone_portal_state = view_obj.restrictedTraverse('@@plone_portal_state') expr_context.setGlobal('plone_portal_state', plone_portal_state) plone_context_state = view_obj.restrictedTraverse('@@plone_context_state') expr_context.setGlobal('plone_context_state', plone_context_state) plone_tools = view_obj.restrictedTraverse('@@plone_tools') expr_context.setGlobal('plone_tools', plone_tools) # Add checkPermission to the action expression context to make cleaner # faster expressions membership_tool = getToolByName(view_obj, 'portal_membership') checkPerm = membership_tool.checkPermission expr_context.setGlobal('checkPermission', checkPerm) # add 'context' as an alias for 'object' expr_context.setGlobal('context', object) # need this for resolving in Unicode expressions expr_context.setContext('context', object) return expr_context
def createExprContext(folder, portal, object): expr_context = Expression.createExprContext(folder, portal, object) view_obj = object if view_obj is None: view_obj = portal req = view_obj.REQUEST expr_context.setGlobal("portal", portal) globals_view = getMultiAdapter((view_obj, req), name="plone") expr_context.setGlobal("globals_view", globals_view) # TODO: For some reason, when using getMultiAdapter() here we get # authoriziation problems in some cases (e.g. when using one of these # in a python: expression in an action). plone_portal_state = view_obj.restrictedTraverse("@@plone_portal_state") expr_context.setGlobal("plone_portal_state", plone_portal_state) plone_context_state = view_obj.restrictedTraverse("@@plone_context_state") expr_context.setGlobal("plone_context_state", plone_context_state) plone_tools = view_obj.restrictedTraverse("@@plone_tools") expr_context.setGlobal("plone_tools", plone_tools) # Add checkPermission to the action expression context to make cleaner # faster expressions membership_tool = getToolByName(view_obj, "portal_membership") checkPerm = membership_tool.checkPermission expr_context.setGlobal("checkPermission", checkPerm) # add 'context' as an alias for 'object' expr_context.setGlobal("context", object) # need this for resolving in Unicode expressions expr_context.setContext("context", object) return expr_context
def items(self): """Use the item brains""" # Mostly copied from plone.app.content.browser.foldercontents, # modified to use catalog brains context = aq_inner(self.context) plone_utils = getToolByName(context, 'plone_utils') plone_view = getMultiAdapter((context, self.request), name=u'plone') portal_workflow = getToolByName(context, 'portal_workflow') portal_properties = getToolByName(context, 'portal_properties') portal_types = getToolByName(context, 'portal_types') site_properties = portal_properties.site_properties portal = getToolByName(context, 'portal_url').getPortalObject() use_view_action = site_properties.getProperty( 'typesUseViewActionInListings', ()) browser_default = context.browserDefault() econtext = Expression.getExprContext(context) results = [] for i, obj in enumerate(self.batch): if (i + 1) % 2 == 0: table_row_class = "draggable even" else: table_row_class = "draggable odd" url = obj.getURL() path = obj.getPath or "/".join(obj.getPhysicalPath()) icon = plone_view.getIcon(obj) type_class = 'contenttype-' + plone_utils.normalizeString( obj.portal_type) review_state = obj.review_state state_class = 'state-' + plone_utils.normalizeString(review_state) relative_url = obj.getURL(relative=True) type_title_msgid = portal_types[obj.portal_type].Title() url_href_title = u'%s: %s' % (translate(type_title_msgid, context=self.request), safe_unicode(obj.Description)) modified = plone_view.toLocalizedTime( obj.ModificationDate, long_format=1) obj_type = obj.Type if obj_type in use_view_action: view_url = url + '/view' elif obj.is_folderish: view_url = url + "/folder_contents" else: view_url = url is_browser_default = len(browser_default[1]) == 1 and ( obj.id == browser_default[1][0]) columns = {} for column in self.columns.ordered: value = getattr(obj, column['field'], MV) # Calculate the sums before using any expression if value is not MV: if column['has_sum']: if 'sum' in column: column['sum'] += value else: column['sum'] = value expr = column.get('expr') if expr: econtext.vars.update(item=obj, value=value) value = econtext.evaluate(expr) del econtext.vars['value'] del econtext.vars['item'] if IDateTime.providedBy(value): value = plone_view.toLocalizedTime(value, long_format=1) columns[column['field']] = value results.append(dict( url=url, url_href_title=url_href_title, id=obj.getId, quoted_id=urllib.quote_plus(obj.getId), path=path, title_or_id=obj.pretty_title_or_id(), obj_type=obj_type, size=obj.getObjSize, modified=modified, icon=icon.html_tag(), type_class=type_class, wf_state=review_state, state_title=portal_workflow.getTitleForStateOnType( review_state, obj_type), state_class=state_class, is_browser_default=is_browser_default, folderish=obj.is_folderish, relative_url=relative_url, view_url=view_url, table_row_class=table_row_class, is_expired=context.isExpired(obj), obj=obj, columns=columns, )) for column in self.columns.ordered: if not column['has_sum'] or 'sum' not in column: continue expr = column.get('expr') if expr: econtext.vars.update(item=obj, value=column['sum']) column['sum'] = econtext.evaluate(expr) del econtext.vars['value'] del econtext.vars['item'] return results