Ignore:
Timestamp:
1 Oct 2012, 21:18:48 (12 years ago)
Author:
Henrik Bettermann
Message:

Add AccessCodeProcessor?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/accesscodes/batching.py

    r9263 r9265  
    2020"""
    2121import grok
     22from hurry.workflow.interfaces import IWorkflowState
    2223from zope.interface import Interface
    23 from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER
     24from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER, IObjectHistory
    2425from waeup.kofa.utils.batching import BatchProcessor
    25 from waeup.kofa.accesscodes.interfaces import IAccessCodeBatch
     26from waeup.kofa.accesscodes.interfaces import IAccessCodeBatch, IAccessCode
    2627
    2728class AccessCodeBatchProcessor(BatchProcessor):
     
    6768        Returns a string describing the fields changed.
    6869        """
    69         changed = []
    70         for key, value in row.items():
    71             # Skip fields to be ignored.
    72             if value == IGNORE_MARKER:
    73                 continue
    74             if not hasattr(obj, key):
    75                 continue
    76             try:
    77                 evalvalue = eval(value)
    78                 if isinstance(evalvalue, list):
    79                     value = eval(value)
    80             except:
    81                 pass
    82             setattr(obj, key, value)
    83             log_value = getattr(value, 'code', value)
    84             changed.append('%s=%s' % (key, log_value))
    85 
     70        items_changed = super(AccessCodeBatchProcessor, self).updateEntry(
     71            obj, row, site)
    8672        # Log actions...
    8773        location_field = self.location_fields[0]
    88         items_changed = ', '.join(changed)
    8974        grok.getSite()['accesscodes'].logger.info('%s - %s - Batch updated: %s'
    9075            % (self.name, row[location_field], items_changed))
    9176        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 TracChangeset for help on using the changeset viewer.