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

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

Add batch processor for ac batches.

  • Property svn:keywords set to Id
File size: 3.0 KB
RevLine 
[9263]1## $Id: batching.py 9263 2012-10-01 06:55:04Z 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 zope.interface import Interface
23from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER
24from waeup.kofa.utils.batching import BatchProcessor
25from waeup.kofa.accesscodes.interfaces import IAccessCodeBatch
26
27class AccessCodeBatchProcessor(BatchProcessor):
28    """A batch processor for IAccessCodeBatch objects.
29    """
30    grok.implements(IBatchProcessor)
31    grok.provides(IBatchProcessor)
32    grok.context(Interface)
33    util_name = 'accesscodebatchprocessor'
34    grok.name(util_name)
35
36    name = u'AccessCodeBatch Processor'
37    iface = IAccessCodeBatch
38
39    location_fields = ['batch_id',]
40    factory_name = 'waeup.AccessCodeBatch'
41
42    mode = None
43
44    def parentsExist(self, row, site):
45        return 'accesscodes' in site.keys()
46
47    def entryExists(self, row, site):
48        return row['batch_id'] in site['accesscodes'].keys()
49
50    def getParent(self, row, site):
51        return site['accesscodes']
52
53    def getEntry(self, row, site):
54        if not self.entryExists(row, site):
55            return None
56        parent = self.getParent(row, site)
57        return parent.get(row['batch_id'])
58
59    def addEntry(self, obj, row, site):
60        parent = self.getParent(row, site)
61        parent.addBatchByImport(obj, row['batch_id'])
62        return
63
64    def updateEntry(self, obj, row, site):
65        """Update obj to the values given in row.
66
67        Returns a string describing the fields changed.
68        """
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
86        # Log actions...
87        location_field = self.location_fields[0]
88        items_changed = ', '.join(changed)
89        grok.getSite()['accesscodes'].logger.info('%s - %s - Batch updated: %s'
90            % (self.name, row[location_field], items_changed))
91        return
Note: See TracBrowser for help on using the repository browser.