[Zope3-Users] Re: Vocabularies beyond SimpleVocabulary
Philipp von Weitershausen
philipp at weitershausen.de
Mon Jun 12 14:59:23 EDT 2006
Piotr Chamera wrote:
> I am just working on similar code. I'm beginner in zope and python, so I
> post this code for improvements from other users. I have defined
> simple "factory" for my vocabularies (root folder is hardvired in the
> code, it gets subfolder by name and creates title from given attribute).
>
> """
> from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
>
> from zope.app import zapi
> from zope.proxy import removeAllProxies
>
> def getVocabulary(context, items_container, title_field):
> list = []
> root=zapi.getRoot(context)
> for (oid, oobj) in root.get("books").get(items_container).items():
> obj = removeAllProxies(oobj)
Don't remove (security) proxies here! You're totally disabling security
with this.
> list.append( SimpleTerm( obj, str(obj.__dict__[title_field]),
> obj.__dict__[title_field]))
This spelling is very awkward. It's probably also the reason why you
wanted to remove proxies above. You should write this as:
list.append(SimpleTerm(obj, getattr(obj, title_field),
getattr(obj, title_field)))
> return SimpleVocabulary( list )
Philipp
More information about the Zope3-users
mailing list