source: main/waeup.kofa/trunk/src/waeup/kofa/hostels/batching.py @ 9680

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

Import roles correctly.

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