Re: [Zope3-Users] Re: Question about ForbiddenAttribute...

2006-06-14 Thread Thierry FLORAC
On Tue, 2006-06-13 at 08:28 +0200, Philipp von Weitershausen wrote:
 As said above, 'result' will be security proxied. Security proxies for
 lists only allow methods that don't change the lists. result.sort()
 would change it. Hence it's forbidden.
 
 An easy workaround is to use sorted():
 
   result = IGalleryFolder(self.context).getImages()
   return sorted(result, cmp=...)
 
 Note that using 'cmp' is very slow. It's much faster to use 'key':
 
   result = IGalleryFolder(self.context).getImages()
   return sorted(result, key=lambda x: IGalleryPhoto(x).name)

Tried this, but it doesn't work because of another ForbiddenAttribute
error :

  File .../gallery/browser/folder.py, line 39, in getImages
return sorted(result, key=lambda x: IGalleryPhoto(x).name)
  File .../gallery/browser/folder.py, line 39, in lambda
return sorted(result, key=lambda x: IGalleryPhoto(x).name)
ForbiddenAttribute: ('name', gallery.photo.GalleryPhoto object at
0xa5bc46ac)

As said before, 'IGalleryPhoto' (implemented by GalleryPhoto) is
provided in the allowed_interface attribute of my ZCML's view page
definition...

Thierry



-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


[Zope3-Users] Re: Question about ForbiddenAttribute...

2006-06-13 Thread Philipp von Weitershausen
Thierry FLORAC wrote:
 Hi,
 
 I'm trying to build a sample photos management application for Zope-3.2,
 with the following interfaces and classes :
 

  - class GalleryFolder(Folder)
  def getImages(self):
return [item for item in self.values() 
  if IGalleryPhoto.providedBy(item)]

The list object you're returning here will be security proxied because
it's returned to a view.

  - class Gallery(GalleryFolder, SiteManagerContainer)
  ...
 
 Until now, it's OK !
 After that, I created a sample view based on a page template for
 IGalleryFolder, using a view class :
 
   - tal:x repeat=image view/getImages ... /tal:x
 
   - class FolderInfo:
 
   def getImages(self):
 Get a sorted list of images
 result = IGalleryFolder(self.context).getImages()
 result.sort (lambda x,y: cmp(IGalleryPhoto(x).name,
  IGalleryPhoto(y).name))
 return result

As said above, 'result' will be security proxied. Security proxies for
lists only allow methods that don't change the lists. result.sort()
would change it. Hence it's forbidden.

An easy workaround is to use sorted():

  result = IGalleryFolder(self.context).getImages()
  return sorted(result, cmp=...)

Note that using 'cmp' is very slow. It's much faster to use 'key':

  result = IGalleryFolder(self.context).getImages()
  return sorted(result, key=lambda x: IGalleryPhoto(x).name)

With this, sorted() will use IGalleryPhoto(...).name of each item as a
sort key. The lambda expession will only be called once for every
object. A 'cmp' function would be called many more times.

 But when trying to display the view, a ForbiddenAttribute exception is
 raised :
   File .../browser/folder.py, line 39, in getImages
 result.sort (lambda x,y:
  cmp(IGalleryPhoto(x).name,IGalleryPhoto(y).name))
   ForbiddenAttribute:('sort',[GalleryPhoto object at 0xa5b378ac,...] 
 
 So the forbidden attribute seems to be the sort method of the
 resulting array, which of course isn't part of any of my interfaces.

Nitpick: This isn't an array, it's a list. There's a big difference
between those two (lists can contain arbitrary objects, for example).

Philipp

___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users