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 hostels. |
---|
19 | |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from zope.interface import Interface |
---|
23 | from waeup.kofa.interfaces import IBatchProcessor, IGNORE_MARKER |
---|
24 | from waeup.kofa.utils.batching import BatchProcessor |
---|
25 | from waeup.kofa.hostels.interfaces import IHostel |
---|
26 | |
---|
27 | class 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 | 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()['hostels'].logger.info('%s - %s - Record updated: %s' |
---|
90 | % (self.name, row[location_field], items_changed)) |
---|
91 | return |
---|