[Zope] ZODB question, again

Jonothan Farr jfarr@real.com
Thu, 16 Mar 2000 13:10:53 -0800


I would implement this whole thing as a Python product and avoid mucking around
directly with the persistence machinery altogether. This would allow you to add
a Counter object anywhere in the Zope tree.

import OFS, Persistence

class Counter(OFS.SimpleItem.Item, Persistence.Persistent):

    def __init__(self, id, title):
        self.id = id
        self.title = title
        self.counters = {}

    def Count(self, name):
        if not self.counters.has_key(name):
            self.counters[name] = 0
        v = self.counters[name] + 1
        self.counters[name] = v
        return v

manage_addCounterForm=HTMLFile('methodAdd', globals())

def manage_addCounter(self, id, title, REQUEST=None):
    ob = Counter(id, title)
    self._setObject(id, ob)
    return self.manage_main(self, REQUEST)


Add a Counter call 'counter' to a folder, then call:

<dtml-var "counter.Count('chickens')">

-jfarr

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Hi! I'm a signature virus.
Copy me into your .sig to join the fun!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~