Plone中的已发布内容项不在请求[“PUBLISHED”]中

时间:2012-05-07 21:39:54

标签: plone zope

我尝试从事件

中获取IPubAfterTraversal挂钩中的上下文对象
@grok.subscribe(IPubAfterTraversal)
def admin_language_negotiator(event):
    """
    Event handler which pokes the language after traversing and authentication is done, but before rendering.
    """
    # Keep the current request language (negotiated on portal_languages)
    # untouched

    request = event.request

    if not IAddonSpecific.providedBy(request):
        # Add on is not active
        return

    context = request.get("PUBLISHED", None)

我想这样做:

   IContentish.providedBy(context) # Check if real content request or CSS/Image request

PUBLISHED不是内容项上下文,但是:

    context
   <FSPageTemplate at /Plone/en/plan/plan/document_view>

PUBLISHED可能会也可能不会指向某个视图。从HTTPRequest获取已发布内容项对象(如果有)的最安全方法是什么?

1 个答案:

答案 0 :(得分:4)

plone.app.theming就是这样的:

def findContext(request):
    """Find the context from the request
    """
    published = request.get('PUBLISHED', None)
    context = getattr(published, '__parent__', None)
    if context is None:
        context = request.PARENTS[0]
    return context

https://github.com/plone/plone.app.theming/blob/master/src/plone/app/theming/utils.py#L146