## $Id: container.py 12090 2014-11-29 07:57:51Z henrik $ ## ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann ## 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 ## """ Containers for customers. """ import grok from thread import allocate_lock from transaction import commit from zope.component import getUtility from waeup.ikoba.customers.interfaces import ( ICustomersContainer, ICustomer, ICustomersUtils) from waeup.ikoba.utils.helpers import attrs_to_fields from waeup.ikoba.utils.logger import Logger lock = allocate_lock() # a lock object to lock threads. class CustomersContainer(grok.Container, Logger): """ The node containing the customer models """ grok.implements(ICustomersContainer) _curr_cust_id = 10 ** 6 logger_name = 'waeup.ikoba.${sitename}.customers' logger_filename = 'customers.log' @property def unique_customer_id(self): """A unique customer id. The customer id returned is guaranteed to be unique. It consists of some prefix (normally a single letter) followed by a number with at least 7 digits. Once a customer id was issued, it won't be issued again. Obtaining a customer id is currently not thread-safe but can be made easily by enabling commented lines. """ prefix = getUtility(ICustomersUtils).CUSTOMER_ID_PREFIX # lock.acquire() # lock data new_id = u'%s%s' % (prefix, self._curr_cust_id) self._curr_cust_id += 1 # self._p_changed = True # commit() # lock.release() # end of lock return new_id def addCustomer(self, customer): """Add a customer with subcontainers. """ if not ICustomer.providedBy(customer): raise TypeError( 'CustomersContainers contain only ICustomer instances') self[customer.customer_id] = customer return CustomersContainer = attrs_to_fields(CustomersContainer)