1 | ## $Id: batching.py 9307 2012-10-07 21:45:46Z 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 | 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 = eval(value) |
---|
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 |
---|