[Zope3-Users] Vocabularies beyond SimpleVocabulary

Anders Bruun Olsen anders at bruun-olsen.net
Tue Jun 13 10:05:05 EDT 2006


On Mon, Jun 12, 2006 at 06:25:57PM +0200, 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).

But you then duplicate alot of data in the process, which for my
purposes seems like a waste of resources. You basically just run through
all objects in the folder in question and create a SimpleVocabulary
based on that. In my case there might be thousands of people and books
in the database, which makes it quite expensive to drag all of them out
of the database and put into a SimpleVocabulary.
Like all the guides and books where I have read about vocabularies we
are back to just using SimpleVocabulary which simply doesn't seem
acceptable for my needs here.

I have however experimented a bit more and have had some progress but am
once again stuck.

I have a Book class and a BookContainer class. They look like this:

class Book(SQLOS):
    implements(IBook, IBookContained, ITitledTokenizedTerm)
    title = UnicodeCol(length=250)
    description = UnicodeCol(default=None)
    personauthors = RelatedJoin("Person")
    def _get_token(self):
        return self.id
    def _get_value(self):
        return self


class BookContainer(SQLObjectContainer):
    implements(IBookContainer, IVocabularyTokenized)
    def getTerm(self, value):
        return value
    def getTermByToken(self, token):
        return self.get(token)

I then have a class to "create" a vocabulary instance:
def book_container_vocabulary(context):
    return BookContainer()

And in zcml I register the vocabulary:
    <vocabulary
        name="books"
        factory=".bookbase.book_container_vocabulary"
        />

I have similar classes for Person and PersonContainer. When
BookContainer is empty a correct widget is actually created on the
edit/add-form for Person objects. But when I add a Book object to
BookContainer the trouble starts.

2006-06-13T15:54:57 ERROR SiteError http://localhost:8080/testy/books/+/AddBook.html%3D
Traceback (most recent call last):
...
  File "/home/abo/sandbox/zope3/zope3.2.1/lib/python/zope/app/form/browser/add.py", line 121, in createAndAdd
    field.set(adapted, data[name])
  File "/home/abo/sandbox/zope3/zope3.2.1/lib/python/zope/schema/_bootstrapfields.py", line 183, in set
    setattr(object, self.__name__, value)
AttributeError: can't set attribute

The object is actually created (and is present in the database). But if
I try to edit the object this traceback happens:

2006-06-13T16:02:44 ERROR SiteError http://localhost:8080/testy/books/bookbase.Book.2/@@edit.html
Traceback (most recent call last):
...
  File "/home/abo/sandbox/zope3/zope3.2.1/lib/python/zope/app/form/browser/itemswidgets.py", line 566, in choices
    return [{'text': self.textForValue(term), 'value': term.token}
AttributeError: 'unicode' object has no attribute 'value'


I can't figure out from this traceback what the error is. I probably do
this completely wrong, but I can't really figure out what the correct
way to do this is.

-- 
Anders
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS/O d--@ s:+ a-- C++ UL+++$ P++ L+++ E- W+ N(+) o K? w O-- M- V
PS+ PE@ Y+ PGP+ t 5 X R+ tv+ b++ DI+++ D+ G e- h !r y?
------END GEEK CODE BLOCK------
PGPKey: http://random.sks.keyserver.penguin.de:11371/pks/lookup?op=get&search=0xD4DEFED0


More information about the Zope3-users mailing list