Provide 'request' attribute for adapted objects.

Put the current request (if available) into concept or resource adapters in order to be able to access the session an follow the navigation history, e.g. getting the current parent object. The request is stored in the adapted object automatically when using the 'adapted' view property.
This commit is contained in:
Helmut Merz 2011-04-28 12:15:13 +02:00
parent c4376c8a30
commit a69bda06d7
3 changed files with 7 additions and 4 deletions

View file

@ -312,7 +312,7 @@ class BaseView(GenericView, I18NView):
@Lazy
def adapted(self):
return adapted(self.context, self.languageInfo)
return adapted(self.context, self.languageInfo, self.request)
@Lazy
def baseObject(self):

View file

@ -1,5 +1,5 @@
#
# Copyright (c) 2008 Helmut Merz helmutm@cy55.de
# Copyright (c) 2011 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -43,10 +43,11 @@ from loops import util
# convenience functions
def adapted(obj, langInfo=None):
def adapted(obj, langInfo=None, request=None):
""" Return adapter based on the object type's type interface.
"""
if isinstance(obj, AdapterBase):
obj.request = request
return obj
t = IType(obj, None)
if t is not None:
@ -57,6 +58,7 @@ def adapted(obj, langInfo=None):
if isinstance(adapted, I18NAdapterBase):
adapted.languageInfo = langInfo
if adapted is not None:
adapted.request = request
return adapted
return obj
@ -98,7 +100,7 @@ class AdapterBase(object):
adapts(IConcept)
_adapterAttributes = ('context', '__parent__',)
_adapterAttributes = ('context', '__parent__', 'request')
_contextAttributes = list(IConcept)
_noexportAttributes = ()
_textIndexAttributes = ()

View file

@ -200,6 +200,7 @@ class ILoopsAdapter(IConceptSchema):
context = Attribute('The underlying persistent object.')
uid = Attribute('Unique id of the context object.')
request = Attribute('A a mapping with current settings, optional.')
def getChildren():
""" Return a collection of child objects provided by the context