add manager class + functionality
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2849 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
2cd4877488
commit
61755379ed
4 changed files with 95 additions and 13 deletions
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
41
commerce/manager.py
Normal file
41
commerce/manager.py
Normal file
|
@ -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')
|
Loading…
Add table
Reference in a new issue