## $Id: batching.py 9265 2012-10-01 21:18:48Z henrik $ ## ## Copyright (C) 2011 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 ## """Batch processing components for accesscodes. """ import grok from hurry.workflow.interfaces import IWorkflowState from zope.interface import Interface from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, IObjectHistory from waeup.kofa.utils.batching import BatchProcessor from waeup.kofa.accesscodes.interfaces import IAccessCodeBatch, IAccessCode class AccessCodeBatchProcessor(BatchProcessor): """A batch processor for IAccessCodeBatch objects. """ grok.implements(IBatchProcessor) grok.provides(IBatchProcessor) grok.context(Interface) util_name = 'accesscodebatchprocessor' grok.name(util_name) name = u'AccessCodeBatch Processor' iface = IAccessCodeBatch location_fields = ['batch_id',] factory_name = 'waeup.AccessCodeBatch' mode = None def parentsExist(self, row, site): return 'accesscodes' in site.keys() def entryExists(self, row, site): return row['batch_id'] in site['accesscodes'].keys() def getParent(self, row, site): return site['accesscodes'] def getEntry(self, row, site): if not self.entryExists(row, site): return None parent = self.getParent(row, site) return parent.get(row['batch_id']) def addEntry(self, obj, row, site): parent = self.getParent(row, site) parent.addBatchByImport(obj, row['batch_id']) return def updateEntry(self, obj, row, site): """Update obj to the values given in row. Returns a string describing the fields changed. """ items_changed = super(AccessCodeBatchProcessor, self).updateEntry( obj, row, site) # Log actions... location_field = self.location_fields[0] grok.getSite()['accesscodes'].logger.info('%s - %s - Batch updated: %s' % (self.name, row[location_field], items_changed)) return class AccessCodeProcessor(BatchProcessor): """A batch processor for IAccessCode objects. """ grok.implements(IBatchProcessor) grok.provides(IBatchProcessor) grok.context(Interface) util_name = 'accesscodeprocessor' grok.name(util_name) name = u'AccessCode Processor' iface = IAccessCode location_fields = ['representation', 'batch_prefix', 'batch_num'] factory_name = 'waeup.AccessCode' mode = None @property def available_fields(self): return sorted(list(set( self.location_fields + [ 'random_num', 'owner', 'cost', 'state', 'batch_serial', 'history',] ))) @property def required_fields(self): return ['random_num', 'state', 'batch_serial', 'history',] def parentsExist(self, row, site): return self.getParent(row,site) is not None def entryExists(self, row, site): parent = self.getParent(row, site) if parent is None: return False return row['representation'] in parent.keys() def getParent(self, row, site): if not 'accesscodes' in site.keys(): return None batch_id = '%s-%s' % (row['batch_prefix'], row['batch_num']) return site['accesscodes'].get(batch_id, None) def getEntry(self, row, site): if not self.entryExists(row, site): return None parent = self.getParent(row, site) return parent.get(row['representation'], None) def addEntry(self, obj, row, site): parent = self.getParent(row, site) obj.batch_serial = row['batch_serial'] obj.random_num = row['random_num'] parent[row['representation']] = obj return def updateEntry(self, obj, row, site): """Update obj to the values given in row. Returns a string describing the fields changed. """ items_changed = '' # Update state if row.has_key('state'): state = row.get('state', IGNORE_MARKER) if state not in (IGNORE_MARKER, ''): value = row['state'] IWorkflowState(obj).setState(value) items_changed += ('%s=%s, ' % ('state', state)) row.pop('state') # Update history if row.has_key('history'): history = row.get('history', IGNORE_MARKER) if history not in (IGNORE_MARKER, ''): values = row['history'].split('||') for value in values: IObjectHistory(obj).addMessage(value) items_changed += ('%s=%s, ' % ('history', history)) row.pop('history') # In import files we can use the hash symbol at the end of a # random_num string to avoid annoying automatic number transformation # by Excel or Calc row['random_num'] = row['random_num'].strip('#') items_changed += super(AccessCodeProcessor, self).updateEntry( obj, row, site) # Log actions... grok.getSite()['accesscodes'].logger.info('%s - %s - AC updated: %s' % (self.name, row['representation'], items_changed)) return