using axiom.dependency for powerups in queries

Robert Gravina <[email protected]> Mon, 10 Mar 2008 13:12:58 +0900
Newsgroups gmane.comp.python.quotient.dev
Message-ID <[email protected]>
Hi all,

I'm having trouble moving from the old way of doing powerups to the  
new axiom.dependency method. I have had a look at the docs on the  
Wiki, but stuck with doing queries involving them.

I've contrived an example here of powering up a Person item to a  
Contractor item (with a attribute for hourly rate), and can query the  
store for a Person based on an hourly rate. Here is the old way of  
doing it:

from axiom.store import Store
from axiom.item import Item
from axiom.attributes import text, reference, money, AND
from zope.interface import implements, Interface, Attribute
from decimal import Decimal

class IContractor(Interface):
     """A Contractor works on projects on an as-needed basis."""
     rate = Attribute("""The rate per hour""")

class Contractor(Item):
     """A Contractor powerup"""
     implements(IContractor)
     typeName = "Contractor"
     rate = money()
     installedOn = reference()

     def installOn(self, other):
         assert self.installedOn is None, 'cannot instal on more than  
one object'
         self.installedOn = other
         other.powerUp(self, IContractor)

class Person(Item):
     """A Person item"""
     typeName = "Person"
     name = text()

     def __init__(self, **kw):
         Item.__init__(self, **kw)
         #make them a contractor
         Contractor(store=self.store).installOn(self)

if __name__ == "__main__":
     s = Store()
     p = Person(store=s)
     #set the rate of this contractor, and query the store for them  
based on this
     IContractor(p).rate = Decimal("40")
     c = s.findFirst(Person, AND(Contractor.installedOn ==  
Person.storeID, Contractor.rate ==  Decimal("40")))
     assert c is p


Here's my attempt at using the new Axiom dependency module. I think  
everything is OK, but not sure how to adapt the query...

from axiom.store import Store
from axiom.item import Item
from axiom.attributes import text, reference, money, AND
from axiom import dependency
from zope.interface import implements, Interface, Attribute
from decimal import Decimal

class IContractor(Interface):
     """A Contractor works on projects on an as-needed basis."""
     rate = Attribute("""The rate per hour""")

class Contractor(Item):
     """A Contractor powerup"""
     implements(IContractor)
     typeName = "Contractor"
     rate = money()
     powerupInterfaces = (IContractor,)

class Person(Item):
     """A Person item"""
     typeName = "Person"
     name = text()

     def __init__(self, **kw):
         Item.__init__(self, **kw)
         #make them a contractor
         dependency.installOn(Contractor(store=self.store), self)

if __name__ == "__main__":
     s = Store()
     p = Person(store=s)
     #set the rate of this contractor, and query the store for them  
based on this
     IContractor(p).rate = Decimal("40")
#    c = s.findFirst(Person, AND(Contractor.installedOn ==  
Person.storeID, Contractor.rate ==  Decimal("40")))
#    assert c is p

Thanks,

Robert