source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/container.py @ 12045

Last change on this file since 12045 was 11956, checked in by Henrik Bettermann, 10 years ago

Add container for customers.

File size: 2.7 KB
Line 
1## $Id: container.py 8737 2012-06-17 07:32:08Z henrik $
2##
3## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""
19Containers for customers.
20"""
21import grok
22from thread import allocate_lock
23from transaction import commit
24from zope.component import getUtility
25from waeup.ikoba.customers.interfaces import (
26    ICustomersContainer, ICustomer, ICustomersUtils)
27from waeup.ikoba.utils.helpers import attrs_to_fields
28from waeup.ikoba.utils.logger import Logger
29
30lock = allocate_lock() # a lock object to lock threads.
31
32class CustomersContainer(grok.Container, Logger):
33    """
34    The node containing the customer models
35    """
36
37    grok.implements(ICustomersContainer)
38
39    _curr_cust_id = 10 ** 6
40
41    logger_name = 'waeup.ikoba.${sitename}.customers'
42    logger_filename = 'customers.log'
43
44    @property
45    def unique_customer_id(self):
46        """A unique customer id.
47
48        The customer id returned is guaranteed to be unique. It
49        consists of some prefix (normally a single letter) followed by
50        a number with at least 7 digits.
51
52        Once a customer id was issued, it won't be issued again.
53
54        Obtaining a customer id is currently not thread-safe but can be
55        made easily by enabling commented lines.
56        """
57        prefix = getUtility(ICustomersUtils).CUSTOMER_ID_PREFIX
58
59        # lock.acquire() # lock data
60        new_id = u'%s%s' % (prefix, self._curr_cust_id)
61        self._curr_cust_id += 1
62        # self._p_changed = True
63        # commit()
64        # lock.release() # end of lock
65        return new_id
66
67    def archive(self, id=None):
68        raise NotImplementedError()
69
70    def clear(self, id=None, archive=True):
71        raise NotImplementedError()
72
73    def addCustomer(self, customer):
74        """Add a customer with subcontainers.
75        """
76        if not ICustomer.providedBy(customer):
77            raise TypeError(
78                'CustomersContainers contain only ICustomer instances')
79        self[customer.customer_id] = customer
80        return
81
82CustomersContainer = attrs_to_fields(CustomersContainer)
Note: See TracBrowser for help on using the repository browser.