1 | ## $Id: batching.py 12073 2014-11-27 21:11:32Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 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 product objects. |
---|
19 | |
---|
20 | Batch processors eat CSV files to add, update or remove large numbers |
---|
21 | of certain kinds of objects at once. |
---|
22 | """ |
---|
23 | import grok |
---|
24 | import unicodecsv as csv # XXX: csv ops should move to dedicated module. |
---|
25 | from time import time |
---|
26 | from datetime import datetime |
---|
27 | from zope.i18n import translate |
---|
28 | from zope.interface import Interface |
---|
29 | from zope.schema import getFields |
---|
30 | from zope.component import queryUtility, getUtility, createObject |
---|
31 | from zope.event import notify |
---|
32 | from zope.catalog.interfaces import ICatalog |
---|
33 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo |
---|
34 | from waeup.ikoba.interfaces import ( |
---|
35 | IBatchProcessor, FatalCSVError, IObjectConverter, IUserAccount, |
---|
36 | IObjectHistory, IGNORE_MARKER) |
---|
37 | from waeup.ikoba.interfaces import IIkobaUtils |
---|
38 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
39 | from waeup.ikoba.products.interfaces import IProduct |
---|
40 | from waeup.ikoba.utils.batching import BatchProcessor |
---|
41 | |
---|
42 | |
---|
43 | class ProductProcessor(BatchProcessor): |
---|
44 | """A batch processor for IProduct objects. |
---|
45 | """ |
---|
46 | grok.implements(IBatchProcessor) |
---|
47 | grok.provides(IBatchProcessor) |
---|
48 | grok.context(Interface) |
---|
49 | util_name = 'productprocessor' |
---|
50 | grok.name(util_name) |
---|
51 | |
---|
52 | name = _('Product Processor') |
---|
53 | iface = IProduct |
---|
54 | |
---|
55 | location_fields = ['product_id',] |
---|
56 | factory_name = 'waeup.Product' |
---|
57 | |
---|
58 | mode = None |
---|
59 | |
---|
60 | def parentsExist(self, row, site): |
---|
61 | return 'products' in site.keys() |
---|
62 | |
---|
63 | def entryExists(self, row, site): |
---|
64 | return row['product_id'] in site['products'].keys() |
---|
65 | |
---|
66 | def getParent(self, row, site): |
---|
67 | return site['products'] |
---|
68 | |
---|
69 | def getEntry(self, row, site): |
---|
70 | if not self.entryExists(row, site): |
---|
71 | return None |
---|
72 | parent = self.getParent(row, site) |
---|
73 | return parent.get(row['product_id']) |
---|
74 | |
---|
75 | def addEntry(self, obj, row, site): |
---|
76 | parent = self.getParent(row, site) |
---|
77 | parent.addProduct(obj) |
---|
78 | return |
---|
79 | |
---|
80 | def delEntry(self, row, site): |
---|
81 | product = self.getEntry(row, site) |
---|
82 | parent = self.getParent(row, site) |
---|
83 | if product is not None: |
---|
84 | grok.getSite().logger.info( |
---|
85 | '%s - Product removed' % product.product_id) |
---|
86 | del parent[product.product_id] |
---|
87 | return |
---|
88 | |
---|
89 | def updateEntry(self, obj, row, site, filename): |
---|
90 | """Update obj to the values given in row. |
---|
91 | """ |
---|
92 | items_changed = super(ProductProcessor, self).updateEntry( |
---|
93 | obj, row, site, filename) |
---|
94 | # Log actions... |
---|
95 | location_field = self.location_fields[0] |
---|
96 | grok.getSite().logger.info( |
---|
97 | '%s - %s - %s - updated: %s' |
---|
98 | % (self.name, filename, row[location_field], items_changed)) |
---|
99 | return |
---|