1 | ## $Id: utils.py 12035 2014-11-22 10:14:38Z 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 | """General helper functions and utilities for the customer section. |
---|
19 | """ |
---|
20 | import re |
---|
21 | import os |
---|
22 | import grok |
---|
23 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
24 | from waeup.ikoba.interfaces import CREATED, STARTED, REQUESTED, APPROVED |
---|
25 | from waeup.ikoba.customers.interfaces import ICustomersUtils |
---|
26 | |
---|
27 | RE_CUSTID_NON_NUM = re.compile('[^\d]+') |
---|
28 | |
---|
29 | def generate_customer_id(): |
---|
30 | customers = grok.getSite()['customers'] |
---|
31 | new_id = customers.unique_customer_id |
---|
32 | return new_id |
---|
33 | |
---|
34 | def path_from_custid(customer_id): |
---|
35 | """Convert a customer_id into a predictable relative folder path. |
---|
36 | |
---|
37 | Used for storing files. |
---|
38 | |
---|
39 | Returns the name of folder in which files for a particular customer |
---|
40 | should be stored. This is a relative path, relative to any general |
---|
41 | customers folder with 5 zero-padded digits (except when customer_id |
---|
42 | is overlong). |
---|
43 | |
---|
44 | We normally map 1,000 different customer ids into one single |
---|
45 | path. For instance ``K1000000`` will give ``01000/K1000000``, |
---|
46 | ``K1234567`` will give ``0123/K1234567`` and ``K12345678`` will |
---|
47 | result in ``1234/K12345678``. |
---|
48 | |
---|
49 | For lower numbers < 10**6 we return the same path for up to 10,000 |
---|
50 | customer_ids. So for instance ``KM123456`` will result in |
---|
51 | ``00120/KM123456`` (there will be no path starting with |
---|
52 | ``00123``). |
---|
53 | |
---|
54 | Works also with overlong number: here the leading zeros will be |
---|
55 | missing but ``K123456789`` will give reliably |
---|
56 | ``12345/K123456789`` as expected. |
---|
57 | """ |
---|
58 | # remove all non numeric characters and turn this into an int. |
---|
59 | num = int(RE_CUSTID_NON_NUM.sub('', customer_id)) |
---|
60 | if num < 10**6: |
---|
61 | # store max. of 10000 custs per folder and correct num for 5 digits |
---|
62 | num = num / 10000 * 10 |
---|
63 | else: |
---|
64 | # store max. of 1000 custs per folder |
---|
65 | num = num / 1000 |
---|
66 | # format folder name to have 5 zero-padded digits |
---|
67 | folder_name = u'%05d' % num |
---|
68 | folder_name = os.path.join(folder_name, customer_id) |
---|
69 | return folder_name |
---|
70 | |
---|
71 | class CustomersUtils(grok.GlobalUtility): |
---|
72 | """A collection of methods subject to customization. |
---|
73 | """ |
---|
74 | grok.implements(ICustomersUtils) |
---|
75 | |
---|
76 | #: A prefix used when generating new customer ids. Each customer id will |
---|
77 | #: start with this string. The default is 'K'. |
---|
78 | CUSTOMER_ID_PREFIX = u'K' |
---|
79 | |
---|
80 | CUSTMANAGE_STATES = (STARTED,) |
---|
81 | |
---|
82 | DOCMANAGE_STATES = (APPROVED,) |
---|
83 | |
---|
84 | SKIP_UPLOAD_VIEWLETS = () |
---|
85 | |
---|
86 | TRANSLATED_STATES = { |
---|
87 | CREATED: _('created'), |
---|
88 | STARTED: _('started'), |
---|
89 | REQUESTED: _('requested'), |
---|
90 | APPROVED: _('approved'), |
---|
91 | } |
---|