From cb7c2927a1a81b402c4e0e90e4482ac681e4fa13 Mon Sep 17 00:00:00 2001 From: helmutm Date: Sun, 24 Aug 2008 08:10:21 +0000 Subject: [PATCH] reimplement relation-based collection attributes git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2844 fd906abe-77d9-0310-91a1-e0d9ade77398 --- commerce/README.txt | 46 ++++++++++++++++++++++++++--------- commerce/common.py | 25 ++++--------------- commerce/customer.py | 5 +++- commerce/product.py | 5 +++- commerce/{base.py => shop.py} | 6 ++--- 5 files changed, 51 insertions(+), 36 deletions(-) rename commerce/{base.py => shop.py} (87%) diff --git a/commerce/README.txt b/commerce/README.txt index 2791177..fed2cca 100644 --- a/commerce/README.txt +++ b/commerce/README.txt @@ -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 diff --git a/commerce/common.py b/commerce/common.py index 07e1150..c6f0fca 100644 --- a/commerce/common.py +++ b/commerce/common.py @@ -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 diff --git a/commerce/customer.py b/commerce/customer.py index 54da118..59917c9 100644 --- a/commerce/customer.py +++ b/commerce/customer.py @@ -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') diff --git a/commerce/product.py b/commerce/product.py index 58a854d..e10ee14 100644 --- a/commerce/product.py +++ b/commerce/product.py @@ -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') diff --git a/commerce/base.py b/commerce/shop.py similarity index 87% rename from commerce/base.py rename to commerce/shop.py index 8cd1bff..9e4e2fd 100644 --- a/commerce/base.py +++ b/commerce/shop.py @@ -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')