source: main/waeup.kofa/branches/uli-stud-utils-cleanup/src/waeup/kofa/hostels/batching.py @ 12697

Last change on this file since 12697 was 11891, checked in by Henrik Bettermann, 10 years ago

Enable localization of batch processing modules.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1## $Id: batching.py 11891 2014-10-28 20:02:45Z 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 hostels.
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.hostels.interfaces import IHostel
26from waeup.kofa.interfaces import MessageFactory as _
27
28class HostelProcessor(BatchProcessor):
29    """A batch processor for IHostel objects.
30    """
31    grok.implements(IBatchProcessor)
32    grok.provides(IBatchProcessor)
33    grok.context(Interface)
34    util_name = 'hostelprocessor'
35    grok.name(util_name)
36
37    name = _('Hostel Processor')
38    iface = IHostel
39
40    location_fields = ['hostel_id',]
41    factory_name = 'waeup.Hostel'
42
43    mode = None
44
45    def parentsExist(self, row, site):
46        return 'hostels' in site.keys()
47
48    def entryExists(self, row, site):
49        return row['hostel_id'] in site['hostels'].keys()
50
51    def getParent(self, row, site):
52        return site['hostels']
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['hostel_id'])
59
60    def addEntry(self, obj, row, site):
61        parent = self.getParent(row, site)
62        parent.addHostel(obj)
63        return
64
65    def updateEntry(self, obj, row, site, filename):
66        """Update obj to the values given in row.
67        """
68        changed = []
69        for key, value in row.items():
70            # Skip fields to be ignored.
71            if value == IGNORE_MARKER:
72                continue
73            if not hasattr(obj, key):
74                continue
75            try:
76                evalvalue = eval(value)
77                if isinstance(evalvalue, list):
78                    value = evalvalue
79            except:
80                pass
81            setattr(obj, key, value)
82            log_value = getattr(value, 'code', value)
83            changed.append('%s=%s' % (key, log_value))
84
85        # Log actions...
86        location_field = self.location_fields[0]
87        items_changed = ', '.join(changed)
88        grok.getSite()['hostels'].logger.info(
89            '%s - %s - %s - updated: %s'
90            % (self.name, filename, row[location_field], items_changed))
91        return
Note: See TracBrowser for help on using the repository browser.