source: main/waeup.ikoba/trunk/src/waeup/ikoba/products/batching.py

Last change on this file was 12073, checked in by Henrik Bettermann, 10 years ago

Add batch processor for products.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
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
20Batch processors eat CSV files to add, update or remove large numbers
21of certain kinds of objects at once.
22"""
23import grok
24import unicodecsv as csv  # XXX: csv ops should move to dedicated module.
25from time import time
26from datetime import datetime
27from zope.i18n import translate
28from zope.interface import Interface
29from zope.schema import getFields
30from zope.component import queryUtility, getUtility, createObject
31from zope.event import notify
32from zope.catalog.interfaces import ICatalog
33from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo
34from waeup.ikoba.interfaces import (
35    IBatchProcessor, FatalCSVError, IObjectConverter, IUserAccount,
36    IObjectHistory, IGNORE_MARKER)
37from waeup.ikoba.interfaces import IIkobaUtils
38from waeup.ikoba.interfaces import MessageFactory as _
39from waeup.ikoba.products.interfaces import IProduct
40from waeup.ikoba.utils.batching import BatchProcessor
41
42
43class 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
Note: See TracBrowser for help on using the repository browser.