## $Id: utils.py 12035 2014-11-22 10:14:38Z 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 ## """General helper functions and utilities for the customer section. """ import re import os import grok from waeup.ikoba.interfaces import MessageFactory as _ from waeup.ikoba.interfaces import CREATED, STARTED, REQUESTED, APPROVED from waeup.ikoba.customers.interfaces import ICustomersUtils RE_CUSTID_NON_NUM = re.compile('[^\d]+') def generate_customer_id(): customers = grok.getSite()['customers'] new_id = customers.unique_customer_id return new_id def path_from_custid(customer_id): """Convert a customer_id into a predictable relative folder path. Used for storing files. Returns the name of folder in which files for a particular customer should be stored. This is a relative path, relative to any general customers folder with 5 zero-padded digits (except when customer_id is overlong). We normally map 1,000 different customer ids into one single path. For instance ``K1000000`` will give ``01000/K1000000``, ``K1234567`` will give ``0123/K1234567`` and ``K12345678`` will result in ``1234/K12345678``. For lower numbers < 10**6 we return the same path for up to 10,000 customer_ids. So for instance ``KM123456`` will result in ``00120/KM123456`` (there will be no path starting with ``00123``). Works also with overlong number: here the leading zeros will be missing but ``K123456789`` will give reliably ``12345/K123456789`` as expected. """ # remove all non numeric characters and turn this into an int. num = int(RE_CUSTID_NON_NUM.sub('', customer_id)) if num < 10**6: # store max. of 10000 custs per folder and correct num for 5 digits num = num / 10000 * 10 else: # store max. of 1000 custs per folder num = num / 1000 # format folder name to have 5 zero-padded digits folder_name = u'%05d' % num folder_name = os.path.join(folder_name, customer_id) return folder_name class CustomersUtils(grok.GlobalUtility): """A collection of methods subject to customization. """ grok.implements(ICustomersUtils) #: A prefix used when generating new customer ids. Each customer id will #: start with this string. The default is 'K'. CUSTOMER_ID_PREFIX = u'K' CUSTMANAGE_STATES = (STARTED,) DOCMANAGE_STATES = (APPROVED,) SKIP_UPLOAD_VIEWLETS = () TRANSLATED_STATES = { CREATED: _('created'), STARTED: _('started'), REQUESTED: _('requested'), APPROVED: _('approved'), }