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

Last change on this file since 13100 was 12883, checked in by Henrik Bettermann, 9 years ago

More docs.

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1## $Id: batching.py 12883 2015-04-25 06:13:18Z 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    """The Hostel Procesor imports hostels, i.e. the container objects of
30    beds. It does not import beds. There is nothing special about this
31    processor.
32    """
33    grok.implements(IBatchProcessor)
34    grok.provides(IBatchProcessor)
35    grok.context(Interface)
36    util_name = 'hostelprocessor'
37    grok.name(util_name)
38
39    name = _('Hostel Processor')
40    iface = IHostel
41
42    location_fields = ['hostel_id',]
43    factory_name = 'waeup.Hostel'
44
45    mode = None
46
47    def parentsExist(self, row, site):
48        return 'hostels' in site.keys()
49
50    def entryExists(self, row, site):
51        return row['hostel_id'] in site['hostels'].keys()
52
53    def getParent(self, row, site):
54        return site['hostels']
55
56    def getEntry(self, row, site):
57        if not self.entryExists(row, site):
58            return None
59        parent = self.getParent(row, site)
60        return parent.get(row['hostel_id'])
61
62    def addEntry(self, obj, row, site):
63        parent = self.getParent(row, site)
64        parent.addHostel(obj)
65        return
66
67    def updateEntry(self, obj, row, site, filename):
68        """Update obj to the values given in row.
69        """
70        changed = []
71        for key, value in row.items():
72            # Skip fields to be ignored.
73            if value == IGNORE_MARKER:
74                continue
75            if not hasattr(obj, key):
76                continue
77            try:
78                evalvalue = eval(value)
79                if isinstance(evalvalue, list):
80                    value = evalvalue
81            except:
82                pass
83            setattr(obj, key, value)
84            log_value = getattr(value, 'code', value)
85            changed.append('%s=%s' % (key, log_value))
86
87        # Log actions...
88        location_field = self.location_fields[0]
89        items_changed = ', '.join(changed)
90        grok.getSite()['hostels'].logger.info(
91            '%s - %s - %s - updated: %s'
92            % (self.name, filename, row[location_field], items_changed))
93        return
Note: See TracBrowser for help on using the repository browser.