1 | ## $Id: container.py 12090 2014-11-29 07:57:51Z 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 | """ |
---|
19 | Containers for customers. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from thread import allocate_lock |
---|
23 | from transaction import commit |
---|
24 | from zope.component import getUtility |
---|
25 | from waeup.ikoba.customers.interfaces import ( |
---|
26 | ICustomersContainer, ICustomer, ICustomersUtils) |
---|
27 | from waeup.ikoba.utils.helpers import attrs_to_fields |
---|
28 | from waeup.ikoba.utils.logger import Logger |
---|
29 | |
---|
30 | lock = allocate_lock() # a lock object to lock threads. |
---|
31 | |
---|
32 | |
---|
33 | class CustomersContainer(grok.Container, Logger): |
---|
34 | """ |
---|
35 | The node containing the customer models |
---|
36 | """ |
---|
37 | |
---|
38 | grok.implements(ICustomersContainer) |
---|
39 | |
---|
40 | _curr_cust_id = 10 ** 6 |
---|
41 | |
---|
42 | logger_name = 'waeup.ikoba.${sitename}.customers' |
---|
43 | logger_filename = 'customers.log' |
---|
44 | |
---|
45 | @property |
---|
46 | def unique_customer_id(self): |
---|
47 | """A unique customer id. |
---|
48 | |
---|
49 | The customer id returned is guaranteed to be unique. It |
---|
50 | consists of some prefix (normally a single letter) followed by |
---|
51 | a number with at least 7 digits. |
---|
52 | |
---|
53 | Once a customer id was issued, it won't be issued again. |
---|
54 | |
---|
55 | Obtaining a customer id is currently not thread-safe but can be |
---|
56 | made easily by enabling commented lines. |
---|
57 | """ |
---|
58 | prefix = getUtility(ICustomersUtils).CUSTOMER_ID_PREFIX |
---|
59 | |
---|
60 | # lock.acquire() # lock data |
---|
61 | new_id = u'%s%s' % (prefix, self._curr_cust_id) |
---|
62 | self._curr_cust_id += 1 |
---|
63 | # self._p_changed = True |
---|
64 | # commit() |
---|
65 | # lock.release() # end of lock |
---|
66 | return new_id |
---|
67 | |
---|
68 | def addCustomer(self, customer): |
---|
69 | """Add a customer with subcontainers. |
---|
70 | """ |
---|
71 | if not ICustomer.providedBy(customer): |
---|
72 | raise TypeError( |
---|
73 | 'CustomersContainers contain only ICustomer instances') |
---|
74 | self[customer.customer_id] = customer |
---|
75 | return |
---|
76 | |
---|
77 | CustomersContainer = attrs_to_fields(CustomersContainer) |
---|