reimplement relation-based collection attributes
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2844 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
a93665a072
commit
cb7c2927a1
5 changed files with 51 additions and 36 deletions
|
@ -8,35 +8,59 @@ Commerce: Shope, Products, Customers, Orders, ...
|
|||
Shops and Products
|
||||
==================
|
||||
|
||||
Let's start with a Shop:
|
||||
Let's start with two shops:
|
||||
|
||||
>>> from cybertools.commerce.base import Shop
|
||||
>>> from cybertools.commerce.shop import Shop
|
||||
>>> shop1 = Shop(u'shop1', u'PC up Ltd')
|
||||
>>> shop2 = Shop(u'shop2', u'Video up Ltd')
|
||||
|
||||
Now we add products to the shop.
|
||||
Now we add products to the shops.
|
||||
|
||||
>>> from cybertools.commerce.product import Product
|
||||
>>> p001 = Product(u'p001', u'Silent Case')
|
||||
>>> p002 = Product(u'p002', u'Portable Projector')
|
||||
>>> p003 = Product(u'p003', u'HD Flatscreen Monitor')
|
||||
>>> p004 = Product(u'p004', u'Giga Mainboard')
|
||||
|
||||
>>> shop1.products.add(p001)
|
||||
>>> shop1.products.add(p003)
|
||||
>>> shop1.products.add(p004)
|
||||
>>> shop2.products.add(p002)
|
||||
>>> shop2.products.add(p003)
|
||||
|
||||
>>> sorted((name, p.title) for name, p in shop1.products.items())
|
||||
[(u'p001', u'Silent Case')]
|
||||
>>> sorted((p.productId, p.title) for p in shop1.products)
|
||||
[(u'p001', u'Silent Case'), (u'p003', u'HD Flatscreen Monitor'),
|
||||
(u'p004', u'Giga Mainboard')]
|
||||
|
||||
Let's have a look at the product - it should correctly reference the shop
|
||||
Let's have a look at the product - it should correctly reference the shops
|
||||
it belongs to.
|
||||
|
||||
>>> len(p001.shops)
|
||||
1
|
||||
>>> p001.shops.keys()[0]
|
||||
u'shop1'
|
||||
>>> sorted((s.name, s.title) for s in p003.shops)
|
||||
[(u'shop1', u'PC up Ltd'), (u'shop2', u'Video up Ltd')]
|
||||
|
||||
|
||||
Customers
|
||||
=========
|
||||
|
||||
>>> from cybertools.commerce.customer import Customer
|
||||
>>> c001 = Customer(u'c001', u'Your Local Store')
|
||||
>>> c001 = Customer(u'c001', u'Your Local Computer Store')
|
||||
>>> c002 = Customer(u'c002', u'Speedy Gonzales')
|
||||
>>> c003 = Customer(u'c003', u'TeeVee')
|
||||
>>> c004 = Customer(u'c004', u'MacVideo')
|
||||
|
||||
>>> shop1.customers.add(c001)
|
||||
>>> shop1.customers.add(c002)
|
||||
>>> shop1.customers.add(c004)
|
||||
>>> shop2.customers.add(c002)
|
||||
>>> shop2.customers.add(c003)
|
||||
>>> shop2.customers.add(c004)
|
||||
|
||||
>>> sorted((c.customerId, c.title) for c in shop1.customers)
|
||||
[(u'c001', u'Your Local Computer Store'), (u'c002', u'Speedy Gonzales'),
|
||||
(u'c004', u'MacVideo')]
|
||||
|
||||
>>> sorted((s.name, s.title) for s in c002.shops)
|
||||
[(u'shop1', u'PC up Ltd'), (u'shop2', u'Video up Ltd')]
|
||||
|
||||
|
||||
Orders
|
||||
|
|
|
@ -32,7 +32,7 @@ class RelationSet(object):
|
|||
|
||||
def add(self, related):
|
||||
self.data[related.name] = related
|
||||
relatedData = getattr(related, self.attributeName)
|
||||
relatedData = getattr(related, self.attributeName).data
|
||||
relatedData[self.parent.name] = self.parent
|
||||
|
||||
def remove(self, related):
|
||||
|
@ -42,24 +42,9 @@ class RelationSet(object):
|
|||
else:
|
||||
name = related.name
|
||||
del self.data[name]
|
||||
relatedData = getattr(related, self.attributeName)
|
||||
relatedData = getattr(related, self.attributeName).data
|
||||
del relatedData[self.parent.name]
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.data[key]
|
||||
|
||||
def get(self, key, default=None):
|
||||
return self.data.get(key, default)
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in self.data
|
||||
|
||||
def items(self):
|
||||
return self.data.items()
|
||||
|
||||
def keys(self):
|
||||
return self.data.keys()
|
||||
|
||||
def values(self):
|
||||
return self.data.values()
|
||||
|
||||
def __iter__(self):
|
||||
for obj in self.data.values():
|
||||
yield obj
|
||||
|
|
|
@ -24,6 +24,7 @@ $Id$
|
|||
|
||||
from zope.interface import implements, Interface
|
||||
|
||||
from cybertools.commerce.common import RelationSet
|
||||
from cybertools.commerce.interfaces import ICustomer
|
||||
|
||||
|
||||
|
@ -31,8 +32,10 @@ class Customer(object):
|
|||
|
||||
implements(ICustomer)
|
||||
|
||||
collection = RelationSet
|
||||
|
||||
def __init__(self, customerId, title=None, client=None):
|
||||
self.name = self.customerId = customerId
|
||||
self.title = title or u'unknown'
|
||||
self.client = client
|
||||
self.shops = {}
|
||||
self.shops = self.collection(self, 'customers')
|
||||
|
|
|
@ -24,6 +24,7 @@ $Id$
|
|||
|
||||
from zope.interface import implements, Interface
|
||||
|
||||
from cybertools.commerce.common import RelationSet
|
||||
from cybertools.commerce.interfaces import IProduct
|
||||
|
||||
|
||||
|
@ -31,8 +32,10 @@ class Product(object):
|
|||
|
||||
implements(IProduct)
|
||||
|
||||
collection = RelationSet
|
||||
|
||||
def __init__(self, productId, title=None):
|
||||
self.name = self.productId = productId
|
||||
self.title = title or u'unknown'
|
||||
self.shops = {}
|
||||
self.shops = self.collection(self, 'products')
|
||||
|
||||
|
|
|
@ -32,10 +32,10 @@ class Shop(object):
|
|||
|
||||
implements(IShop)
|
||||
|
||||
mappingAttribute = RelationSet
|
||||
collection = RelationSet
|
||||
|
||||
def __init__(self, name, title=None):
|
||||
self.name = name
|
||||
self.title = title or u'Shop'
|
||||
self.products = self.mappingAttribute(self, 'shops')
|
||||
self.customers = self.mappingAttribute(self, 'shops')
|
||||
self.products = self.collection(self, 'shops')
|
||||
self.customers = self.collection(self, 'shops')
|
Loading…
Add table
Reference in a new issue