provide basic customer implementation
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2806 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
621d894799
commit
14493af40a
6 changed files with 122 additions and 16 deletions
|
@ -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
|
||||
======
|
||||
|
|
|
@ -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')
|
||||
|
|
65
commerce/common.py
Normal file
65
commerce/common.py
Normal file
|
@ -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()
|
||||
|
38
commerce/customer.py
Normal file
38
commerce/customer.py
Normal file
|
@ -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 = {}
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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 = {}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue