diff --git a/commerce/README.txt b/commerce/README.txt index 9232e57..2791177 100644 --- a/commerce/README.txt +++ b/commerce/README.txt @@ -18,7 +18,7 @@ Now we add products to the shop. >>> from cybertools.commerce.product import Product >>> p001 = Product(u'p001', u'Silent Case') - >>> shop1.addProduct(p001) + >>> shop1.products.add(p001) >>> sorted((name, p.title) for name, p in shop1.products.items()) [(u'p001', u'Silent Case')] @@ -35,6 +35,9 @@ it belongs to. Customers ========= + >>> from cybertools.commerce.customer import Customer + >>> c001 = Customer(u'c001', u'Your Local Store') + Orders ====== diff --git a/commerce/base.py b/commerce/base.py index 76336e0..8cd1bff 100644 --- a/commerce/base.py +++ b/commerce/base.py @@ -22,8 +22,9 @@ Base classes. $Id$ """ -from zope.interface import implements, Interface +from zope.interface import implements +from cybertools.commerce.common import RelationSet from cybertools.commerce.interfaces import IShop @@ -31,11 +32,10 @@ class Shop(object): implements(IShop) + mappingAttribute = RelationSet + def __init__(self, name, title=None): self.name = name self.title = title or u'Shop' - self.products = {} - - def addProduct(self, product): - self.products[product.name] = product - product.shops[self.name] = self + self.products = self.mappingAttribute(self, 'shops') + self.customers = self.mappingAttribute(self, 'shops') diff --git a/commerce/common.py b/commerce/common.py new file mode 100644 index 0000000..07e1150 --- /dev/null +++ b/commerce/common.py @@ -0,0 +1,65 @@ +# +# Copyright (c) 2008 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Common functionality. + +$Id$ +""" + + +class RelationSet(object): + + def __init__(self, parent, attributeName): + self.parent = parent + self.attributeName = attributeName + self.data = {} + + def add(self, related): + self.data[related.name] = related + relatedData = getattr(related, self.attributeName) + relatedData[self.parent.name] = self.parent + + def remove(self, related): + if isinstance(related, basestring): + name = related + related = self[name] + else: + name = related.name + del self.data[name] + relatedData = getattr(related, self.attributeName) + 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() + diff --git a/commerce/customer.py b/commerce/customer.py new file mode 100644 index 0000000..54da118 --- /dev/null +++ b/commerce/customer.py @@ -0,0 +1,38 @@ +# +# Copyright (c) 2008 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +Customer classes. + +$Id$ +""" + +from zope.interface import implements, Interface + +from cybertools.commerce.interfaces import ICustomer + + +class Customer(object): + + implements(ICustomer) + + def __init__(self, customerId, title=None, client=None): + self.name = self.customerId = customerId + self.title = title or u'unknown' + self.client = client + self.shops = {} diff --git a/commerce/interfaces.py b/commerce/interfaces.py index 0c10c46..127f836 100644 --- a/commerce/interfaces.py +++ b/commerce/interfaces.py @@ -61,10 +61,6 @@ class IShop(Interface): u'this shop.') customers = Attribute(u'The customers registered for this shop.') - def addProduct(product): - """ Add the product given to the shop's product listing. - """ - # suppliers @@ -159,8 +155,11 @@ class ICategory(Interface): # customers class ICustomer(Interface): - """ A role of - for example - a person or institution (the ``client``) + """ Typically a role of - for example - a person or institution (the ``client``) representing a customer of one or more shops. + + The client may be None in which case the customer is an object on + its own. """ customerId = schema.ASCII( @@ -169,9 +168,10 @@ class ICustomer(Interface): default='', required=True) - client = Attribute(u'The (real) client object of the customer role.') shops = Attribute(u'The shops the client object is a customer of.') + client = Attribute(u'An optional (real) client object of the customer role.') + # orders diff --git a/commerce/product.py b/commerce/product.py index 6fca2fb..58a854d 100644 --- a/commerce/product.py +++ b/commerce/product.py @@ -31,8 +31,8 @@ class Product(object): implements(IProduct) - def __init__(self, name, title=None): - self.name = name - self.title = title or u'Product' + def __init__(self, productId, title=None): + self.name = self.productId = productId + self.title = title or u'unknown' self.shops = {}