getting size of zope.schema.List from a view in bluebream
Hello all, While trying to get the length of zope.schema.List field in a view (like this: len(self.context.list_field) I get the following error ForbiddenAttribute: ('__len__', []) Even though my zcml configuration for that class is defined as below <class class=".app.MyObject"> <require permission="zope.Public" interface=".interfaces.IMyObject" /> <require permission="zope.Public" set_schema=".interfaces.IMyObject" /> </class> I still get the above error. To circumvent this issue, I added a method in the interface like say 'getListSize()' which has the following implementation class MyObject(persistent.Persistent): implements(IMyObject) name = u'' list_field = PersistentList() ... def getListSize(self): return len(self.list_field) This solves my issue. However, I am curious to know whether is this the only solution available to the above issue. Am I going in the right direction? Please guide me. -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
Hello, You forgot to paste interfaces.IMyObject definition. On Tue, 23 Aug 2011 13:16:24 +0530 you wrote:
Hello all, While trying to get the length of zope.schema.List field in a view (like this: len(self.context.list_field) I get the following error
ForbiddenAttribute: ('__len__', [])
Even though my zcml configuration for that class is defined as below
<class class=".app.MyObject">
<require permission="zope.Public" interface=".interfaces.IMyObject" />
<require permission="zope.Public" set_schema=".interfaces.IMyObject" /> </class>
I still get the above error. To circumvent this issue, I added a method in the interface like say 'getListSize()' which has the following implementation
class MyObject(persistent.Persistent): implements(IMyObject)
name = u'' list_field = PersistentList() ...
def getListSize(self): return len(self.list_field)
This solves my issue. However, I am curious to know whether is this the only solution available to the above issue. Am I going in the right direction?
Please guide me.
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
-- Best regards, Adam GROSZER -- Quote of the day: Two men please God: who serves Him with all his heart because he knows Him; who seeks Him with all his heart because he knows Him not. - Nikita Ivanovich Panin
Hello Adam, On Tue, 2011-08-23 at 11:29 +0200, Adam GROSZER wrote:
You forgot to paste interfaces.IMyObject definition.
Sorry. Here is my interface definition. class IMyObject(Interface): name = TextLine( title=u'Name') list_field = List( title=u'My List', value_type=Object( title=u'Some other object', schema=IOtherObject) ) ... As there was nothing complex involved in the interface definition, I omitted them in my original post. -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
Hello all, On Tue, 2011-08-23 at 13:16 +0530, Joshua Immanuel wrote:
def getListSize(self): return len(self.list_field)
This solves my issue.
This solves just the '__len__' issue. But if do the slice operation like this self.context.list_field[offset:limit] I get the following error ForbiddenAttribute: ('__getslice__',[...]) I guess my approach is flawed. Implementing all the functionality (like the one I did for length) that a list provides is a overkill. So, please guide me in this regard. Thanks -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
Am 23.08.2011, 13:34 Uhr, schrieb Joshua Immanuel <josh@hipro.co.in>:
This solves just the '__len__' issue. But if do the slice operation like this self.context.list_field[offset:limit] I get the following error ForbiddenAttribute: ('__getslice__',[...]) I guess my approach is flawed. Implementing all the functionality (like the one I did for length) that a list provides is a overkill. So, please guide me in this regard.
Joshua, I think it's really difficult to work out what you are trying to do. Please state your problem more clearly. Are you still using zope.form or are you using z3c.form? Charlie -- Charlie Clark Managing Director Clark Consulting & Research German Office Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-600-3657 Mobile: +49-178-782-6226
Hello Charlie, Thanks for the reply. On Tue, 2011-08-23 at 13:56 +0200, Charlie Clark wrote:
I think it's really difficult to work out what you are trying to do. Please state your problem more clearly. Are you still using zope.form or are you using z3c.form?
I am still using zope.formlib. I guess I found the root of the problem. All the schema fields are security proxied I guess. Removing the security proxy from those fields before accessing the field's inbuilt methods solves the problem. So, my code now looks like this from zope.security.proxy import removeSecurityProxy sz = len(removeSecurityProxy(self.context.list_field)) ... li = removeSecurityProxy(self.context.list_field) res = li[offset:limit] I guess this is a cleaner approach. Regards -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
Hello, Well using removeSecurityProxy is not so nice. If there's really a list in your property, then zope should give you zope.Public access: _default_checkers = { ... list: NamesChecker(['__getitem__', '__getslice__', '__len__', '__iter__', '__contains__', 'index', 'count', '__str__', '__add__', '__radd__', ]), ... Can you do: from zope.security.proxy import removeSecurityProxy li = removeSecurityProxy(self.context.list_field) print type(li) On Tue, 23 Aug 2011 17:43:22 +0530 you wrote:
Hello Charlie,
Thanks for the reply.
On Tue, 2011-08-23 at 13:56 +0200, Charlie Clark wrote:
I think it's really difficult to work out what you are trying to do. Please state your problem more clearly. Are you still using zope.form or are you using z3c.form?
I am still using zope.formlib. I guess I found the root of the problem. All the schema fields are security proxied I guess. Removing the security proxy from those fields before accessing the field's inbuilt methods solves the problem. So, my code now looks like this
from zope.security.proxy import removeSecurityProxy
sz = len(removeSecurityProxy(self.context.list_field)) ...
li = removeSecurityProxy(self.context.list_field) res = li[offset:limit]
I guess this is a cleaner approach.
Regards
_______________________________________________ Zope-Dev maillist - Zope-Dev@zope.org https://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - https://mail.zope.org/mailman/listinfo/zope-announce https://mail.zope.org/mailman/listinfo/zope )
-- Best regards, Adam GROSZER -- Quote of the day: The celestial order and the beauty of the universe compel me to admit that there is some excellent and eternal Being, Who deserves the respect and homage of men. - Cicero
Hello Adam, Thanks for the reply. On Tue, 2011-08-23 at 14:39 +0200, Adam GROSZER wrote:
Well using removeSecurityProxy is not so nice.
If there's really a list in your property, then zope should give you zope.Public access:
_default_checkers = { ... list: NamesChecker(['__getitem__', '__getslice__', '__len__', '__iter__', '__contains__', 'index', 'count', '__str__', '__add__', '__radd__', ]), ...
Can you do:
from zope.security.proxy import removeSecurityProxy
li = removeSecurityProxy(self.context.list_field) print type(li)
As I am using the persistent List in the implementation of IMyObject, the output of the above is <class 'persistent.list.PersistentList'> So, How can I access its inbuilt methods without removing the security proxy. Please guide me. Regards -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
participants (3)
-
Adam GROSZER -
Charlie Clark -
Joshua Immanuel