source: main/waeup.kofa/trunk/src/waeup/kofa/accesscodes/batching.py @ 9265

Last change on this file since 9265 was 9265, checked in by Henrik Bettermann, 12 years ago

Add AccessCodeProcessor?.

  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1## $Id: batching.py 9265 2012-10-01 21:18:48Z henrik $
2##
3## Copyright (C) 2011 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"""Batch processing components for accesscodes.
19
20"""
21import grok
22from hurry.workflow.interfaces import IWorkflowState
23from zope.interface import Interface
24from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, IObjectHistory
25from waeup.kofa.utils.batching import BatchProcessor
26from waeup.kofa.accesscodes.interfaces import IAccessCodeBatch, IAccessCode
27
28class AccessCodeBatchProcessor(BatchProcessor):
29    """A batch processor for IAccessCodeBatch objects.
30    """
31    grok.implements(IBatchProcessor)
32    grok.provides(IBatchProcessor)
33    grok.context(Interface)
34    util_name = 'accesscodebatchprocessor'
35    grok.name(util_name)
36
37    name = u'AccessCodeBatch Processor'
38    iface = IAccessCodeBatch
39
40    location_fields = ['batch_id',]
41    factory_name = 'waeup.AccessCodeBatch'
42
43    mode = None
44
45    def parentsExist(self, row, site):
46        return 'accesscodes' in site.keys()
47
48    def entryExists(self, row, site):
49        return row['batch_id'] in site['accesscodes'].keys()
50
51    def getParent(self, row, site):
52        return site['accesscodes']
53
54    def getEntry(self, row, site):
55        if not self.entryExists(row, site):
56            return None
57        parent = self.getParent(row, site)
58        return parent.get(row['batch_id'])
59
60    def addEntry(self, obj, row, site):
61        parent = self.getParent(row, site)
62        parent.addBatchByImport(obj, row['batch_id'])
63        return
64
65    def updateEntry(self, obj, row, site):
66        """Update obj to the values given in row.
67
68        Returns a string describing the fields changed.
69        """
70        items_changed = super(AccessCodeBatchProcessor, self).updateEntry(
71            obj, row, site)
72        # Log actions...
73        location_field = self.location_fields[0]
74        grok.getSite()['accesscodes'].logger.info('%s - %s - Batch updated: %s'
75            % (self.name, row[location_field], items_changed))
76        return
77
78class AccessCodeProcessor(BatchProcessor):
79    """A batch processor for IAccessCode objects.
80    """
81    grok.implements(IBatchProcessor)
82    grok.provides(IBatchProcessor)
83    grok.context(Interface)
84    util_name = 'accesscodeprocessor'
85    grok.name(util_name)
86
87    name = u'AccessCode Processor'
88    iface = IAccessCode
89
90    location_fields = ['representation', 'batch_prefix', 'batch_num']
91    factory_name = 'waeup.AccessCode'
92
93    mode = None
94
95    @property
96    def available_fields(self):
97        return sorted(list(set(
98                    self.location_fields + [
99                        'random_num', 'owner', 'cost',
100                        'state', 'batch_serial',
101                        'history',]
102                    )))
103
104    @property
105    def required_fields(self):
106        return ['random_num', 'state', 'batch_serial', 'history',]
107
108    def parentsExist(self, row, site):
109        return self.getParent(row,site) is not None
110
111    def entryExists(self, row, site):
112        parent = self.getParent(row, site)
113        if parent is None:
114            return False
115        return row['representation'] in parent.keys()
116
117    def getParent(self, row, site):
118        if not 'accesscodes' in site.keys():
119            return None
120        batch_id = '%s-%s' % (row['batch_prefix'], row['batch_num'])
121        return site['accesscodes'].get(batch_id, None)
122
123    def getEntry(self, row, site):
124        if not self.entryExists(row, site):
125            return None
126        parent = self.getParent(row, site)
127        return parent.get(row['representation'], None)
128
129    def addEntry(self, obj, row, site):
130        parent = self.getParent(row, site)
131        obj.batch_serial = row['batch_serial']
132        obj.random_num = row['random_num']
133        parent[row['representation']] = obj
134        return
135
136    def updateEntry(self, obj, row, site):
137        """Update obj to the values given in row.
138
139        Returns a string describing the fields changed.
140        """
141        items_changed = ''
142        # Update state
143        if row.has_key('state'):
144            state = row.get('state', IGNORE_MARKER)
145            if state not in (IGNORE_MARKER, ''):
146                value = row['state']
147                IWorkflowState(obj).setState(value)
148                items_changed += ('%s=%s, ' % ('state', state))
149            row.pop('state')
150        # Update history
151        if row.has_key('history'):
152            history = row.get('history', IGNORE_MARKER)
153            if history not in (IGNORE_MARKER, ''):
154                values = row['history'].split('||')
155                for value in values:
156                    IObjectHistory(obj).addMessage(value)
157                items_changed += ('%s=%s, ' % ('history', history))
158            row.pop('history')
159
160        # In import files we can use the hash symbol at the end of a
161        # random_num string to avoid annoying automatic number transformation
162        # by Excel or Calc
163        row['random_num'] = row['random_num'].strip('#')
164
165        items_changed += super(AccessCodeProcessor, self).updateEntry(
166            obj, row, site)
167
168        # Log actions...
169        grok.getSite()['accesscodes'].logger.info('%s - %s - AC updated: %s'
170            % (self.name, row['representation'], items_changed))
171        return
Note: See TracBrowser for help on using the repository browser.