2012-05-07 38 views
2

發佈內容項進行的請求[「發佈」]我嘗試從事件快速在Plone

@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) 

獲取上下文對象IPubAfterTraversal鉤我想要做的:

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

出版不是內容項上下文,但是:

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

PUBLISHED可能指向視圖,也可能不指向視圖。使用HTTPRequest獲取發佈的內容項目對象(如果有的話)的最安全方法是什麼?

回答

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

+0

作品。這裏是完整的故事:https://github.com/miohtama/silvuple/blob/master/silvuple/negotiator.py –