From 61755379edd908fc5235d4e7047046538df99de7 Mon Sep 17 00:00:00 2001 From: helmutm Date: Sun, 24 Aug 2008 11:27:48 +0000 Subject: [PATCH] add manager class + functionality git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2849 fd906abe-77d9-0310-91a1-e0d9ade77398 --- commerce/README.txt | 29 ++++++++++++++++------------- commerce/common.py | 26 ++++++++++++++++++++++++++ commerce/interfaces.py | 12 ++++++++++++ commerce/manager.py | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 commerce/manager.py diff --git a/commerce/README.txt b/commerce/README.txt index 6393b03..4b9a8cc 100644 --- a/commerce/README.txt +++ b/commerce/README.txt @@ -4,23 +4,27 @@ Commerce: Shope, Products, Customers, Orders, ... ($Id$) + >>> from cybertools.commerce.manager import Manager + >>> manager = Manager() + Shops and Products ================== Let's start with two shops: - >>> from cybertools.commerce.shop import Shop - >>> shop1 = Shop(u'shop1', u'PC up Ltd') - >>> shop2 = Shop(u'shop2', u'Video up Ltd') + >>> shop1 = manager.shops.create(u'shop1', title=u'PC up Ltd') + >>> shop2 = manager.shops.create(u'shop2', title=u'Video up Ltd') + + >>> len(list(manager.shops)) + 2 Now we add products to the shops. - >>> from cybertools.commerce.product import Product - >>> p001 = Product(u'001', u'Silent Case') - >>> p002 = Product(u'002', u'Portable Projector') - >>> p003 = Product(u'003', u'HD Flatscreen Monitor') - >>> p004 = Product(u'004', u'Giga Mainboard') + >>> p001 = manager.products.create(u'001', title=u'Silent Case') + >>> p002 = manager.products.create(u'002', title=u'Portable Projector') + >>> p003 = manager.products.create(u'003', title=u'HD Flatscreen Monitor') + >>> p004 = manager.products.create(u'004', title=u'Giga Mainboard') >>> shop1.products.add(p001) >>> shop1.products.add(p003) @@ -42,11 +46,10 @@ it belongs to. Customers ========= - >>> from cybertools.commerce.customer import Customer - >>> c001 = Customer(u'001', u'Your Local Computer Store') - >>> c002 = Customer(u'002', u'Speedy Gonzales') - >>> c003 = Customer(u'003', u'TeeVee') - >>> c004 = Customer(u'004', u'MacVideo') + >>> c001 = manager.customers.create(u'001', title=u'Your Local Computer Store') + >>> c002 = manager.customers.create(u'002', title=u'Speedy Gonzales') + >>> c003 = manager.customers.create(u'003', title=u'TeeVee') + >>> c004 = manager.customers.create(u'004', title=u'MacVideo') >>> shop1.customers.add(c001) >>> shop1.customers.add(c002) diff --git a/commerce/common.py b/commerce/common.py index 8c8a102..592e601 100644 --- a/commerce/common.py +++ b/commerce/common.py @@ -23,6 +23,32 @@ $Id$ """ +class ContainerAttribute(object): + + def __init__(self, factory, idAttr='name'): + self.factory = factory + self.idAttr = idAttr + self.data = {} + + def create(self, id, **kw): + if self.idAttr not in kw: + kw[self.idAttr] = id + obj = self.factory(id) + for k, v in kw.items(): + setattr(obj, k, v) + self.data[id] = obj + return obj + + def remove(self, id): + del self.data[id] + + def get(self, id, default=None): + return self.data.get(id, default) + + def __iter__(self): + return iter(self.data.values()) + + class RelationSet(object): def __init__(self, parent, attributeName): diff --git a/commerce/interfaces.py b/commerce/interfaces.py index 4e8a09e..03a5788 100644 --- a/commerce/interfaces.py +++ b/commerce/interfaces.py @@ -33,6 +33,18 @@ from loops import util _ = MessageFactory('cybertools.commerce') +# manager + +class IManager(Interface): + """ A top-level container, registry, manager that provides access to + all components of a commerce site. + """ + + shops = Attribute('All shops in this commerce manager.') + products = Attribute('All products in this commerce manager.') + customers = Attribute('All customers in this commerce manager.') + + # shops class IShop(Interface): diff --git a/commerce/manager.py b/commerce/manager.py new file mode 100644 index 0000000..c24b132 --- /dev/null +++ b/commerce/manager.py @@ -0,0 +1,41 @@ +# +# 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 +# + +""" +The commerce manager (container, registry, ...). + +$Id$ +""" + +from zope.interface import implements + +from cybertools.commerce.common import ContainerAttribute +from cybertools.commerce.customer import Customer +from cybertools.commerce.interfaces import IManager +from cybertools.commerce.product import Product +from cybertools.commerce.shop import Shop + + +class Manager(object): + + implements(IManager) + + def __init__(self): + self.shops = ContainerAttribute(Shop) + self.products = ContainerAttribute(Product, 'productId') + self.customers = ContainerAttribute(Customer, 'customerId')